Compare commits
	
		
			10 Commits
		
	
	
		
			c443dd4199
			...
			e6e2fa65cb
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|   | e6e2fa65cb | ||
|   | 2cb578d38b | ||
|   | cb5f1a0efd | ||
|   | 59d73239d8 | ||
|   | 5dc871d8db | ||
|   | 1ac9bb4fbe | ||
|   | e3498601a1 | ||
|   | f256310387 | ||
|   | d138241473 | ||
|   | d19dc7ae08 | 
							
								
								
									
										8
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										8
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @ -1,5 +1,5 @@ | ||||
| # Ignore npm modules and src folder | ||||
| node_modules | ||||
| # src | ||||
| # tsconfig.json | ||||
| # Ignore npm modules and src folder | ||||
| node_modules | ||||
| # src | ||||
| # tsconfig.json | ||||
| test | ||||
							
								
								
									
										228
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										228
									
								
								README.md
									
									
									
									
									
								
							| @ -1,114 +1,114 @@ | ||||
| # Less watch for .less files | ||||
| 
 | ||||
| This is a super-light npm package that watches .less files and actively compiles them into .css files | ||||
| 
 | ||||
| --- | ||||
| 
 | ||||
| ## Prerequisites | ||||
| 
 | ||||
| You need `less` npm package insalled. you can add it your project by running: | ||||
| 
 | ||||
| ```bash | ||||
| npm install less | ||||
| ``` | ||||
| 
 | ||||
| ## Installation | ||||
| 
 | ||||
| To install this package simply run: | ||||
| 
 | ||||
| ```bash | ||||
| npm install lessc-watcher | ||||
| ``` | ||||
| 
 | ||||
| ## How to use | ||||
| 
 | ||||
| There are few different ways to run your less compiler | ||||
| 
 | ||||
| ### Basic usage | ||||
| 
 | ||||
| ```bash | ||||
| npx lessc-watcher --src ./folder --dst ./dist/less.css | ||||
| ``` | ||||
| 
 | ||||
| This traverses the folder and searches for a `main.less` file. This file serves as the source for your bundled `.css` file. If you want to target a specific file, use: | ||||
| 
 | ||||
| ```bash | ||||
| npx lessc-watcher --src ./folder/src.less --dst ./dist/less.css | ||||
| ``` | ||||
| 
 | ||||
| Or you can just watch an entire folder. In this case less-watch will searc for an entry file named `main.less`. Without this entry file, your compiler wouldn't work. | ||||
| 
 | ||||
| ```bash | ||||
| 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 | ||||
| 
 | ||||
| You can add more source folders by using a comma(`,`) separator. Example: | ||||
| 
 | ||||
| ```bash | ||||
| npx lessc-watcher --src ./folder-1,./folder-2 --dst ./dist/folder-1.css,./dist/folder-2.css | ||||
| ``` | ||||
| 
 | ||||
| **_NOTE:_** Your `--dst` input must match the number of comma-separated folders in your `src` input. | ||||
| 
 | ||||
| ## Advanced Features | ||||
| 
 | ||||
| lessc-watcher has more advanced features to better fine-tune your files | ||||
| 
 | ||||
| ### Ignore files | ||||
| 
 | ||||
| Ignore files/folders by enlosing the names in braces. Eg `(general).less` | ||||
| 
 | ||||
| ### Compile specific files | ||||
| 
 | ||||
| If you're watching an entire folder, you can compile specific files in that folder to a stanalone file. Example if you create a file named `[test].less` in your watch directory, in your distribution directory there will be an extra file named `test.css`. | ||||
| 
 | ||||
| ## Using a `lesscw.config.json` file | ||||
| 
 | ||||
| You can use a `lesscw.config.json` file to add your files instead of using the CLI interface. For this to work your `lesscw.config.json` file must be located in the root directory of your project, and it must contain at least one `src` and one `dst` entry | ||||
| 
 | ||||
| ### Basic Config JSON | ||||
| 
 | ||||
| ```json | ||||
| { | ||||
|     "src": "./folder", | ||||
|     "dst": "./dist/less.css" | ||||
| } | ||||
| ``` | ||||
| 
 | ||||
| This works the same as running `npx lessc-watcher --src ./folder --dst ./dist/less.css` in your terminal. Instead you only need to run: | ||||
| 
 | ||||
| ```bash | ||||
| npx lessc-watcher | ||||
| ``` | ||||
| 
 | ||||
| ### Using multiple sources and destinations | ||||
| 
 | ||||
| ```json | ||||
| { | ||||
|     "src": ["./folder", "./folder-2", "./folder/sub-folder/admin.less"], | ||||
|     "dst": ["./dist/less.css", "./dist/folder-2/less.css", "./dist/sub-folder.css"] | ||||
| } | ||||
| ``` | ||||
| 
 | ||||
| Like the CLI paradigm, the number of paths in the `src` array must match the `dst array`. And note that you can use multiple different folders with multiple different destinations. | ||||
| 
 | ||||
| ### Using a `srcDst` cofiguration | ||||
| 
 | ||||
| ALternative to the `src` and `dst` paths, you can provide a `srcDst` key instead, this will contain an array of key-value pairs as follows | ||||
| 
 | ||||
| ```json | ||||
| { | ||||
|     "srcDst": [ | ||||
|         { | ||||
|             "src": "./folder", | ||||
|             "dst": "./folder-2" | ||||
|         } | ||||
|     ] | ||||
| } | ||||
| ``` | ||||
| 
 | ||||
| **_NOTE:_** If you provide a config file and still add `--src` and `--dst` arguments in your terminal, the terminal source and distributuion arguments will be ignored. | ||||
| # Less watch for .less files | ||||
| 
 | ||||
| This is a super-light npm package that watches .less files and actively compiles them into .css files | ||||
| 
 | ||||
| --- | ||||
| 
 | ||||
| ## Prerequisites | ||||
| 
 | ||||
| You need `less` npm package insalled. you can add it your project by running: | ||||
| 
 | ||||
| ```bash | ||||
| npm install less | ||||
| ``` | ||||
| 
 | ||||
| ## Installation | ||||
| 
 | ||||
| To install this package simply run: | ||||
| 
 | ||||
| ```bash | ||||
| npm install lessc-watcher | ||||
| ``` | ||||
| 
 | ||||
| ## How to use | ||||
| 
 | ||||
| There are few different ways to run your less compiler | ||||
| 
 | ||||
| ### Basic usage | ||||
| 
 | ||||
| ```bash | ||||
| npx lessc-watcher --src ./folder --dst ./dist/less.css | ||||
| ``` | ||||
| 
 | ||||
| This traverses the `--src` folder and searches for a `main.less` file. This file serves as the source for your bundled `.css` file. If you want to target a specific file, use: | ||||
| 
 | ||||
| ```bash | ||||
| npx lessc-watcher --src ./folder/src.less --dst ./dist/less.css | ||||
| ``` | ||||
| 
 | ||||
| Or you can just watch an entire folder. In this case less-watch will searc for an entry file named `main.less`. Without this entry file, your compiler wouldn't work. | ||||
| 
 | ||||
| ```bash | ||||
| 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 | ||||
| 
 | ||||
| You can add more source folders by using a comma(`,`) separator. Example: | ||||
| 
 | ||||
| ```bash | ||||
| npx lessc-watcher --src ./folder-1,./folder-2 --dst ./dist/folder-1.css,./dist/folder-2.css | ||||
| ``` | ||||
| 
 | ||||
| **_NOTE:_** Your `--dst` input must match the number of comma-separated folders in your `src` input. | ||||
| 
 | ||||
| ## Advanced Features | ||||
| 
 | ||||
| lessc-watcher has more advanced features to better fine-tune your files | ||||
| 
 | ||||
| ### Ignore files | ||||
| 
 | ||||
| Ignore files/folders by enlosing the names in braces. Eg `(general).less` | ||||
| 
 | ||||
| ### Compile specific files | ||||
| 
 | ||||
| If you're watching an entire folder, you can compile specific files in that folder to a stanalone file. Example if you create a file named `[test].less` in your watch directory, in your distribution directory there will be an extra file named `test.css`. | ||||
| 
 | ||||
| ## Using a `lesscw.config.json` file | ||||
| 
 | ||||
| You can use a `lesscw.config.json` file to add your files instead of using the CLI interface. For this to work your `lesscw.config.json` file must be located in the root directory of your project, and it must contain at least one `src` and one `dst` entry | ||||
| 
 | ||||
| ### Basic Config JSON | ||||
| 
 | ||||
| ```json | ||||
| { | ||||
|     "src": "./folder", | ||||
|     "dst": "./dist/less.css" | ||||
| } | ||||
| ``` | ||||
| 
 | ||||
| This works the same as running `npx lessc-watcher --src ./folder --dst ./dist/less.css` in your terminal. Instead you only need to run: | ||||
| 
 | ||||
| ```bash | ||||
| npx lessc-watcher | ||||
| ``` | ||||
| 
 | ||||
| ### Using multiple sources and destinations | ||||
| 
 | ||||
| ```json | ||||
| { | ||||
|     "src": ["./folder", "./folder-2", "./folder/sub-folder/admin.less"], | ||||
|     "dst": ["./dist/less.css", "./dist/folder-2/less.css", "./dist/sub-folder.css"] | ||||
| } | ||||
| ``` | ||||
| 
 | ||||
| Like the CLI paradigm, the number of paths in the `src` array must match the `dst array`. And note that you can use multiple different folders with multiple different destinations. | ||||
| 
 | ||||
| ### Using a `srcDst` cofiguration | ||||
| 
 | ||||
| ALternative to the `src` and `dst` paths, you can provide a `srcDst` key instead, this will contain an array of key-value pairs as follows | ||||
| 
 | ||||
| ```json | ||||
| { | ||||
|     "srcDst": [ | ||||
|         { | ||||
|             "src": "./folder", | ||||
|             "dst": "./folder-2" | ||||
|         } | ||||
|     ] | ||||
| } | ||||
| ``` | ||||
| 
 | ||||
| **_NOTE:_** If you provide a config file and still add `--src` and `--dst` arguments in your terminal, the terminal source and distributuion arguments will be ignored. | ||||
|  | ||||
							
								
								
									
										283
									
								
								dist/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										283
									
								
								dist/index.js
									
									
									
									
										vendored
									
									
								
							| @ -6,111 +6,242 @@ var __importDefault = (this && this.__importDefault) || function (mod) { | ||||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||||
| const fs_1 = __importDefault(require("fs")); | ||||
| const child_process_1 = require("child_process"); | ||||
| if (fs_1.default.existsSync("./lesscw.config.json")) { | ||||
|     if (process.argv.indexOf("--src") >= 0 || process.argv.indexOf("--dst") >= 0) { | ||||
| const grabSrcDisStrings = () => { | ||||
|     let srcArray = []; | ||||
|     let dstArray = []; | ||||
|     if (fs_1.default.existsSync("./lesscw.config.json")) { | ||||
|         if (process.argv.indexOf("--src")) { | ||||
|             try { | ||||
|                 process.argv.splice(process.argv.indexOf("--src")); | ||||
|             } | ||||
|             catch (error) { } | ||||
|         } | ||||
|         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); | ||||
|         } | ||||
|         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(",")); | ||||
|         } | ||||
|         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(",")); | ||||
|             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") { | ||||
|                 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)) { | ||||
|                 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; | ||||
|                 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); | ||||
|                     } | ||||
|                 }); | ||||
|             } | ||||
|             else { | ||||
|                 console.log("- \x1b[31mERROR:\x1b[0m Your config file has some errors. Please check your config file"); | ||||
|                 process.exit(); | ||||
|             } | ||||
|         } | ||||
|         catch (error) { | ||||
|             console.log("- \x1b[31mERROR:\x1b[0m Your config file has some errors. ERROR =>", error.message); | ||||
|             process.exit(); | ||||
|         } | ||||
|     } | ||||
|     catch (error) { | ||||
|         console.log("ERROR in your config file =>", 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(); | ||||
|         } | ||||
|     } | ||||
|     return { | ||||
|         sourceFile: srcArray.join(","), | ||||
|         destinationFile: dstArray.join(","), | ||||
|     }; | ||||
| }; | ||||
| const { sourceFile, destinationFile } = grabSrcDisStrings(); | ||||
| if (sourceFile && destinationFile) { | ||||
|     process.argv.push("--src", sourceFile, "--dst", destinationFile); | ||||
| } | ||||
| 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; | ||||
| console.log("\x1b[44mRunning Less compiler\x1b[0m ..."); | ||||
| console.log("- \x1b[35mStart:\x1b[0m Running Less compiler ..."); | ||||
| 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(); | ||||
| } | ||||
| const sourceFiles = sourceFile.split(","); | ||||
| const dstFiles = destinationFile.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$/))) { | ||||
|         console.log("\x1b[33mERROR:\x1b[0m Source must be a folder or a .less file"); | ||||
|         process.exit(); | ||||
|     } | ||||
|     compile(srcFolder, dstFile, null); | ||||
|     if (!fs_1.default.existsSync(srcFolder)) { | ||||
| 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$/))) { | ||||
|             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.writeFileSync(srcFolder, "", "utf-8"); | ||||
|             } | ||||
|             else { | ||||
|                 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, ""))) { | ||||
|         } | ||||
|         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, | ||||
|                 }); | ||||
|             } | ||||
|             else { | ||||
|                 fs_1.default.mkdirSync(dstFile.replace(/\/[^\/]+\.[^\/]+$/, ""), { | ||||
|                     recursive: true, | ||||
|                 }); | ||||
|             } | ||||
|         } | ||||
|         compile(srcFolder, dstFile, null); | ||||
|         try { | ||||
|             fs_1.default.readdirSync(srcFolder).forEach((file) => { | ||||
|                 if (file === null || file === void 0 ? void 0 : file.match(/^\[.*\.less$/)) { | ||||
|                     compile(srcFolder + "/" + file, dstFile, null); | ||||
|                 } | ||||
|             }); | ||||
|         } | ||||
|         catch (error) { } | ||||
|         if (srcFolder === null || srcFolder === void 0 ? void 0 : srcFolder.match(/\.less$/)) { | ||||
|             fs_1.default.mkdirSync(srcFolder.replace(/\/[^\/]+\.less$/, ""), { recursive: true }); | ||||
|             fs_1.default.writeFileSync(srcFolder, "", "utf-8"); | ||||
|             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(srcFolder, dstFilePathRoot, null); | ||||
|                     } | ||||
|                     else { | ||||
|                         fs_1.default.unwatchFile(srcFolder); | ||||
|                     } | ||||
|                 } | ||||
|                 catch (error) { | ||||
|                     console.log("- \x1b[31mERROR:\x1b[0m Please check your config file =>", error.message); | ||||
|                 } | ||||
|             }); | ||||
|         } | ||||
|         else if (!(srcFolder === null || srcFolder === void 0 ? void 0 : srcFolder.match(/\.[^\/]+$/))) { | ||||
|             fs_1.default.watch(srcFolder, { | ||||
|                 recursive: ((_a = process.platform) === null || _a === void 0 ? void 0 : _a.match(/win32/i)) | ||||
|                     ? true | ||||
|                     : undefined, | ||||
|             }, (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 (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)) { | ||||
|                         return; | ||||
|                     } | ||||
|                     else { | ||||
|                         compile(srcFilePathRoot, dstFile, evtType); | ||||
|                     } | ||||
|                 } | ||||
|                 catch (error) { | ||||
|                     console.log("- \x1b[31mERROR:\x1b[0m Please check your config file =>", error.message); | ||||
|                 } | ||||
|             }); | ||||
|         } | ||||
|         else { | ||||
|             fs_1.default.mkdirSync(srcFolder.replace(/\/[^\/]+\.[^\/]+$/, ""), { recursive: true }); | ||||
|             console.log("- \x1b[31mERROR:\x1b[0m Source must be a folder or a .less file"); | ||||
|             process.exit(); | ||||
|         } | ||||
|     } | ||||
|     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, "", "utf-8"); | ||||
|         } | ||||
|         else { | ||||
|             fs_1.default.mkdirSync(dstFile.replace(/\/[^\/]+\.[^\/]+$/, ""), { recursive: true }); | ||||
|             fs_1.default.writeFileSync((dstFile + "/_main.css").replace(/\/\//g, ""), "", "utf-8"); | ||||
|         } | ||||
|     } | ||||
|     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; | ||||
|         compile(filePathRoot, dstFile, evtType); | ||||
|     }); | ||||
| } | ||||
| 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; | ||||
|     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)) { | ||||
|         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]; | ||||
|         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}/${targetPathFull}`; | ||||
|         finalSrcPath = fileName; | ||||
|         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("ERROR =>", error.message); | ||||
|             if (!(evtType === null || evtType === void 0 ? void 0 : evtType.match(/change/i)) && fileName && fileName.match(/\[/)) { | ||||
|             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(/\[/)) { | ||||
|                 fs_1.default.unlinkSync(finalDstPath); | ||||
|             } | ||||
|             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", { 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); | ||||
|             traverseFiles(newSrcDistStrings.sourceFile, newSrcDistStrings.destinationFile); | ||||
|         } | ||||
|     }); | ||||
| } | ||||
|  | ||||
							
								
								
									
										22
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										22
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							| @ -1,22 +0,0 @@ | ||||
| { | ||||
|     "name": "lessc-watcher", | ||||
|     "version": "1.0.3", | ||||
|     "lockfileVersion": 3, | ||||
|     "requires": true, | ||||
|     "packages": { | ||||
|         "": { | ||||
|             "name": "lessc-watcher", | ||||
|             "version": "1.0.3", | ||||
|             "license": "ISC", | ||||
|             "devDependencies": { | ||||
|                 "@types/node": "^20.4.5" | ||||
|             } | ||||
|         }, | ||||
|         "node_modules/@types/node": { | ||||
|             "version": "20.4.5", | ||||
|             "resolved": "https://registry.npmjs.org/@types/node/-/node-20.4.5.tgz", | ||||
|             "integrity": "sha512-rt40Nk13II9JwQBdeYqmbn2Q6IVTA5uPhvSO+JVqdXw/6/4glI6oR9ezty/A9Hg5u7JH4OmYmuQ+XvjKm0Datg==", | ||||
|             "dev": true | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										51
									
								
								package.json
									
									
									
									
									
								
							
							
						
						
									
										51
									
								
								package.json
									
									
									
									
									
								
							| @ -1,23 +1,30 @@ | ||||
| { | ||||
|     "name": "lessc-watcher", | ||||
|     "version": "1.1.4", | ||||
|     "description": "A minimal package to watch less files and compile them to css", | ||||
|     "main": "dist/index.js", | ||||
|     "bin": { | ||||
|         "lessc-watcher": "./dist/index.js" | ||||
|     }, | ||||
|     "repository": { | ||||
|         "type": "git", | ||||
|         "url": "git+https://github.com/BenjaminToby/less-watcher.git" | ||||
|     }, | ||||
|     "keywords": [ | ||||
|         "less", | ||||
|         "watcher", | ||||
|         "css" | ||||
|     ], | ||||
|     "author": "Benjamin Toby", | ||||
|     "license": "ISC", | ||||
|     "devDependencies": { | ||||
|         "@types/node": "^20.4.5" | ||||
|     } | ||||
| } | ||||
|   "name": "lessc-watcher", | ||||
|   "version": "1.2.2", | ||||
|   "description": "A minimal package to watch less files and compile them to css", | ||||
|   "main": "dist/index.js", | ||||
|   "bin": { | ||||
|     "lessc-watcher": "./dist/index.js" | ||||
|   }, | ||||
|   "repository": { | ||||
|     "type": "git", | ||||
|     "url": "git+https://github.com/BenjaminToby/less-watcher.git" | ||||
|   }, | ||||
|   "keywords": [ | ||||
|     "less", | ||||
|     "watcher", | ||||
|     "css" | ||||
|   ], | ||||
|   "author": "Benjamin Toby", | ||||
|   "license": "MIT", | ||||
|   "devDependencies": { | ||||
|     "@types/node": "^20.4.5", | ||||
|     "@types/bun": "latest" | ||||
|   }, | ||||
|   "dependencies": { | ||||
|     "less": "^4.2.0" | ||||
|   }, | ||||
|   "peerDependencies": { | ||||
|     "typescript": "^5.0.0" | ||||
|   } | ||||
| } | ||||
							
								
								
									
										14
									
								
								publish.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								publish.sh
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,14 @@ | ||||
| #!/bin/bash | ||||
| 
 | ||||
| tsc | ||||
| 
 | ||||
| if [ -z "$1" ]; then | ||||
|     msg="Updates" | ||||
| else | ||||
|     msg="$1" | ||||
| fi | ||||
| 
 | ||||
| git add . | ||||
| git commit -m "$msg" | ||||
| git push | ||||
| bun publish | ||||
							
								
								
									
										11
									
								
								push.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										11
									
								
								push.sh
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,11 @@ | ||||
| #!/bin/bash | ||||
| 
 | ||||
| if [ -z "$1" ]; then | ||||
|     msg="Updates" | ||||
| else | ||||
|     msg="$1" | ||||
| fi | ||||
| 
 | ||||
| git add . | ||||
| git commit -m "$msg" | ||||
| git push | ||||
							
								
								
									
										8
									
								
								src/index.d.ts
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										8
									
								
								src/index.d.ts
									
									
									
									
										vendored
									
									
								
							| @ -1,8 +0,0 @@ | ||||
| export type LessCssWatcherConfigObject = { | ||||
|     src?: string | string[]; | ||||
|     dst?: string | string[]; | ||||
|     srcDst?: { | ||||
|         src?: string; | ||||
|         dst?: string; | ||||
|     }[]; | ||||
| }; | ||||
							
								
								
									
										528
									
								
								src/index.ts
									
									
									
									
									
								
							
							
						
						
									
										528
									
								
								src/index.ts
									
									
									
									
									
								
							| @ -1,152 +1,376 @@ | ||||
| #! /usr/bin/env node | ||||
| 
 | ||||
| import fs from "fs"; | ||||
| import { exec } from "child_process"; | ||||
| import { LessCssWatcherConfigObject } from "./index.d"; | ||||
| 
 | ||||
| if (fs.existsSync("./lesscw.config.json")) { | ||||
|     if (process.argv.indexOf("--src") >= 0 || process.argv.indexOf("--dst") >= 0) { | ||||
|         try { | ||||
|             process.argv.splice(process.argv.indexOf("--src")); | ||||
|             process.argv.splice(process.argv.indexOf("--dst")); | ||||
|         } catch (error) {} | ||||
|     } | ||||
| 
 | ||||
|     try { | ||||
|         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); | ||||
|         } 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(",")); | ||||
|         } 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(",")); | ||||
|             } | ||||
|         } | ||||
|     } catch (error: any) { | ||||
|         console.log("ERROR in your config file =>", error.message); | ||||
|         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; | ||||
| 
 | ||||
| /** ****************************************************************************** */ | ||||
| /** ****************************************************************************** */ | ||||
| /** ****************************************************************************** */ | ||||
| /** ****************************************************************************** */ | ||||
| /** ****************************************************************************** */ | ||||
| /** ****************************************************************************** */ | ||||
| 
 | ||||
| console.log("\x1b[44mRunning Less compiler\x1b[0m ..."); | ||||
| 
 | ||||
| if (!sourceFile || !destinationFile) { | ||||
|     console.log("\x1b[33mERROR:\x1b[0m => Missing source or destination file"); | ||||
|     process.exit(); | ||||
| } | ||||
| 
 | ||||
| const sourceFiles = sourceFile.split(","); | ||||
| const dstFiles = destinationFile.split(","); | ||||
| 
 | ||||
| /** | ||||
|  * Loop through source files and destination files and run the compile function | ||||
|  */ | ||||
| for (let i = 0; i < sourceFiles.length; i++) { | ||||
|     const srcFolder = sourceFiles[i]; | ||||
|     const dstFile = dstFiles[i]; | ||||
| 
 | ||||
|     if (srcFolder?.match(/\.[^\/]+$/) && !srcFolder?.match(/\.less$/)) { | ||||
|         console.log("\x1b[33mERROR:\x1b[0m Source must be a folder or a .less file"); | ||||
|         process.exit(); | ||||
|     } | ||||
| 
 | ||||
|     compile(srcFolder, dstFile, null); | ||||
| 
 | ||||
|     if (!fs.existsSync(srcFolder)) { | ||||
|         if (srcFolder?.match(/\.less$/)) { | ||||
|             fs.mkdirSync(srcFolder.replace(/\/[^\/]+\.less$/, ""), { recursive: true }); | ||||
|             fs.writeFileSync(srcFolder, "", "utf-8"); | ||||
|         } else { | ||||
|             fs.mkdirSync(srcFolder.replace(/\/[^\/]+\.[^\/]+$/, ""), { recursive: true }); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     if (!fs.existsSync(dstFile)) { | ||||
|         if (dstFile?.match(/\.css$/)) { | ||||
|             fs.mkdirSync(dstFile.replace(/\/[^\/]+\.css$/, ""), { recursive: true }); | ||||
|             fs.writeFileSync(dstFile, "", "utf-8"); | ||||
|         } else { | ||||
|             fs.mkdirSync(dstFile.replace(/\/[^\/]+\.[^\/]+$/, ""), { recursive: true }); | ||||
|             fs.writeFileSync((dstFile + "/_main.css").replace(/\/\//g, ""), "", "utf-8"); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     fs.watch(srcFolder, { recursive: true }, (evtType, fileName) => { | ||||
|         if (!fileName) return; | ||||
| 
 | ||||
|         const filePathRoot = srcFolder?.match(/\.less$/) ? srcFolder : srcFolder + "/" + fileName; | ||||
| 
 | ||||
|         compile(filePathRoot, dstFile, evtType); | ||||
|     }); | ||||
| } | ||||
| 
 | ||||
| /** | ||||
|  * Compile less file to css function | ||||
|  * @param fileName - less file path or folder path | ||||
|  * @param dst - destination file path or folder path | ||||
|  * @param evtType - event type (change, rename, etc.) or null | ||||
|  * @returns | ||||
|  */ | ||||
| function compile(fileName: string, dst: string, evtType: string | null) { | ||||
|     if (fileName?.match(/\(/) || (fileName.match(/\..{2,4}$/) && !fileName?.match(/\.less$/i))) { | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     let finalSrcPath = fileName?.match(/\.less$/) ? fileName : `${fileName}/main.less`; | ||||
|     let finalDstPath = dst; | ||||
| 
 | ||||
|     if (fileName?.match(/\[/)) { | ||||
|         const paths = fileName.split("/"); | ||||
|         const targetPathFull = paths[paths.length - 1]; | ||||
|         const targetPath = targetPathFull.replace(/\[|\]/g, "").replace(/\.less/, ""); | ||||
| 
 | ||||
|         const destinationFileParentFolder = dst.replace(/\/[^\/]+\.css$/, ""); | ||||
| 
 | ||||
|         const targetDstFilePath = `${destinationFileParentFolder}/${targetPath}.css`; | ||||
| 
 | ||||
|         finalSrcPath = `${fileName}/${targetPathFull}`; | ||||
|         finalDstPath = targetDstFilePath; | ||||
|     } | ||||
| 
 | ||||
|     exec(`lessc ${finalSrcPath} ${finalDstPath?.match(/\.css$/) ? finalDstPath : finalDstPath.replace(/\/$/, "") + "/_main.css"}`, (error, stdout, stderr) => { | ||||
|         /** @type {Error} */ | ||||
|         if (error) { | ||||
|             console.log("ERROR =>", error.message); | ||||
| 
 | ||||
|             if (!evtType?.match(/change/i) && fileName && fileName.match(/\[/)) { | ||||
|                 fs.unlinkSync(finalDstPath); | ||||
|             } | ||||
| 
 | ||||
|             return; | ||||
|         } | ||||
| 
 | ||||
|         console.log("Less Compilation \x1b[32msuccessful\x1b[0m!"); | ||||
|     }); | ||||
| } | ||||
| import fs from "fs"; | ||||
| import { exec } from "child_process"; | ||||
| 
 | ||||
| export type LessCssWatcherConfigObject = { | ||||
|     src?: string | string[]; | ||||
|     dst?: string | string[]; | ||||
|     srcDst?: { | ||||
|         src?: string; | ||||
|         dst?: string; | ||||
|     }[]; | ||||
| }; | ||||
| 
 | ||||
| const grabSrcDisStrings = () => { | ||||
|     let srcArray: string[] = []; | ||||
|     let dstArray: string[] = []; | ||||
| 
 | ||||
|     if (fs.existsSync("./lesscw.config.json")) { | ||||
|         if (process.argv.indexOf("--src")) { | ||||
|             try { | ||||
|                 process.argv.splice(process.argv.indexOf("--src")); | ||||
|             } catch (error) {} | ||||
|         } | ||||
| 
 | ||||
|         try { | ||||
|             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" | ||||
|             ) { | ||||
|                 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) | ||||
|             ) { | ||||
|                 srcArray = configObject.src; | ||||
|                 dstArray = configObject.dst; | ||||
|             } 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" | ||||
|                     ) { | ||||
|                         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" | ||||
|                 ); | ||||
|                 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(); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     //////////////////////////////////////////////////////////////////////////////
 | ||||
|     //////////////////////////////////////////////////////////////////////////////
 | ||||
|     //////////////////////////////////////////////////////////////////////////////
 | ||||
| 
 | ||||
|     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" | ||||
|     ); | ||||
|     process.exit(); | ||||
| } | ||||
| 
 | ||||
| //////////////////////////////////////////////////////////////////////////////
 | ||||
| //////////////////////////////////////////////////////////////////////////////
 | ||||
| //////////////////////////////////////////////////////////////////////////////
 | ||||
| 
 | ||||
| /** | ||||
|  * 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++) { | ||||
|         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" | ||||
|             ); | ||||
|             process.exit(); | ||||
|         } | ||||
| 
 | ||||
|         if (!fs.existsSync(srcFolder)) { | ||||
|             if (srcFolder?.match(/\.less$/)) { | ||||
|                 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" | ||||
|                 ); | ||||
|             } | ||||
|         } 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, | ||||
|                 }); | ||||
|             } else { | ||||
|                 fs.mkdirSync(dstFile.replace(/\/[^\/]+\.[^\/]+$/, ""), { | ||||
|                     recursive: true, | ||||
|                 }); | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         compile(srcFolder, dstFile, null); | ||||
| 
 | ||||
|         try { | ||||
|             fs.readdirSync(srcFolder).forEach((file) => { | ||||
|                 if (file?.match(/^\[.*\.less$/)) { | ||||
|                     compile(srcFolder + "/" + file, dstFile, null); | ||||
|                 } | ||||
|             }); | ||||
|         } catch (error) {} | ||||
| 
 | ||||
|         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(srcFolder, dstFilePathRoot, null); | ||||
|                     } else { | ||||
|                         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: process.platform?.match(/win32/i) | ||||
|                         ? true | ||||
|                         : undefined, | ||||
|                 }, | ||||
|                 (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) | ||||
|                         ) { | ||||
|                             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(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| traverseFiles(sourceFile, destinationFile); | ||||
| 
 | ||||
| //////////////////////////////////////////////////////////////////////////////
 | ||||
| //////////////////////////////////////////////////////////////////////////////
 | ||||
| //////////////////////////////////////////////////////////////////////////////
 | ||||
| 
 | ||||
| /** | ||||
|  * Compile less file to css function | ||||
|  * @param fileName - less file path or folder path | ||||
|  * @param dst - destination file path or folder path | ||||
|  * @param evtType - event type (change, rename, etc.) or null | ||||
|  * @returns | ||||
|  */ | ||||
| function compile(fileName: string, dst: string, evtType: string | null) { | ||||
|     if ( | ||||
|         fileName?.match(/\(/) || | ||||
|         (fileName.match(/\.[\/]$/) && !fileName?.match(/\.less$/i)) | ||||
|     ) { | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     let finalSrcPath = fileName?.match(/\.less$/) | ||||
|         ? fileName | ||||
|         : `${fileName}/main.less`; | ||||
|     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("/"); | ||||
|         const targetPathFull = paths[paths.length - 1]; | ||||
|         const targetPath = targetPathFull | ||||
|             .replace(/\[|\]/g, "") | ||||
|             .replace(/\.less/, ""); | ||||
| 
 | ||||
|         const destinationFileParentFolder = dst.replace(/\/[^\/]+\.css$/, ""); | ||||
| 
 | ||||
|         const targetDstFilePath = `${destinationFileParentFolder}/${targetPath}.css`; | ||||
| 
 | ||||
|         finalSrcPath = fileName; | ||||
|         finalDstPath = targetDstFilePath; | ||||
|     } | ||||
| 
 | ||||
|     const executionCmd = `npx 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 | ||||
|             ); | ||||
| 
 | ||||
|             if ( | ||||
|                 !evtType?.match(/change/i) && | ||||
|                 fileName && | ||||
|                 fileName.match(/\[/) | ||||
|             ) { | ||||
|                 fs.unlinkSync(finalDstPath); | ||||
|             } | ||||
| 
 | ||||
|             return; | ||||
|         } | ||||
| 
 | ||||
|         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", | ||||
|         { 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 | ||||
|                 ); | ||||
|                 traverseFiles( | ||||
|                     newSrcDistStrings.sourceFile, | ||||
|                     newSrcDistStrings.destinationFile | ||||
|                 ); | ||||
|             } | ||||
|         } | ||||
|     ); | ||||
| } | ||||
|  | ||||
| @ -1,13 +1,21 @@ | ||||
| { | ||||
|     "compilerOptions": { | ||||
|         "target": "es2016", | ||||
|         "target": "ES2015", | ||||
|         "module": "commonjs", | ||||
|         "rootDir": "./src", | ||||
|         "outDir": "./dist", | ||||
|         "removeComments": true, | ||||
|         "maxNodeModuleJsDepth": 10, | ||||
|         "esModuleInterop": true, | ||||
|         "forceConsistentCasingInFileNames": true, | ||||
|         "strict": true, | ||||
|         "skipLibCheck": true | ||||
|     } | ||||
|         "skipLibCheck": true, | ||||
|         "lib": ["dom", "dom.iterable", "esnext"], | ||||
|         "allowJs": true, | ||||
|         "incremental": true, | ||||
|         "resolveJsonModule": true, | ||||
|         "jsx": "preserve", | ||||
|         "moduleResolution": "node", | ||||
|         "declaration": true, | ||||
|         "outDir": "dist" | ||||
|     }, | ||||
|     "include": ["**/*.ts"], | ||||
|     "exclude": ["node_modules", "dist"] | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user