Upgrade
This commit is contained in:
parent
d138241473
commit
f256310387
47
dist/index.js
vendored
47
dist/index.js
vendored
@ -6,6 +6,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
const fs_1 = __importDefault(require("fs"));
|
const fs_1 = __importDefault(require("fs"));
|
||||||
const child_process_1 = require("child_process");
|
const child_process_1 = require("child_process");
|
||||||
|
const grabSrcDisStrings = () => {
|
||||||
if (fs_1.default.existsSync("./lesscw.config.json")) {
|
if (fs_1.default.existsSync("./lesscw.config.json")) {
|
||||||
if (process.argv.indexOf("--src") >= 0 || process.argv.indexOf("--dst") >= 0) {
|
if (process.argv.indexOf("--src") >= 0 || process.argv.indexOf("--dst") >= 0) {
|
||||||
try {
|
try {
|
||||||
@ -41,24 +42,28 @@ if (fs_1.default.existsSync("./lesscw.config.json")) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
console.log("ERROR in your config file =>", error.message);
|
console.log("- \x1b[31mERROR:\x1b[0m Your config file has some errors. ERROR =>", error.message);
|
||||||
process.exit();
|
process.exit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const sourceFile = process.argv.indexOf("--src") >= 0 ? process.argv[process.argv.indexOf("--src") + 1] : null;
|
const sourceFile = process.argv.indexOf("--src") >= 0 ? process.argv[process.argv.indexOf("--src") + 1] : null;
|
||||||
const destinationFile = process.argv.indexOf("--dst") >= 0 ? process.argv[process.argv.indexOf("--dst") + 1] : null;
|
const destinationFile = process.argv.indexOf("--dst") >= 0 ? process.argv[process.argv.indexOf("--dst") + 1] : null;
|
||||||
console.log("\x1b[44mRunning Less compiler\x1b[0m ...");
|
return { sourceFile, destinationFile };
|
||||||
|
};
|
||||||
|
const { sourceFile, destinationFile } = grabSrcDisStrings();
|
||||||
|
console.log("- \x1b[35mStart:\x1b[0m Running Less compiler ...");
|
||||||
if (!sourceFile || !destinationFile) {
|
if (!sourceFile || !destinationFile) {
|
||||||
console.log("\x1b[33mERROR:\x1b[0m => Missing source or destination file");
|
console.log("- \x1b[31mERROR:\x1b[0m => Missing source or destination file");
|
||||||
process.exit();
|
process.exit();
|
||||||
}
|
}
|
||||||
const sourceFiles = sourceFile.split(",");
|
function traverseFiles(src, dst) {
|
||||||
const dstFiles = destinationFile.split(",");
|
const sourceFiles = src.split(",");
|
||||||
|
const dstFiles = dst.split(",");
|
||||||
for (let i = 0; i < sourceFiles.length; i++) {
|
for (let i = 0; i < sourceFiles.length; i++) {
|
||||||
const srcFolder = sourceFiles[i];
|
const srcFolder = sourceFiles[i];
|
||||||
const dstFile = dstFiles[i];
|
const dstFile = dstFiles[i];
|
||||||
if ((srcFolder === null || srcFolder === void 0 ? void 0 : srcFolder.match(/\.[^\/]+$/)) && !(srcFolder === null || srcFolder === void 0 ? void 0 : srcFolder.match(/\.less$/))) {
|
if ((srcFolder === null || srcFolder === void 0 ? void 0 : srcFolder.match(/\.[^\/]+$/)) && !(srcFolder === null || srcFolder === void 0 ? void 0 : srcFolder.match(/\.less$/))) {
|
||||||
console.log("\x1b[33mERROR:\x1b[0m Source must be a folder or a .less file");
|
console.log("- \x1b[31mERROR:\x1b[0m Source must be a folder or a .less file");
|
||||||
process.exit();
|
process.exit();
|
||||||
}
|
}
|
||||||
if (!fs_1.default.existsSync(srcFolder)) {
|
if (!fs_1.default.existsSync(srcFolder)) {
|
||||||
@ -85,13 +90,27 @@ for (let i = 0; i < sourceFiles.length; i++) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
compile(srcFolder, dstFile, null);
|
compile(srcFolder, dstFile, null);
|
||||||
fs_1.default.watch(srcFolder, { recursive: true }, (evtType, fileName) => {
|
const watcher = fs_1.default.watch(srcFolder, { recursive: true }, (evtType, fileName) => {
|
||||||
if (!fileName)
|
if (!fileName)
|
||||||
return;
|
return;
|
||||||
const filePathRoot = (srcFolder === null || srcFolder === void 0 ? void 0 : srcFolder.match(/\.less$/)) ? srcFolder : srcFolder + "/" + fileName;
|
const filePathRoot = (srcFolder === null || srcFolder === void 0 ? void 0 : srcFolder.match(/\.less$/)) ? srcFolder : srcFolder + "/" + fileName;
|
||||||
|
try {
|
||||||
|
const currentProcessArgsSrc = process.argv[process.argv.indexOf("--src") + 1];
|
||||||
|
const activeSourceFiles = currentProcessArgsSrc.split(",");
|
||||||
|
if (activeSourceFiles.includes(srcFolder)) {
|
||||||
compile(filePathRoot, dstFile, evtType);
|
compile(filePathRoot, dstFile, evtType);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
watcher.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.log("- \x1b[31mERROR:\x1b[0m Please check your config file =>", error.message);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
traverseFiles(sourceFile, destinationFile);
|
||||||
function compile(fileName, dst, evtType) {
|
function compile(fileName, dst, evtType) {
|
||||||
if ((fileName === null || fileName === void 0 ? void 0 : fileName.match(/\(/)) || (fileName.match(/\..{2,4}$/) && !(fileName === null || fileName === void 0 ? void 0 : fileName.match(/\.less$/i)))) {
|
if ((fileName === null || fileName === void 0 ? void 0 : fileName.match(/\(/)) || (fileName.match(/\..{2,4}$/) && !(fileName === null || fileName === void 0 ? void 0 : fileName.match(/\.less$/i)))) {
|
||||||
return;
|
return;
|
||||||
@ -109,12 +128,22 @@ function compile(fileName, dst, evtType) {
|
|||||||
}
|
}
|
||||||
(0, child_process_1.exec)(`lessc ${finalSrcPath} ${(finalDstPath === null || finalDstPath === void 0 ? void 0 : finalDstPath.match(/\.css$/)) ? finalDstPath : finalDstPath.replace(/\/$/, "") + "/_main.css"}`, (error, stdout, stderr) => {
|
(0, child_process_1.exec)(`lessc ${finalSrcPath} ${(finalDstPath === null || finalDstPath === void 0 ? void 0 : finalDstPath.match(/\.css$/)) ? finalDstPath : finalDstPath.replace(/\/$/, "") + "/_main.css"}`, (error, stdout, stderr) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
console.log("ERROR =>", error.message);
|
console.log("- \x1b[33mWarn:\x1b[0m Compilation didn't run successfully. ERROR =>", error.message);
|
||||||
if (!(evtType === null || evtType === void 0 ? void 0 : evtType.match(/change/i)) && fileName && fileName.match(/\[/)) {
|
if (!(evtType === null || evtType === void 0 ? void 0 : evtType.match(/change/i)) && fileName && fileName.match(/\[/)) {
|
||||||
fs_1.default.unlinkSync(finalDstPath);
|
fs_1.default.unlinkSync(finalDstPath);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log("Less Compilation \x1b[32msuccessful\x1b[0m!");
|
console.log("- \x1b[32mCompiled:\x1b[0m Less Compilation Successful!");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (fs_1.default.existsSync("./lesscw.config.json")) {
|
||||||
|
fs_1.default.watchFile("./lesscw.config.json", (evtType, fileName) => {
|
||||||
|
console.log("- \x1b[34mInfo:\x1b[0m! Restarting process...");
|
||||||
|
const newSrcDistStrings = grabSrcDisStrings();
|
||||||
|
if (newSrcDistStrings.destinationFile && newSrcDistStrings.sourceFile) {
|
||||||
|
process.argv.push("--src", newSrcDistStrings.sourceFile, "--dst", newSrcDistStrings.destinationFile);
|
||||||
|
traverseFiles(newSrcDistStrings.sourceFile, newSrcDistStrings.destinationFile);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "lessc-watcher",
|
"name": "lessc-watcher",
|
||||||
"version": "1.1.5",
|
"version": "1.1.6",
|
||||||
"description": "A minimal package to watch less files and compile them to css",
|
"description": "A minimal package to watch less files and compile them to css",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"bin": {
|
"bin": {
|
||||||
|
88
src/index.ts
88
src/index.ts
@ -1,9 +1,14 @@
|
|||||||
#! /usr/bin/env node
|
#! /usr/bin/env node
|
||||||
|
|
||||||
import fs from "fs";
|
import fs, { watch } from "fs";
|
||||||
import { exec } from "child_process";
|
import { exec, spawn } from "child_process";
|
||||||
import { LessCssWatcherConfigObject } from "./index.d";
|
import { LessCssWatcherConfigObject } from "./index.d";
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
const grabSrcDisStrings = () => {
|
||||||
if (fs.existsSync("./lesscw.config.json")) {
|
if (fs.existsSync("./lesscw.config.json")) {
|
||||||
if (process.argv.indexOf("--src") >= 0 || process.argv.indexOf("--dst") >= 0) {
|
if (process.argv.indexOf("--src") >= 0 || process.argv.indexOf("--dst") >= 0) {
|
||||||
try {
|
try {
|
||||||
@ -40,40 +45,51 @@ if (fs.existsSync("./lesscw.config.json")) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.log("ERROR in your config file =>", error.message);
|
console.log("- \x1b[31mERROR:\x1b[0m Your config file has some errors. ERROR =>", error.message);
|
||||||
process.exit();
|
process.exit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
const sourceFile = process.argv.indexOf("--src") >= 0 ? process.argv[process.argv.indexOf("--src") + 1] : null;
|
const sourceFile = process.argv.indexOf("--src") >= 0 ? process.argv[process.argv.indexOf("--src") + 1] : null;
|
||||||
const destinationFile = process.argv.indexOf("--dst") >= 0 ? process.argv[process.argv.indexOf("--dst") + 1] : null;
|
const destinationFile = process.argv.indexOf("--dst") >= 0 ? process.argv[process.argv.indexOf("--dst") + 1] : null;
|
||||||
|
|
||||||
/** ****************************************************************************** */
|
return { sourceFile, destinationFile };
|
||||||
/** ****************************************************************************** */
|
};
|
||||||
/** ****************************************************************************** */
|
|
||||||
/** ****************************************************************************** */
|
|
||||||
/** ****************************************************************************** */
|
|
||||||
/** ****************************************************************************** */
|
|
||||||
|
|
||||||
console.log("\x1b[44mRunning Less compiler\x1b[0m ...");
|
const { sourceFile, destinationFile } = grabSrcDisStrings();
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
console.log("- \x1b[35mStart:\x1b[0m Running Less compiler ...");
|
||||||
|
|
||||||
if (!sourceFile || !destinationFile) {
|
if (!sourceFile || !destinationFile) {
|
||||||
console.log("\x1b[33mERROR:\x1b[0m => Missing source or destination file");
|
console.log("- \x1b[31mERROR:\x1b[0m => Missing source or destination file");
|
||||||
process.exit();
|
process.exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
const sourceFiles = sourceFile.split(",");
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
const dstFiles = destinationFile.split(",");
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loop through source files and destination files and run the compile function
|
* Loop through source files and destination files and run the compile function
|
||||||
*/
|
*/
|
||||||
|
function traverseFiles(src: string, dst: string) {
|
||||||
|
const sourceFiles = src.split(",");
|
||||||
|
const dstFiles = dst.split(",");
|
||||||
|
|
||||||
for (let i = 0; i < sourceFiles.length; i++) {
|
for (let i = 0; i < sourceFiles.length; i++) {
|
||||||
const srcFolder = sourceFiles[i];
|
const srcFolder = sourceFiles[i];
|
||||||
const dstFile = dstFiles[i];
|
const dstFile = dstFiles[i];
|
||||||
|
|
||||||
if (srcFolder?.match(/\.[^\/]+$/) && !srcFolder?.match(/\.less$/)) {
|
if (srcFolder?.match(/\.[^\/]+$/) && !srcFolder?.match(/\.less$/)) {
|
||||||
console.log("\x1b[33mERROR:\x1b[0m Source must be a folder or a .less file");
|
console.log("- \x1b[31mERROR:\x1b[0m Source must be a folder or a .less file");
|
||||||
process.exit();
|
process.exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,14 +117,32 @@ for (let i = 0; i < sourceFiles.length; i++) {
|
|||||||
|
|
||||||
compile(srcFolder, dstFile, null);
|
compile(srcFolder, dstFile, null);
|
||||||
|
|
||||||
fs.watch(srcFolder, { recursive: true }, (evtType, fileName) => {
|
const watcher = fs.watch(srcFolder, { recursive: true }, (evtType, fileName) => {
|
||||||
if (!fileName) return;
|
if (!fileName) return;
|
||||||
|
|
||||||
const filePathRoot = srcFolder?.match(/\.less$/) ? srcFolder : srcFolder + "/" + fileName;
|
const filePathRoot = srcFolder?.match(/\.less$/) ? srcFolder : srcFolder + "/" + fileName;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const currentProcessArgsSrc = process.argv[process.argv.indexOf("--src") + 1];
|
||||||
|
const activeSourceFiles = currentProcessArgsSrc.split(",");
|
||||||
|
|
||||||
|
if (activeSourceFiles.includes(srcFolder)) {
|
||||||
compile(filePathRoot, dstFile, evtType);
|
compile(filePathRoot, dstFile, evtType);
|
||||||
|
} else {
|
||||||
|
watcher.close();
|
||||||
|
}
|
||||||
|
} catch (error: any) {
|
||||||
|
console.log("- \x1b[31mERROR:\x1b[0m Please check your config file =>", error.message);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
traverseFiles(sourceFile, destinationFile);
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compile less file to css function
|
* Compile less file to css function
|
||||||
@ -141,7 +175,7 @@ function compile(fileName: string, dst: string, evtType: string | null) {
|
|||||||
exec(`lessc ${finalSrcPath} ${finalDstPath?.match(/\.css$/) ? finalDstPath : finalDstPath.replace(/\/$/, "") + "/_main.css"}`, (error, stdout, stderr) => {
|
exec(`lessc ${finalSrcPath} ${finalDstPath?.match(/\.css$/) ? finalDstPath : finalDstPath.replace(/\/$/, "") + "/_main.css"}`, (error, stdout, stderr) => {
|
||||||
/** @type {Error} */
|
/** @type {Error} */
|
||||||
if (error) {
|
if (error) {
|
||||||
console.log("ERROR =>", error.message);
|
console.log("- \x1b[33mWarn:\x1b[0m Compilation didn't run successfully. ERROR =>", error.message);
|
||||||
|
|
||||||
if (!evtType?.match(/change/i) && fileName && fileName.match(/\[/)) {
|
if (!evtType?.match(/change/i) && fileName && fileName.match(/\[/)) {
|
||||||
fs.unlinkSync(finalDstPath);
|
fs.unlinkSync(finalDstPath);
|
||||||
@ -150,6 +184,26 @@ function compile(fileName: string, dst: string, evtType: string | null) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("Less Compilation \x1b[32msuccessful\x1b[0m!");
|
console.log("- \x1b[32mCompiled:\x1b[0m Less Compilation Successful!");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
/**
|
||||||
|
* watch for changes to the config file
|
||||||
|
*/
|
||||||
|
if (fs.existsSync("./lesscw.config.json")) {
|
||||||
|
fs.watchFile("./lesscw.config.json", (evtType, fileName) => {
|
||||||
|
console.log("- \x1b[34mInfo:\x1b[0m! Restarting process...");
|
||||||
|
|
||||||
|
const newSrcDistStrings = grabSrcDisStrings();
|
||||||
|
|
||||||
|
if (newSrcDistStrings.destinationFile && newSrcDistStrings.sourceFile) {
|
||||||
|
process.argv.push("--src", newSrcDistStrings.sourceFile, "--dst", newSrcDistStrings.destinationFile);
|
||||||
|
traverseFiles(newSrcDistStrings.sourceFile, newSrcDistStrings.destinationFile);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user