Code Upgrade
This commit is contained in:
parent
4b2d39f9c6
commit
67ab4488b2
4
.gitignore
vendored
4
.gitignore
vendored
@ -1,5 +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
|
test
|
47
README.md
47
README.md
@ -65,3 +65,50 @@ Ignore files/folders by enlosing the names in braces. Eg `(general).less`
|
|||||||
##### Compile specific files
|
##### 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`.
|
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.
|
||||||
|
62
dist/index.js
vendored
62
dist/index.js
vendored
@ -6,11 +6,50 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
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");
|
||||||
|
if (fs_1.default.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 = 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(","));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
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 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("\x1b[44mRunning Less compiler\x1b[0m ...");
|
console.log("\x1b[44mRunning Less compiler\x1b[0m ...");
|
||||||
if (!sourceFile || !destinationFile) {
|
if (!sourceFile || !destinationFile) {
|
||||||
console.log("ERROR => Missing source or destination file");
|
console.log("\x1b[33mERROR:\x1b[0m => Missing source or destination file");
|
||||||
process.exit();
|
process.exit();
|
||||||
}
|
}
|
||||||
const sourceFiles = sourceFile.split(",");
|
const sourceFiles = sourceFile.split(",");
|
||||||
@ -18,11 +57,30 @@ 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$/))) {
|
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");
|
console.log("\x1b[33mERROR:\x1b[0m Source must be a folder or a .less file");
|
||||||
process.exit();
|
process.exit();
|
||||||
}
|
}
|
||||||
compile(srcFolder, dstFile, null);
|
compile(srcFolder, dstFile, null);
|
||||||
|
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 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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) => {
|
fs_1.default.watch(srcFolder, { recursive: true }, (evtType, fileName) => {
|
||||||
if (!fileName)
|
if (!fileName)
|
||||||
return;
|
return;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "lessc-watcher",
|
"name": "lessc-watcher",
|
||||||
"version": "1.1.2",
|
"version": "1.1.3",
|
||||||
"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": {
|
||||||
|
8
src/index.d.ts
vendored
Normal file
8
src/index.d.ts
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
export type LessCssWatcherConfigObject = {
|
||||||
|
src?: string | string[];
|
||||||
|
dst?: string | string[];
|
||||||
|
srcDst?: {
|
||||||
|
src?: string;
|
||||||
|
dst?: string;
|
||||||
|
}[];
|
||||||
|
};
|
152
src/index.ts
Normal file
152
src/index.ts
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
#! /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!");
|
||||||
|
});
|
||||||
|
}
|
13
tsconfig.json
Normal file
13
tsconfig.json
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "es2016",
|
||||||
|
"module": "commonjs",
|
||||||
|
"rootDir": "./src",
|
||||||
|
"outDir": "./dist",
|
||||||
|
"removeComments": true,
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"forceConsistentCasingInFileNames": true,
|
||||||
|
"strict": true,
|
||||||
|
"skipLibCheck": true
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user