-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathless-compile.js
49 lines (44 loc) · 1.38 KB
/
less-compile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import less from 'less';
import fs from 'fs';
import path from 'path';
import { readdir } from 'fs/promises';
import { fileURLToPath } from 'url';
const dirname = path.dirname(fileURLToPath(import.meta.url));
const compileAndSave = async (lessFile) => {
const dest = lessFile.replace(path.extname(lessFile), '.css');
const data = fs.readFileSync(
lessFile,
{ encoding: 'utf8', flag: 'r' },
);
less.render(data).then((output) => {
fs.writeFileSync(dest, output.css);
// eslint-disable-next-line no-console
console.log(`Compiled ${lessFile} to ${dest}`);
});
};
const processFiles = async (parent) => {
const files = await readdir(parent, { withFileTypes: true });
files.forEach(async (file) => {
if (file.isDirectory()) {
await processFiles(path.join(parent, file.name));
}
if (path.extname(file.name) === '.less') {
await compileAndSave(path.join(parent, file.name));
}
});
};
['styles', 'blocks'].forEach(async (folder) => {
try {
await processFiles(path.join(dirname, folder));
} catch (err) {
// eslint-disable-next-line no-console
console.error(err);
}
});
if(process.argv.length > 2 && process.argv[2] == 'watch') {
fs.watch('.', { recursive: true }, (eventType, fileName) => {
if (path.extname(fileName) === '.less' && eventType === 'change') {
compileAndSave(path.join(dirname, fileName));
}
});
}