This commit is contained in:
Benoti 2023-09-01 12:32:35 +01:00
parent 5dc871d8db
commit 59d73239d8
7 changed files with 577 additions and 421 deletions

84
dist/index.js vendored
View File

@ -18,18 +18,31 @@ const grabSrcDisStrings = () => {
}
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") {
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") {
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)) {
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)) {
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) {
else if ((configObject === null || configObject === void 0 ? void 0 : configObject.srcDst) &&
Array.isArray(configObject.srcDst) &&
configObject.srcDst.length > 0) {
const srcDstArray = configObject.srcDst;
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);
dstArray.push(item.dst);
}
@ -46,10 +59,13 @@ const grabSrcDisStrings = () => {
}
}
else {
if (process.argv.indexOf("--src") >= 0 && process.argv.indexOf("--dst") >= 0) {
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(",");
srcArray =
process.argv[process.argv.indexOf("--src") + 1].split(",");
dstArray =
process.argv[process.argv.indexOf("--dst") + 1].split(",");
}
catch (error) { }
}
@ -73,33 +89,44 @@ if (!sourceFile || !destinationFile) {
process.exit();
}
function traverseFiles(src, dst) {
var _a;
const sourceFiles = src.split(",");
const dstFiles = dst.split(",");
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();
}
if (!fs_1.default.existsSync(srcFolder)) {
if (srcFolder === null || srcFolder === void 0 ? void 0 : srcFolder.match(/\.less$/)) {
fs_1.default.mkdirSync(srcFolder.replace(/\/[^\/]+\.less$/, ""), { recursive: true });
fs_1.default.mkdirSync(srcFolder.replace(/\/[^\/]+\.less$/, ""), {
recursive: true,
});
fs_1.default.writeFileSync(srcFolder, "", "utf-8");
}
else {
fs_1.default.mkdirSync(srcFolder.replace(/\/[^\/]+\.[^\/]+$/, ""), { recursive: true });
fs_1.default.mkdirSync(srcFolder.replace(/\/[^\/]+\.[^\/]+$/, ""), {
recursive: true,
});
fs_1.default.writeFileSync((srcFolder + "/main.less").replace(/\/\//g, ""), "", "utf-8");
}
}
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, ""))) {
}
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.mkdirSync(dstFile.replace(/\/[^\/]+\.css$/, ""), {
recursive: true,
});
}
else {
fs_1.default.mkdirSync(dstFile.replace(/\/[^\/]+\.[^\/]+$/, ""), { recursive: true });
fs_1.default.mkdirSync(dstFile.replace(/\/[^\/]+\.[^\/]+$/, ""), {
recursive: true,
});
}
}
compile(srcFolder, dstFile, null);
@ -113,7 +140,9 @@ function traverseFiles(src, dst) {
catch (error) { }
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";
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(",");
@ -130,7 +159,11 @@ function traverseFiles(src, dst) {
});
}
else if (!(srcFolder === null || srcFolder === void 0 ? void 0 : srcFolder.match(/\.[^\/]+$/))) {
fs_1.default.watch(srcFolder, { recursive: true }, (evtType, fileName) => {
fs_1.default.watch(srcFolder, {
recursive: ((_a = process.platform) === null || _a === void 0 ? void 0 : _a.match(/win/i))
? true
: undefined,
}, (evtType, fileName) => {
if (!(evtType === null || evtType === void 0 ? void 0 : evtType.match(/change/i))) {
return;
}
@ -143,7 +176,8 @@ function traverseFiles(src, dst) {
if (fileName === null || fileName === void 0 ? void 0 : fileName.match(/^\[/)) {
compile(srcFolder + "/" + fileName, dstFile, evtType);
}
else if ((fileName === null || fileName === void 0 ? void 0 : fileName.match(/^\(/)) || activeSourceFiles.includes(srcFilePathRoot)) {
else if ((fileName === null || fileName === void 0 ? void 0 : fileName.match(/^\(/)) ||
activeSourceFiles.includes(srcFilePathRoot)) {
return;
}
else {
@ -163,10 +197,13 @@ function traverseFiles(src, dst) {
}
traverseFiles(sourceFile, destinationFile);
function compile(fileName, dst, evtType) {
if ((fileName === null || fileName === void 0 ? void 0 : fileName.match(/\(/)) || (fileName.match(/\.[\/]$/) && !(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 finalSrcPath = (fileName === null || fileName === void 0 ? void 0 : fileName.match(/\.less$/))
? fileName
: `${fileName}/main.less`;
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)) {
@ -175,7 +212,9 @@ function compile(fileName, dst, evtType) {
if (fileName === null || fileName === void 0 ? void 0 : fileName.match(/\[/)) {
const paths = fileName.split("/");
const targetPathFull = paths[paths.length - 1];
const targetPath = targetPathFull.replace(/\[|\]/g, "").replace(/\.less/, "");
const targetPath = targetPathFull
.replace(/\[|\]/g, "")
.replace(/\.less/, "");
const destinationFileParentFolder = dst.replace(/\/[^\/]+\.css$/, "");
const targetDstFilePath = `${destinationFileParentFolder}/${targetPath}.css`;
finalSrcPath = fileName;
@ -185,7 +224,9 @@ function compile(fileName, dst, evtType) {
(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(/\[/)) {
if (!(evtType === null || evtType === void 0 ? void 0 : evtType.match(/change/i)) &&
fileName &&
fileName.match(/\[/)) {
fs_1.default.unlinkSync(finalDstPath);
}
return;
@ -197,7 +238,8 @@ if (fs_1.default.existsSync("./lesscw.config.json")) {
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) {
if (newSrcDistStrings.destinationFile &&
newSrcDistStrings.sourceFile) {
process.argv.push("--src", newSrcDistStrings.sourceFile, "--dst", newSrcDistStrings.destinationFile);
traverseFiles(newSrcDistStrings.sourceFile, newSrcDistStrings.destinationFile);
}

View File

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

View File

@ -20,39 +20,74 @@ const grabSrcDisStrings = () => {
}
try {
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"
) {
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)) {
} else if (
configObject?.src &&
configObject?.dst &&
typeof configObject.src === "object" &&
typeof configObject.dst === "object" &&
Array.isArray(configObject.src) &&
Array.isArray(configObject.dst)
) {
srcArray = configObject.src;
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;
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);
dstArray.push(item.dst);
}
});
} else {
console.log("- \x1b[31mERROR:\x1b[0m Your config file has some errors. Please check your config file");
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);
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) {
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(",");
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");
console.log(
"- \x1b[31mERROR:\x1b[0m Missing source or destination file"
);
process.exit();
}
}
@ -80,7 +115,9 @@ if (sourceFile && destinationFile) {
console.log("- \x1b[35mStart:\x1b[0m Running Less compiler ...");
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"
);
process.exit();
}
@ -99,27 +136,47 @@ function traverseFiles(src: string, dst: string) {
const srcFolder = sourceFiles[i];
const dstFile = dstFiles[i];
if (srcFolder?.match(/\/[^\/]+\.[^\/]+$/) && !srcFolder?.match(/\.less$/)) {
console.log("- \x1b[31mERROR:\x1b[0m Source must be a folder or a .less file");
if (
srcFolder?.match(/\/[^\/]+\.[^\/]+$/) &&
!srcFolder?.match(/\.less$/)
) {
console.log(
"- \x1b[31mERROR:\x1b[0m Source must be a folder or a .less file"
);
process.exit();
}
if (!fs.existsSync(srcFolder)) {
if (srcFolder?.match(/\.less$/)) {
fs.mkdirSync(srcFolder.replace(/\/[^\/]+\.less$/, ""), { recursive: true });
fs.mkdirSync(srcFolder.replace(/\/[^\/]+\.less$/, ""), {
recursive: true,
});
fs.writeFileSync(srcFolder, "", "utf-8");
} else {
fs.mkdirSync(srcFolder.replace(/\/[^\/]+\.[^\/]+$/, ""), { recursive: true });
fs.writeFileSync((srcFolder + "/main.less").replace(/\/\//g, ""), "", "utf-8");
fs.mkdirSync(srcFolder.replace(/\/[^\/]+\.[^\/]+$/, ""), {
recursive: true,
});
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, ""))
) {
}
if (!fs.existsSync(dstFile)) {
if (dstFile?.match(/\.css$/)) {
fs.mkdirSync(dstFile.replace(/\/[^\/]+\.css$/, ""), { recursive: true });
fs.mkdirSync(dstFile.replace(/\/[^\/]+\.css$/, ""), {
recursive: true,
});
} else {
fs.mkdirSync(dstFile.replace(/\/[^\/]+\.[^\/]+$/, ""), { recursive: true });
fs.mkdirSync(dstFile.replace(/\/[^\/]+\.[^\/]+$/, ""), {
recursive: true,
});
}
}
@ -135,10 +192,13 @@ function traverseFiles(src: string, dst: string) {
if (srcFolder?.match(/\.less$/)) {
fs.watchFile(srcFolder, { interval: 500 }, (current, previous) => {
const dstFilePathRoot = dstFile?.match(/\.css$/) ? dstFile : dstFile + "/" + "_main.css";
const dstFilePathRoot = dstFile?.match(/\.css$/)
? dstFile
: dstFile + "/" + "_main.css";
try {
const currentProcessArgsSrc = process.argv[process.argv.indexOf("--src") + 1];
const currentProcessArgsSrc =
process.argv[process.argv.indexOf("--src") + 1];
const activeSourceFiles = currentProcessArgsSrc.split(",");
if (activeSourceFiles.includes(srcFolder)) {
@ -147,36 +207,61 @@ function traverseFiles(src: string, dst: string) {
fs.unwatchFile(srcFolder);
}
} 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 (fileName?.match(/^\[/)) {
compile(srcFolder + "/" + fileName, dstFile, evtType);
} else if (fileName?.match(/^\(/) || activeSourceFiles.includes(srcFilePathRoot)) {
fs.watch(
srcFolder,
{
recursive: process.platform?.match(/win/i)
? true
: undefined,
},
(evtType, fileName) => {
if (!evtType?.match(/change/i)) {
return;
} else {
compile(srcFilePathRoot, dstFile, evtType);
}
} catch (error: any) {
console.log("- \x1b[31mERROR:\x1b[0m Please check your config file =>", error.message);
if (!fileName) return;
const srcFilePathRoot = srcFolder + "/main.less";
try {
const currentProcessArgsSrc =
process.argv[process.argv.indexOf("--src") + 1];
const activeSourceFiles =
currentProcessArgsSrc.split(",");
if (fileName?.match(/^\[/)) {
compile(
srcFolder + "/" + fileName,
dstFile,
evtType
);
} else if (
fileName?.match(/^\(/) ||
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");
console.log(
"- \x1b[31mERROR:\x1b[0m Source must be a folder or a .less file"
);
process.exit();
}
}
@ -196,11 +281,16 @@ traverseFiles(sourceFile, destinationFile);
* @returns
*/
function compile(fileName: string, dst: string, evtType: string | null) {
if (fileName?.match(/\(/) || (fileName.match(/\.[\/]$/) && !fileName?.match(/\.less$/i))) {
if (
fileName?.match(/\(/) ||
(fileName.match(/\.[\/]$/) && !fileName?.match(/\.less$/i))
) {
return;
}
let finalSrcPath = fileName?.match(/\.less$/) ? fileName : `${fileName}/main.less`;
let finalSrcPath = fileName?.match(/\.less$/)
? fileName
: `${fileName}/main.less`;
const distFolder = dst?.match(/\.css$/) ? null : dst?.replace(/\/+$/, "");
let finalDstPath = distFolder ? `${distFolder}/_main.css` : dst;
@ -211,7 +301,9 @@ function compile(fileName: string, dst: string, evtType: string | null) {
if (fileName?.match(/\[/)) {
const paths = fileName.split("/");
const targetPathFull = paths[paths.length - 1];
const targetPath = targetPathFull.replace(/\[|\]/g, "").replace(/\.less/, "");
const targetPath = targetPathFull
.replace(/\[|\]/g, "")
.replace(/\.less/, "");
const destinationFileParentFolder = dst.replace(/\/[^\/]+\.css$/, "");
@ -226,9 +318,16 @@ function compile(fileName: string, dst: string, evtType: string | null) {
exec(executionCmd, (error, stdout, stderr) => {
/** @type {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?.match(/change/i) && fileName && fileName.match(/\[/)) {
if (
!evtType?.match(/change/i) &&
fileName &&
fileName.match(/\[/)
) {
fs.unlinkSync(finalDstPath);
}
@ -247,14 +346,29 @@ 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", { interval: 500 }, (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();
const newSrcDistStrings = grabSrcDisStrings();
if (newSrcDistStrings.destinationFile && newSrcDistStrings.sourceFile) {
process.argv.push("--src", newSrcDistStrings.sourceFile, "--dst", newSrcDistStrings.destinationFile);
traverseFiles(newSrcDistStrings.sourceFile, newSrcDistStrings.destinationFile);
if (
newSrcDistStrings.destinationFile &&
newSrcDistStrings.sourceFile
) {
process.argv.push(
"--src",
newSrcDistStrings.sourceFile,
"--dst",
newSrcDistStrings.destinationFile
);
traverseFiles(
newSrcDistStrings.sourceFile,
newSrcDistStrings.destinationFile
);
}
}
});
);
}