forked from r3c/yaronet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
74 lines (55 loc) · 1.73 KB
/
main.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"use strict";
const fs = require('fs');
const less = require('less');
const path = require('path');
const convert = (source, target) => new Promise((resolve, reject) => fs.readFile(source, 'utf8', (err, data) => {
if (err)
return reject(err);
less
.render(data, {
filename: source
})
.then(function (output) {
fs.writeFile(target, output, (err) => {
if (err)
return reject(err);
console.log('built ' + target);
resolve();
});
});
}));
const recurse = (parent, callback) => new Promise((resolve, reject) => fs.readdir(parent, { withFileTypes: true }, (err, files) => {
const promises = [];
for (var file of files) {
const source = path.join(parent, file.name);
if (file.isFile() && file.name.endsWith('.less') && !file.name.endsWith('.inc.less'))
promises.push(callback(source, source.replace(/\.less$/, '.css')));
else if (file.isDirectory())
promises.push(recurse(source, callback));
}
Promise.all(promises)
.catch(reject)
.then(resolve);
}));
const remove = (source, target) => new Promise((resolve, reject) => fs.unlink(target, (err) => {
if (err && err.code !== 'ENOENT')
return reject(err);
console.log('removed ' + target);
resolve();
}));
const action = process.argv[2];
switch (action) {
case 'build':
recurse('src/static', convert)
.catch(err => console.log(err))
.then(() => console.log('you can now deploy yAronet with option "engine.text.display.use-less" set to "false".'));
break;
case 'clean':
recurse('src/static', remove)
.catch(err => console.log(err))
.then(() => console.log('switch option "engine.text.display.use-less" set to "true" if you want to run website locally.'));
break;
default:
console.error(`unknown action "${action}"`);
break;
}