This commit is contained in:
Tben 2023-07-30 12:38:44 +01:00
parent f256310387
commit e3498601a1
3 changed files with 169 additions and 83 deletions

103
dist/index.js vendored
View File

@ -7,38 +7,37 @@ Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const child_process_1 = require("child_process");
const grabSrcDisStrings = () => {
let srcArray = [];
let dstArray = [];
if (fs_1.default.existsSync("./lesscw.config.json")) {
if (process.argv.indexOf("--src") >= 0 || process.argv.indexOf("--dst") >= 0) {
if (process.argv.indexOf("--src")) {
try {
process.argv.splice(process.argv.indexOf("--src"));
process.argv.splice(process.argv.indexOf("--dst"));
}
catch (error) { }
}
try {
const configObject = JSON.parse(fs_1.default.readFileSync("./lesscw.config.json", "utf-8"));
if ((configObject === null || configObject === void 0 ? void 0 : configObject.src) && (configObject === null || configObject === void 0 ? void 0 : configObject.dst) && typeof configObject.src === "string" && typeof configObject.dst === "string") {
process.argv.push("--src", configObject.src);
process.argv.push("--dst", configObject.dst);
srcArray = configObject.src.split(",");
dstArray = configObject.dst.split(",");
}
else if ((configObject === null || configObject === void 0 ? void 0 : configObject.src) && (configObject === null || configObject === void 0 ? void 0 : configObject.dst) && typeof configObject.src === "object" && typeof configObject.dst === "object" && Array.isArray(configObject.src) && Array.isArray(configObject.dst)) {
process.argv.push("--src", configObject.src.join(","));
process.argv.push("--dst", configObject.dst.join(","));
srcArray = configObject.src;
dstArray = configObject.dst;
}
else if ((configObject === null || configObject === void 0 ? void 0 : configObject.srcDst) && Array.isArray(configObject.srcDst) && configObject.srcDst.length > 0) {
const srcDstArray = configObject.srcDst;
let srcArray = [];
let dstArray = [];
srcDstArray.forEach((item) => {
if ((item === null || item === void 0 ? void 0 : item.src) && (item === null || item === void 0 ? void 0 : item.dst) && typeof item.src === "string" && typeof item.dst === "string") {
srcArray.push(item.src);
dstArray.push(item.dst);
}
});
if (srcArray.length && dstArray.length) {
process.argv.push("--src", srcArray.join(","));
process.argv.push("--dst", dstArray.join(","));
}
else {
console.log("- \x1b[31mERROR:\x1b[0m Your config file has some errors. Please check your config file");
process.exit();
}
}
catch (error) {
@ -46,11 +45,28 @@ const grabSrcDisStrings = () => {
process.exit();
}
}
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;
return { sourceFile, destinationFile };
else {
if (process.argv.indexOf("--src") >= 0 && process.argv.indexOf("--dst") >= 0) {
try {
srcArray = process.argv[process.argv.indexOf("--src") + 1].split(",");
dstArray = process.argv[process.argv.indexOf("--dst") + 1].split(",");
}
catch (error) { }
}
else {
console.log("- \x1b[31mERROR:\x1b[0m Missing source or destination file");
process.exit();
}
}
return {
sourceFile: srcArray.join(","),
destinationFile: dstArray.join(","),
};
};
const { sourceFile, destinationFile } = grabSrcDisStrings();
if (sourceFile && destinationFile) {
process.argv.push("--src", sourceFile, "--dst", destinationFile);
}
console.log("- \x1b[35mStart:\x1b[0m Running Less compiler ...");
if (!sourceFile || !destinationFile) {
console.log("- \x1b[31mERROR:\x1b[0m => Missing source or destination file");
@ -62,7 +78,7 @@ function traverseFiles(src, dst) {
for (let i = 0; i < sourceFiles.length; i++) {
const srcFolder = sourceFiles[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[31mERROR:\x1b[0m Source must be a folder or a .less file");
process.exit();
}
@ -77,31 +93,27 @@ function traverseFiles(src, dst) {
}
}
else if (fs_1.default.existsSync(srcFolder) && fs_1.default.existsSync((srcFolder + "/main.less").replace(/\/\//g, ""))) {
fs_1.default.writeFileSync((srcFolder + "/main.less").replace(/\/\//g, ""), "", "utf-8");
}
if (!fs_1.default.existsSync(dstFile)) {
if (dstFile === null || dstFile === void 0 ? void 0 : dstFile.match(/\.css$/)) {
fs_1.default.mkdirSync(dstFile.replace(/\/[^\/]+\.css$/, ""), { recursive: true });
fs_1.default.writeFileSync(dstFile, "/* Your compiled CSS from your less file(s) goes here */", "utf-8");
}
else {
fs_1.default.mkdirSync(dstFile.replace(/\/[^\/]+\.[^\/]+$/, ""), { recursive: true });
fs_1.default.writeFileSync((dstFile + "/_main.css").replace(/\/\//g, ""), "/* Your compiled CSS from your less file(s) goes here */", "utf-8");
}
}
compile(srcFolder, dstFile, null);
const watcher = fs_1.default.watch(srcFolder, { recursive: true }, (evtType, fileName) => {
if (!fileName)
return;
const filePathRoot = (srcFolder === null || srcFolder === void 0 ? void 0 : srcFolder.match(/\.less$/)) ? srcFolder : srcFolder + "/" + fileName;
if (srcFolder === null || srcFolder === void 0 ? void 0 : srcFolder.match(/\.less$/)) {
fs_1.default.watchFile(srcFolder, { interval: 500 }, (current, previous) => {
const dstFilePathRoot = (dstFile === null || dstFile === void 0 ? void 0 : dstFile.match(/\.css$/)) ? dstFile : dstFile + "/" + "_main.css";
try {
const currentProcessArgsSrc = process.argv[process.argv.indexOf("--src") + 1];
const activeSourceFiles = currentProcessArgsSrc.split(",");
if (activeSourceFiles.includes(srcFolder)) {
compile(filePathRoot, dstFile, evtType);
compile(srcFolder, dstFilePathRoot, null);
}
else {
watcher.close();
fs_1.default.unwatchFile(srcFolder);
}
}
catch (error) {
@ -109,14 +121,46 @@ function traverseFiles(src, dst) {
}
});
}
else if (!(srcFolder === null || srcFolder === void 0 ? void 0 : srcFolder.match(/\.[^\/]+$/))) {
fs_1.default.watch(srcFolder, { recursive: true }, (evtType, fileName) => {
if (!(evtType === null || evtType === void 0 ? void 0 : evtType.match(/change/i))) {
return;
}
if (!fileName)
return;
const srcFilePathRoot = srcFolder + "/main.less";
try {
const currentProcessArgsSrc = process.argv[process.argv.indexOf("--src") + 1];
const activeSourceFiles = currentProcessArgsSrc.split(",");
if (activeSourceFiles.includes(srcFilePathRoot)) {
return;
}
else {
compile(srcFilePathRoot, dstFile, evtType);
}
}
catch (error) {
console.log("- \x1b[31mERROR:\x1b[0m Please check your config file =>", error.message);
}
});
}
else {
console.log("- \x1b[31mERROR:\x1b[0m Source must be a folder or a .less file");
process.exit();
}
}
}
traverseFiles(sourceFile, destinationFile);
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(/\.[\/]$/) && !(fileName === null || fileName === void 0 ? void 0 : fileName.match(/\.less$/i)))) {
return;
}
let finalSrcPath = (fileName === null || fileName === void 0 ? void 0 : fileName.match(/\.less$/)) ? fileName : `${fileName}/main.less`;
let finalDstPath = dst;
const distFolder = (dst === null || dst === void 0 ? void 0 : dst.match(/\.css$/)) ? null : dst === null || dst === void 0 ? void 0 : dst.replace(/\/+$/, "");
let finalDstPath = distFolder ? `${distFolder}/_main.css` : dst;
if (distFolder && !fs_1.default.existsSync(distFolder)) {
fs_1.default.mkdirSync(distFolder, { recursive: true });
}
if (fileName === null || fileName === void 0 ? void 0 : fileName.match(/\[/)) {
const paths = fileName.split("/");
const targetPathFull = paths[paths.length - 1];
@ -126,7 +170,8 @@ function compile(fileName, dst, evtType) {
finalSrcPath = `${fileName}/${targetPathFull}`;
finalDstPath = targetDstFilePath;
}
(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) => {
const executionCmd = `lessc ${finalSrcPath} ${finalDstPath}`;
(0, child_process_1.exec)(executionCmd, (error, stdout, stderr) => {
if (error) {
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(/\[/)) {
@ -138,8 +183,8 @@ function compile(fileName, dst, evtType) {
});
}
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...");
fs_1.default.watchFile("./lesscw.config.json", { interval: 500 }, (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);

View File

@ -1,6 +1,6 @@
{
"name": "lessc-watcher",
"version": "1.1.6",
"version": "1.1.7",
"description": "A minimal package to watch less files and compile them to css",
"main": "dist/index.js",
"bin": {

View File

@ -9,11 +9,13 @@ import { LessCssWatcherConfigObject } from "./index.d";
//////////////////////////////////////////////////////////////////////////////
const grabSrcDisStrings = () => {
let srcArray: string[] = [];
let dstArray: string[] = [];
if (fs.existsSync("./lesscw.config.json")) {
if (process.argv.indexOf("--src") >= 0 || process.argv.indexOf("--dst") >= 0) {
if (process.argv.indexOf("--src")) {
try {
process.argv.splice(process.argv.indexOf("--src"));
process.argv.splice(process.argv.indexOf("--dst"));
} catch (error) {}
}
@ -21,47 +23,56 @@ const grabSrcDisStrings = () => {
const configObject: LessCssWatcherConfigObject = JSON.parse(fs.readFileSync("./lesscw.config.json", "utf-8"));
if (configObject?.src && configObject?.dst && typeof configObject.src === "string" && typeof configObject.dst === "string") {
process.argv.push("--src", configObject.src);
process.argv.push("--dst", configObject.dst);
srcArray = configObject.src.split(",");
dstArray = configObject.dst.split(",");
} else if (configObject?.src && configObject?.dst && typeof configObject.src === "object" && typeof configObject.dst === "object" && Array.isArray(configObject.src) && Array.isArray(configObject.dst)) {
process.argv.push("--src", configObject.src.join(","));
process.argv.push("--dst", configObject.dst.join(","));
srcArray = configObject.src;
dstArray = configObject.dst;
} else if (configObject?.srcDst && Array.isArray(configObject.srcDst) && configObject.srcDst.length > 0) {
const srcDstArray = configObject.srcDst;
let srcArray: string[] = [];
let dstArray: string[] = [];
srcDstArray.forEach((item) => {
if (item?.src && item?.dst && typeof item.src === "string" && typeof item.dst === "string") {
srcArray.push(item.src);
dstArray.push(item.dst);
}
});
if (srcArray.length && dstArray.length) {
process.argv.push("--src", srcArray.join(","));
process.argv.push("--dst", dstArray.join(","));
}
} else {
console.log("- \x1b[31mERROR:\x1b[0m Your config file has some errors. Please check your config file");
process.exit();
}
} catch (error: any) {
console.log("- \x1b[31mERROR:\x1b[0m Your config file has some errors. ERROR =>", error.message);
process.exit();
}
} else {
if (process.argv.indexOf("--src") >= 0 && process.argv.indexOf("--dst") >= 0) {
try {
srcArray = process.argv[process.argv.indexOf("--src") + 1].split(",");
dstArray = process.argv[process.argv.indexOf("--dst") + 1].split(",");
} catch (error) {}
} else {
console.log("- \x1b[31mERROR:\x1b[0m Missing source or destination file");
process.exit();
}
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
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;
return { sourceFile, destinationFile };
return {
sourceFile: srcArray.join(","),
destinationFile: dstArray.join(","),
};
};
const { sourceFile, destinationFile } = grabSrcDisStrings();
if (sourceFile && destinationFile) {
process.argv.push("--src", sourceFile, "--dst", destinationFile);
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
@ -88,7 +99,7 @@ function traverseFiles(src: string, dst: string) {
const srcFolder = sourceFiles[i];
const dstFile = dstFiles[i];
if (srcFolder?.match(/\.[^\/]+$/) && !srcFolder?.match(/\.less$/)) {
if (srcFolder?.match(/\/[^\/]+\.[^\/]+$/) && !srcFolder?.match(/\.less$/)) {
console.log("- \x1b[31mERROR:\x1b[0m Source must be a folder or a .less file");
process.exit();
}
@ -102,39 +113,62 @@ function traverseFiles(src: string, dst: string) {
fs.writeFileSync((srcFolder + "/main.less").replace(/\/\//g, ""), "", "utf-8");
}
} else if (fs.existsSync(srcFolder) && fs.existsSync((srcFolder + "/main.less").replace(/\/\//g, ""))) {
fs.writeFileSync((srcFolder + "/main.less").replace(/\/\//g, ""), "", "utf-8");
}
if (!fs.existsSync(dstFile)) {
if (dstFile?.match(/\.css$/)) {
fs.mkdirSync(dstFile.replace(/\/[^\/]+\.css$/, ""), { recursive: true });
fs.writeFileSync(dstFile, "/* Your compiled CSS from your less file(s) goes here */", "utf-8");
} else {
fs.mkdirSync(dstFile.replace(/\/[^\/]+\.[^\/]+$/, ""), { recursive: true });
fs.writeFileSync((dstFile + "/_main.css").replace(/\/\//g, ""), "/* Your compiled CSS from your less file(s) goes here */", "utf-8");
}
}
compile(srcFolder, dstFile, null);
const watcher = fs.watch(srcFolder, { recursive: true }, (evtType, fileName) => {
if (!fileName) return;
const filePathRoot = srcFolder?.match(/\.less$/) ? srcFolder : srcFolder + "/" + fileName;
if (srcFolder?.match(/\.less$/)) {
fs.watchFile(srcFolder, { interval: 500 }, (current, previous) => {
const dstFilePathRoot = dstFile?.match(/\.css$/) ? dstFile : dstFile + "/" + "_main.css";
try {
const currentProcessArgsSrc = process.argv[process.argv.indexOf("--src") + 1];
const activeSourceFiles = currentProcessArgsSrc.split(",");
if (activeSourceFiles.includes(srcFolder)) {
compile(filePathRoot, dstFile, evtType);
compile(srcFolder, dstFilePathRoot, null);
} else {
watcher.close();
fs.unwatchFile(srcFolder);
}
} catch (error: any) {
console.log("- \x1b[31mERROR:\x1b[0m Please check your config file =>", error.message);
}
});
} else if (!srcFolder?.match(/\.[^\/]+$/)) {
fs.watch(srcFolder, { recursive: true }, (evtType, fileName) => {
if (!evtType?.match(/change/i)) {
return;
}
if (!fileName) return;
const srcFilePathRoot = srcFolder + "/main.less";
try {
const currentProcessArgsSrc = process.argv[process.argv.indexOf("--src") + 1];
const activeSourceFiles = currentProcessArgsSrc.split(",");
if (activeSourceFiles.includes(srcFilePathRoot)) {
return;
} else {
compile(srcFilePathRoot, dstFile, evtType);
}
} catch (error: any) {
console.log("- \x1b[31mERROR:\x1b[0m Please check your config file =>", error.message);
}
});
} else {
console.log("- \x1b[31mERROR:\x1b[0m Source must be a folder or a .less file");
process.exit();
}
}
}
@ -152,12 +186,17 @@ traverseFiles(sourceFile, destinationFile);
* @returns
*/
function compile(fileName: string, dst: string, evtType: string | null) {
if (fileName?.match(/\(/) || (fileName.match(/\..{2,4}$/) && !fileName?.match(/\.less$/i))) {
if (fileName?.match(/\(/) || (fileName.match(/\.[\/]$/) && !fileName?.match(/\.less$/i))) {
return;
}
let finalSrcPath = fileName?.match(/\.less$/) ? fileName : `${fileName}/main.less`;
let finalDstPath = dst;
const distFolder = dst?.match(/\.css$/) ? null : dst?.replace(/\/+$/, "");
let finalDstPath = distFolder ? `${distFolder}/_main.css` : dst;
if (distFolder && !fs.existsSync(distFolder)) {
fs.mkdirSync(distFolder, { recursive: true });
}
if (fileName?.match(/\[/)) {
const paths = fileName.split("/");
@ -172,7 +211,9 @@ function compile(fileName: string, dst: string, evtType: string | null) {
finalDstPath = targetDstFilePath;
}
exec(`lessc ${finalSrcPath} ${finalDstPath?.match(/\.css$/) ? finalDstPath : finalDstPath.replace(/\/$/, "") + "/_main.css"}`, (error, stdout, stderr) => {
const executionCmd = `lessc ${finalSrcPath} ${finalDstPath}`;
exec(executionCmd, (error, stdout, stderr) => {
/** @type {Error} */
if (error) {
console.log("- \x1b[33mWarn:\x1b[0m Compilation didn't run successfully. ERROR =>", error.message);
@ -196,8 +237,8 @@ function compile(fileName: string, dst: string, evtType: string | null) {
* 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...");
fs.watchFile("./lesscw.config.json", { interval: 500 }, (evtType, fileName) => {
console.log("- \x1b[34mInfo:\x1b[0m Restarting process...");
const newSrcDistStrings = grabSrcDisStrings();