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 fs_1 = __importDefault(require("fs"));
const child_process_1 = require("child_process"); const child_process_1 = require("child_process");
const grabSrcDisStrings = () => { const grabSrcDisStrings = () => {
let srcArray = [];
let dstArray = [];
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")) {
try { try {
process.argv.splice(process.argv.indexOf("--src")); process.argv.splice(process.argv.indexOf("--src"));
process.argv.splice(process.argv.indexOf("--dst"));
} }
catch (error) { } catch (error) { }
} }
try { try {
const configObject = JSON.parse(fs_1.default.readFileSync("./lesscw.config.json", "utf-8")); 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") { 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); srcArray = configObject.src.split(",");
process.argv.push("--dst", configObject.dst); 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)) { 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(",")); srcArray = configObject.src;
process.argv.push("--dst", configObject.dst.join(",")); dstArray = configObject.dst;
} }
else if ((configObject === null || configObject === void 0 ? void 0 : configObject.srcDst) && Array.isArray(configObject.srcDst) && configObject.srcDst.length > 0) { else if ((configObject === null || configObject === void 0 ? void 0 : configObject.srcDst) && Array.isArray(configObject.srcDst) && configObject.srcDst.length > 0) {
const srcDstArray = configObject.srcDst; const srcDstArray = configObject.srcDst;
let srcArray = [];
let dstArray = [];
srcDstArray.forEach((item) => { 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") { 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); srcArray.push(item.src);
dstArray.push(item.dst); 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) { catch (error) {
@ -46,11 +45,28 @@ const grabSrcDisStrings = () => {
process.exit(); process.exit();
} }
} }
const sourceFile = process.argv.indexOf("--src") >= 0 ? process.argv[process.argv.indexOf("--src") + 1] : null; else {
const destinationFile = process.argv.indexOf("--dst") >= 0 ? process.argv[process.argv.indexOf("--dst") + 1] : null; if (process.argv.indexOf("--src") >= 0 && process.argv.indexOf("--dst") >= 0) {
return { sourceFile, destinationFile }; 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(); const { sourceFile, destinationFile } = grabSrcDisStrings();
if (sourceFile && destinationFile) {
process.argv.push("--src", sourceFile, "--dst", destinationFile);
}
console.log("- \x1b[35mStart:\x1b[0m Running Less compiler ..."); console.log("- \x1b[35mStart:\x1b[0m Running Less compiler ...");
if (!sourceFile || !destinationFile) { if (!sourceFile || !destinationFile) {
console.log("- \x1b[31mERROR:\x1b[0m => Missing source or destination file"); 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++) { 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[31mERROR:\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();
} }
@ -77,31 +93,27 @@ function traverseFiles(src, dst) {
} }
} }
else if (fs_1.default.existsSync(srcFolder) && fs_1.default.existsSync((srcFolder + "/main.less").replace(/\/\//g, ""))) { 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 (!fs_1.default.existsSync(dstFile)) {
if (dstFile === null || dstFile === void 0 ? void 0 : dstFile.match(/\.css$/)) { if (dstFile === null || dstFile === void 0 ? void 0 : dstFile.match(/\.css$/)) {
fs_1.default.mkdirSync(dstFile.replace(/\/[^\/]+\.css$/, ""), { recursive: true }); 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 { else {
fs_1.default.mkdirSync(dstFile.replace(/\/[^\/]+\.[^\/]+$/, ""), { recursive: true }); 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); compile(srcFolder, dstFile, null);
const watcher = fs_1.default.watch(srcFolder, { recursive: true }, (evtType, fileName) => { if (srcFolder === null || srcFolder === void 0 ? void 0 : srcFolder.match(/\.less$/)) {
if (!fileName) fs_1.default.watchFile(srcFolder, { interval: 500 }, (current, previous) => {
return; const dstFilePathRoot = (dstFile === null || dstFile === void 0 ? void 0 : dstFile.match(/\.css$/)) ? dstFile : dstFile + "/" + "_main.css";
const filePathRoot = (srcFolder === null || srcFolder === void 0 ? void 0 : srcFolder.match(/\.less$/)) ? srcFolder : srcFolder + "/" + fileName;
try { try {
const currentProcessArgsSrc = process.argv[process.argv.indexOf("--src") + 1]; const currentProcessArgsSrc = process.argv[process.argv.indexOf("--src") + 1];
const activeSourceFiles = currentProcessArgsSrc.split(","); const activeSourceFiles = currentProcessArgsSrc.split(",");
if (activeSourceFiles.includes(srcFolder)) { if (activeSourceFiles.includes(srcFolder)) {
compile(filePathRoot, dstFile, evtType); compile(srcFolder, dstFilePathRoot, null);
} }
else { else {
watcher.close(); fs_1.default.unwatchFile(srcFolder);
} }
} }
catch (error) { 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); 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(/\.[\/]$/) && !(fileName === null || fileName === void 0 ? void 0 : fileName.match(/\.less$/i)))) {
return; return;
} }
let finalSrcPath = (fileName === null || fileName === void 0 ? void 0 : fileName.match(/\.less$/)) ? fileName : `${fileName}/main.less`; 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(/\[/)) { if (fileName === null || fileName === void 0 ? void 0 : fileName.match(/\[/)) {
const paths = fileName.split("/"); const paths = fileName.split("/");
const targetPathFull = paths[paths.length - 1]; const targetPathFull = paths[paths.length - 1];
@ -126,7 +170,8 @@ function compile(fileName, dst, evtType) {
finalSrcPath = `${fileName}/${targetPathFull}`; finalSrcPath = `${fileName}/${targetPathFull}`;
finalDstPath = targetDstFilePath; 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) { if (error) {
console.log("- \x1b[33mWarn:\x1b[0m Compilation didn't run successfully. 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(/\[/)) {
@ -138,8 +183,8 @@ function compile(fileName, dst, evtType) {
}); });
} }
if (fs_1.default.existsSync("./lesscw.config.json")) { if (fs_1.default.existsSync("./lesscw.config.json")) {
fs_1.default.watchFile("./lesscw.config.json", (evtType, fileName) => { fs_1.default.watchFile("./lesscw.config.json", { interval: 500 }, (evtType, fileName) => {
console.log("- \x1b[34mInfo:\x1b[0m! Restarting process..."); console.log("- \x1b[34mInfo:\x1b[0m Restarting process...");
const newSrcDistStrings = grabSrcDisStrings(); const newSrcDistStrings = grabSrcDisStrings();
if (newSrcDistStrings.destinationFile && newSrcDistStrings.sourceFile) { if (newSrcDistStrings.destinationFile && newSrcDistStrings.sourceFile) {
process.argv.push("--src", newSrcDistStrings.sourceFile, "--dst", newSrcDistStrings.destinationFile); process.argv.push("--src", newSrcDistStrings.sourceFile, "--dst", newSrcDistStrings.destinationFile);

View File

@ -1,6 +1,6 @@
{ {
"name": "lessc-watcher", "name": "lessc-watcher",
"version": "1.1.6", "version": "1.1.7",
"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": {

View File

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