Code Upgrade

This commit is contained in:
Tben 2023-07-30 01:20:33 +01:00
parent 9b97b04ad1
commit 4b2d39f9c6
4 changed files with 41 additions and 27 deletions

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
# Ignore npm modules and src folder # Ignore npm modules and src folder
node_modules node_modules
src src
tsconfig.json tsconfig.json
test

View File

@ -42,6 +42,8 @@ Or you can just watch an entire folder. In this case less-watch will searc for a
npx lessc-watcher --src ./folder --dst ./dist/less.css npx lessc-watcher --src ./folder --dst ./dist/less.css
``` ```
**_NOTE:_** If you only provide a destination path, without specifying the exact file name in `.css`, your files will be compiled to a css file named `_main.css`. This is done to prevent conflicts with another possible `main.css` file.
#### Adding more source folders #### Adding more source folders
You can add more source folders by using a comma(`,`) separator. Example: You can add more source folders by using a comma(`,`) separator. Example:

61
dist/index.js vendored
View File

@ -8,40 +8,51 @@ const fs_1 = __importDefault(require("fs"));
const child_process_1 = require("child_process"); const child_process_1 = require("child_process");
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("Running Less compiler ..."); console.log("\x1b[44mRunning Less compiler\x1b[0m ...");
if (!sourceFile || !destinationFile) { if (!sourceFile || !destinationFile) {
console.log("ERROR => Missing source or destination file"); console.log("ERROR => Missing source or destination file");
process.exit(1); process.exit();
} }
const sourceFiles = sourceFile.split(","); const sourceFiles = sourceFile.split(",");
const dstFiles = destinationFile.split(","); const dstFiles = destinationFile.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(/\..{2,4}$/)) && !(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");
process.exit();
}
compile(srcFolder, dstFile, null);
fs_1.default.watch(srcFolder, { recursive: true }, (evtType, fileName) => { fs_1.default.watch(srcFolder, { recursive: true }, (evtType, fileName) => {
if ((fileName === null || fileName === void 0 ? void 0 : fileName.match(/\(/)) || (fileName === null || fileName === void 0 ? void 0 : fileName.match(/\.js$/i))) { if (!fileName)
return; return;
} const filePathRoot = (srcFolder === null || srcFolder === void 0 ? void 0 : srcFolder.match(/\.less$/)) ? srcFolder : srcFolder + "/" + fileName;
let finalSrcPath = `${srcFolder}/main.less`; compile(filePathRoot, dstFile, evtType);
let finalDstPath = dstFile; });
if (fileName === null || fileName === void 0 ? void 0 : fileName.match(/\[/)) { }
const paths = fileName.split("/"); function compile(fileName, dst, evtType) {
const targetPathFull = paths[paths.length - 1]; 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)))) {
const targetPath = targetPathFull.replace(/\[|\]/g, "").replace(/\.less/, ""); return;
const destinationFileParentFolder = dstFile.replace(/\/[^\/]+\.css$/, ""); }
const targetDstFilePath = `${destinationFileParentFolder}/${targetPath}.css`; let finalSrcPath = (fileName === null || fileName === void 0 ? void 0 : fileName.match(/\.less$/)) ? fileName : `${fileName}/main.less`;
finalSrcPath = `${srcFolder}/${targetPathFull}`; let finalDstPath = dst;
finalDstPath = targetDstFilePath; if (fileName === null || fileName === void 0 ? void 0 : fileName.match(/\[/)) {
} const paths = fileName.split("/");
(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 targetPathFull = paths[paths.length - 1];
if (error) { const targetPath = targetPathFull.replace(/\[|\]/g, "").replace(/\.less/, "");
console.log("ERROR =>", error.message); const destinationFileParentFolder = dst.replace(/\/[^\/]+\.css$/, "");
if (!(evtType === null || evtType === void 0 ? void 0 : evtType.match(/change/i)) && fileName && fileName.match(/\[/)) { const targetDstFilePath = `${destinationFileParentFolder}/${targetPath}.css`;
fs_1.default.unlinkSync(finalDstPath); finalSrcPath = `${fileName}/${targetPathFull}`;
} finalDstPath = targetDstFilePath;
return; }
} (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) => {
console.log("Less Compilation \x1b[32msuccessful\x1b[0m!"); if (error) {
}); console.log("ERROR =>", error.message);
if (!(evtType === null || evtType === void 0 ? void 0 : evtType.match(/change/i)) && fileName && fileName.match(/\[/)) {
fs_1.default.unlinkSync(finalDstPath);
}
return;
}
console.log("Less Compilation \x1b[32msuccessful\x1b[0m!");
}); });
} }

View File

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