-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile-less.js
60 lines (51 loc) · 1.59 KB
/
compile-less.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
50
51
52
53
54
55
56
57
58
59
60
const less = require('less');
const fs = require('fs');
const path = require('path');
const chokidar = require('chokidar');
const styles = require('./src/data/styles');
//paths
const cssDir = 'src/assets/static/css/';
const lessGlob = 'src/assets/less/**/*.less'
if (!fs.existsSync(cssDir)) {
fs.mkdirSync(cssDir, { recursive: true });
console.log(`Created CSS directory: ${cssDir}`);
}
//get file name slugs from styles data
let files = [];
for (let style of styles) {
files.push(`src/assets/less/${style.slug}.less`);
}
const shouldWatch = process.argv.includes('--watch');
files.forEach(file => {
const outputFilePath = path.join(cssDir, path.basename(file, '.less') + '.css');
const compileLess = (filePath) => {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error(`Failed to read ${filePath}: ${err}`);
return;
}
less.render(data, { filename: filePath })
.then(result => {
fs.writeFile(outputFilePath, result.css, err => {
if (err) {
console.error(`Failed to write ${outputFilePath}: ${err}`);
return;
}
console.log(`Compiled ${filePath} to ${outputFilePath}`);
});
})
.catch(error => {
console.error(`Error compiling ${filePath}: ${error}`);
});
});
};
if (shouldWatch) {
// Watch file and recompile on changes
chokidar.watch(lessGlob).on('change', () => {
console.log(`Detected change in ${file}. Recompiling...`);
compileLess(file);
});
}
// Initial compilation
compileLess(file);
});