2023-07-29 22:18:34 +00:00
|
|
|
"use strict";
|
|
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
|
|
};
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
const fs_1 = __importDefault(require("fs"));
|
|
|
|
const child_process_1 = require("child_process");
|
2023-07-30 09:06:18 +00:00
|
|
|
const grabSrcDisStrings = () => {
|
2023-07-30 11:38:44 +00:00
|
|
|
let srcArray = [];
|
|
|
|
let dstArray = [];
|
2023-07-30 09:06:18 +00:00
|
|
|
if (fs_1.default.existsSync("./lesscw.config.json")) {
|
2023-07-30 11:38:44 +00:00
|
|
|
if (process.argv.indexOf("--src")) {
|
2023-07-30 09:06:18 +00:00
|
|
|
try {
|
|
|
|
process.argv.splice(process.argv.indexOf("--src"));
|
|
|
|
}
|
|
|
|
catch (error) { }
|
2023-07-30 06:12:09 +00:00
|
|
|
}
|
2023-07-30 09:06:18 +00:00
|
|
|
try {
|
|
|
|
const configObject = JSON.parse(fs_1.default.readFileSync("./lesscw.config.json", "utf-8"));
|
2023-09-01 11:32:35 +00:00
|
|
|
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") {
|
2023-07-30 11:38:44 +00:00
|
|
|
srcArray = configObject.src.split(",");
|
|
|
|
dstArray = configObject.dst.split(",");
|
2023-07-30 09:06:18 +00:00
|
|
|
}
|
2023-09-01 11:32:35 +00:00
|
|
|
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)) {
|
2023-07-30 11:38:44 +00:00
|
|
|
srcArray = configObject.src;
|
|
|
|
dstArray = configObject.dst;
|
2023-07-30 09:06:18 +00:00
|
|
|
}
|
2023-09-01 11:32:35 +00:00
|
|
|
else if ((configObject === null || configObject === void 0 ? void 0 : configObject.srcDst) &&
|
|
|
|
Array.isArray(configObject.srcDst) &&
|
|
|
|
configObject.srcDst.length > 0) {
|
2023-07-30 09:06:18 +00:00
|
|
|
const srcDstArray = configObject.srcDst;
|
|
|
|
srcDstArray.forEach((item) => {
|
2023-09-01 11:32:35 +00:00
|
|
|
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") {
|
2023-07-30 09:06:18 +00:00
|
|
|
srcArray.push(item.src);
|
|
|
|
dstArray.push(item.dst);
|
|
|
|
}
|
|
|
|
});
|
2023-07-30 11:38:44 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log("- \x1b[31mERROR:\x1b[0m Your config file has some errors. Please check your config file");
|
|
|
|
process.exit();
|
2023-07-30 06:12:09 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-30 09:06:18 +00:00
|
|
|
catch (error) {
|
|
|
|
console.log("- \x1b[31mERROR:\x1b[0m Your config file has some errors. ERROR =>", error.message);
|
|
|
|
process.exit();
|
|
|
|
}
|
2023-07-30 06:12:09 +00:00
|
|
|
}
|
2023-07-30 11:38:44 +00:00
|
|
|
else {
|
2023-09-01 11:32:35 +00:00
|
|
|
if (process.argv.indexOf("--src") >= 0 &&
|
|
|
|
process.argv.indexOf("--dst") >= 0) {
|
2023-07-30 11:38:44 +00:00
|
|
|
try {
|
2023-09-01 11:32:35 +00:00
|
|
|
srcArray =
|
|
|
|
process.argv[process.argv.indexOf("--src") + 1].split(",");
|
|
|
|
dstArray =
|
|
|
|
process.argv[process.argv.indexOf("--dst") + 1].split(",");
|
2023-07-30 11:38:44 +00:00
|
|
|
}
|
|
|
|
catch (error) { }
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log("- \x1b[31mERROR:\x1b[0m Missing source or destination file");
|
|
|
|
process.exit();
|
|
|
|
}
|
|
|
|
}
|
2025-01-16 06:24:22 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2023-07-30 11:38:44 +00:00
|
|
|
return {
|
|
|
|
sourceFile: srcArray.join(","),
|
|
|
|
destinationFile: dstArray.join(","),
|
|
|
|
};
|
2023-07-30 09:06:18 +00:00
|
|
|
};
|
|
|
|
const { sourceFile, destinationFile } = grabSrcDisStrings();
|
2023-07-30 11:38:44 +00:00
|
|
|
if (sourceFile && destinationFile) {
|
|
|
|
process.argv.push("--src", sourceFile, "--dst", destinationFile);
|
|
|
|
}
|
2025-01-16 06:24:22 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2023-07-30 09:06:18 +00:00
|
|
|
console.log("- \x1b[35mStart:\x1b[0m Running Less compiler ...");
|
2023-07-29 22:18:34 +00:00
|
|
|
if (!sourceFile || !destinationFile) {
|
2023-07-30 09:06:18 +00:00
|
|
|
console.log("- \x1b[31mERROR:\x1b[0m => Missing source or destination file");
|
2023-07-30 00:20:33 +00:00
|
|
|
process.exit();
|
2023-07-29 22:18:34 +00:00
|
|
|
}
|
2025-01-16 06:24:22 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
/**
|
|
|
|
* Loop through source files and destination files and run the compile function
|
|
|
|
*/
|
2023-07-30 09:06:18 +00:00
|
|
|
function traverseFiles(src, dst) {
|
2023-09-01 11:32:35 +00:00
|
|
|
var _a;
|
2023-07-30 09:06:18 +00:00
|
|
|
const sourceFiles = src.split(",");
|
|
|
|
const dstFiles = dst.split(",");
|
|
|
|
for (let i = 0; i < sourceFiles.length; i++) {
|
|
|
|
const srcFolder = sourceFiles[i];
|
|
|
|
const dstFile = dstFiles[i];
|
2023-09-01 11:32:35 +00:00
|
|
|
if ((srcFolder === null || srcFolder === void 0 ? void 0 : srcFolder.match(/\/[^\/]+\.[^\/]+$/)) &&
|
|
|
|
!(srcFolder === null || srcFolder === void 0 ? void 0 : srcFolder.match(/\.less$/))) {
|
2023-07-30 09:06:18 +00:00
|
|
|
console.log("- \x1b[31mERROR:\x1b[0m Source must be a folder or a .less file");
|
|
|
|
process.exit();
|
2023-07-30 06:12:09 +00:00
|
|
|
}
|
2023-07-30 09:06:18 +00:00
|
|
|
if (!fs_1.default.existsSync(srcFolder)) {
|
|
|
|
if (srcFolder === null || srcFolder === void 0 ? void 0 : srcFolder.match(/\.less$/)) {
|
2023-09-01 11:32:35 +00:00
|
|
|
fs_1.default.mkdirSync(srcFolder.replace(/\/[^\/]+\.less$/, ""), {
|
|
|
|
recursive: true,
|
|
|
|
});
|
2023-07-30 09:06:18 +00:00
|
|
|
fs_1.default.writeFileSync(srcFolder, "", "utf-8");
|
|
|
|
}
|
|
|
|
else {
|
2023-09-01 11:32:35 +00:00
|
|
|
fs_1.default.mkdirSync(srcFolder.replace(/\/[^\/]+\.[^\/]+$/, ""), {
|
|
|
|
recursive: true,
|
|
|
|
});
|
2023-07-30 09:06:18 +00:00
|
|
|
fs_1.default.writeFileSync((srcFolder + "/main.less").replace(/\/\//g, ""), "", "utf-8");
|
|
|
|
}
|
2023-07-30 06:12:09 +00:00
|
|
|
}
|
2023-09-01 11:32:35 +00:00
|
|
|
else if (fs_1.default.existsSync(srcFolder) &&
|
|
|
|
fs_1.default.existsSync((srcFolder + "/main.less").replace(/\/\//g, ""))) {
|
2023-07-30 06:12:09 +00:00
|
|
|
}
|
2023-07-30 09:06:18 +00:00
|
|
|
if (!fs_1.default.existsSync(dstFile)) {
|
|
|
|
if (dstFile === null || dstFile === void 0 ? void 0 : dstFile.match(/\.css$/)) {
|
2023-09-01 11:32:35 +00:00
|
|
|
fs_1.default.mkdirSync(dstFile.replace(/\/[^\/]+\.css$/, ""), {
|
|
|
|
recursive: true,
|
|
|
|
});
|
2023-07-30 09:06:18 +00:00
|
|
|
}
|
|
|
|
else {
|
2023-09-01 11:32:35 +00:00
|
|
|
fs_1.default.mkdirSync(dstFile.replace(/\/[^\/]+\.[^\/]+$/, ""), {
|
|
|
|
recursive: true,
|
|
|
|
});
|
2023-07-30 09:06:18 +00:00
|
|
|
}
|
2023-07-30 06:12:09 +00:00
|
|
|
}
|
2023-07-30 09:06:18 +00:00
|
|
|
compile(srcFolder, dstFile, null);
|
2023-08-01 03:07:10 +00:00
|
|
|
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) { }
|
2023-07-30 11:38:44 +00:00
|
|
|
if (srcFolder === null || srcFolder === void 0 ? void 0 : srcFolder.match(/\.less$/)) {
|
|
|
|
fs_1.default.watchFile(srcFolder, { interval: 500 }, (current, previous) => {
|
2023-09-01 11:32:35 +00:00
|
|
|
const dstFilePathRoot = (dstFile === null || dstFile === void 0 ? void 0 : dstFile.match(/\.css$/))
|
|
|
|
? dstFile
|
|
|
|
: dstFile + "/" + "_main.css";
|
2023-07-30 11:38:44 +00:00
|
|
|
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);
|
|
|
|
}
|
2023-07-30 09:06:18 +00:00
|
|
|
}
|
2023-07-30 11:38:44 +00:00
|
|
|
catch (error) {
|
|
|
|
console.log("- \x1b[31mERROR:\x1b[0m Please check your config file =>", error.message);
|
2023-07-30 09:06:18 +00:00
|
|
|
}
|
2023-07-30 11:38:44 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else if (!(srcFolder === null || srcFolder === void 0 ? void 0 : srcFolder.match(/\.[^\/]+$/))) {
|
2023-09-01 11:32:35 +00:00
|
|
|
fs_1.default.watch(srcFolder, {
|
2023-09-30 18:36:01 +00:00
|
|
|
recursive: ((_a = process.platform) === null || _a === void 0 ? void 0 : _a.match(/win32/i))
|
2023-09-01 11:32:35 +00:00
|
|
|
? true
|
|
|
|
: undefined,
|
|
|
|
}, (evtType, fileName) => {
|
2023-07-30 11:38:44 +00:00
|
|
|
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(",");
|
2023-08-01 03:07:10 +00:00
|
|
|
if (fileName === null || fileName === void 0 ? void 0 : fileName.match(/^\[/)) {
|
|
|
|
compile(srcFolder + "/" + fileName, dstFile, evtType);
|
|
|
|
}
|
2023-09-01 11:32:35 +00:00
|
|
|
else if ((fileName === null || fileName === void 0 ? void 0 : fileName.match(/^\(/)) ||
|
|
|
|
activeSourceFiles.includes(srcFilePathRoot)) {
|
2023-07-30 11:38:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
compile(srcFilePathRoot, dstFile, evtType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
console.log("- \x1b[31mERROR:\x1b[0m Please check your config file =>", error.message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log("- \x1b[31mERROR:\x1b[0m Source must be a folder or a .less file");
|
|
|
|
process.exit();
|
|
|
|
}
|
2023-07-30 06:12:09 +00:00
|
|
|
}
|
2023-07-30 00:20:33 +00:00
|
|
|
}
|
2023-07-30 09:06:18 +00:00
|
|
|
traverseFiles(sourceFile, destinationFile);
|
2025-01-16 06:24:22 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2023-07-30 00:20:33 +00:00
|
|
|
function compile(fileName, dst, evtType) {
|
2023-09-01 11:32:35 +00:00
|
|
|
if ((fileName === null || fileName === void 0 ? void 0 : fileName.match(/\(/)) ||
|
|
|
|
(fileName.match(/\.[\/]$/) && !(fileName === null || fileName === void 0 ? void 0 : fileName.match(/\.less$/i)))) {
|
2023-07-30 00:20:33 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-09-01 11:32:35 +00:00
|
|
|
let finalSrcPath = (fileName === null || fileName === void 0 ? void 0 : fileName.match(/\.less$/))
|
|
|
|
? fileName
|
|
|
|
: `${fileName}/main.less`;
|
2023-07-30 11:38:44 +00:00
|
|
|
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 });
|
|
|
|
}
|
2023-07-30 00:20:33 +00:00
|
|
|
if (fileName === null || fileName === void 0 ? void 0 : fileName.match(/\[/)) {
|
|
|
|
const paths = fileName.split("/");
|
|
|
|
const targetPathFull = paths[paths.length - 1];
|
2023-09-01 11:32:35 +00:00
|
|
|
const targetPath = targetPathFull
|
|
|
|
.replace(/\[|\]/g, "")
|
|
|
|
.replace(/\.less/, "");
|
2023-07-30 00:20:33 +00:00
|
|
|
const destinationFileParentFolder = dst.replace(/\/[^\/]+\.css$/, "");
|
|
|
|
const targetDstFilePath = `${destinationFileParentFolder}/${targetPath}.css`;
|
2023-08-01 03:07:10 +00:00
|
|
|
finalSrcPath = fileName;
|
2023-07-30 00:20:33 +00:00
|
|
|
finalDstPath = targetDstFilePath;
|
|
|
|
}
|
2025-01-16 06:24:22 +00:00
|
|
|
const executionCmd = `npx lessc ${finalSrcPath} ${finalDstPath}`;
|
2023-07-30 11:38:44 +00:00
|
|
|
(0, child_process_1.exec)(executionCmd, (error, stdout, stderr) => {
|
2025-01-16 06:24:22 +00:00
|
|
|
/** @type {Error} */
|
2023-07-30 00:20:33 +00:00
|
|
|
if (error) {
|
2023-07-30 09:06:18 +00:00
|
|
|
console.log("- \x1b[33mWarn:\x1b[0m Compilation didn't run successfully. ERROR =>", error.message);
|
2023-09-01 11:32:35 +00:00
|
|
|
if (!(evtType === null || evtType === void 0 ? void 0 : evtType.match(/change/i)) &&
|
|
|
|
fileName &&
|
|
|
|
fileName.match(/\[/)) {
|
2023-07-30 00:20:33 +00:00
|
|
|
fs_1.default.unlinkSync(finalDstPath);
|
2023-07-29 22:18:34 +00:00
|
|
|
}
|
2023-07-30 00:20:33 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-07-30 09:06:18 +00:00
|
|
|
console.log("- \x1b[32mCompiled:\x1b[0m Less Compilation Successful!");
|
|
|
|
});
|
|
|
|
}
|
2025-01-16 06:24:22 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
/**
|
|
|
|
* watch for changes to the config file
|
|
|
|
*/
|
2023-07-30 09:06:18 +00:00
|
|
|
if (fs_1.default.existsSync("./lesscw.config.json")) {
|
2023-07-30 11:38:44 +00:00
|
|
|
fs_1.default.watchFile("./lesscw.config.json", { interval: 500 }, (evtType, fileName) => {
|
|
|
|
console.log("- \x1b[34mInfo:\x1b[0m Restarting process...");
|
2023-07-30 09:06:18 +00:00
|
|
|
const newSrcDistStrings = grabSrcDisStrings();
|
2023-09-01 11:32:35 +00:00
|
|
|
if (newSrcDistStrings.destinationFile &&
|
|
|
|
newSrcDistStrings.sourceFile) {
|
2023-07-30 09:06:18 +00:00
|
|
|
process.argv.push("--src", newSrcDistStrings.sourceFile, "--dst", newSrcDistStrings.destinationFile);
|
|
|
|
traverseFiles(newSrcDistStrings.sourceFile, newSrcDistStrings.destinationFile);
|
|
|
|
}
|
2023-07-29 22:18:34 +00:00
|
|
|
});
|
|
|
|
}
|