-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
executable file
·83 lines (76 loc) · 2.12 KB
/
index.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
75
76
77
78
79
80
81
82
83
#!/usr/bin/env node
const fs = require("fs");
const less = require("less");
const watch = require("node-watch");
const argv = require("minimist")(
process.argv.slice(2),
{
string: ['antd', 'theme'],
boolean: ["verbose", "watch"],
alias: {"verbose": ["v"], "watch": ["w"]},
}
);
const DEFAULTS = {
verbose: false,
watch: false,
in: "./custom-theme.less",
out: "./custom-theme.css",
antd: "./node_modules/antd",
theme: "default"
};
const verbose = argv["verbose"] || DEFAULTS.verbose;
const shouldWatch = argv["watch"] || DEFAULTS.watch;
const inFilePath = argv._[0] || DEFAULTS.in;
const outFilePath = argv._[1] || DEFAULTS.out;
const antdLibPath = argv["antd"] || DEFAULTS.antd;
const theme = argv["theme"] || DEFAULTS.theme;
verbose && console.debug(`Verbose::
\x1b[34m[Params]\x1b[0m
Args:
verbose: ${argv["verbose"] || ''}
watch: ${argv["watch"] || ''}
antd: ${argv["antd"] || ''}
theme: ${argv["theme"] || ''}
in: ${argv._[0] || ''}
out: ${argv._[1] || ''}
CWD: ${process.cwd()}
\x1b[34m[Vars]\x1b[0m
customTheme: ${inFilePath}
generatedTheme: ${outFilePath}
antdLib: ${antdLibPath}
theme: ${theme}
`);
const imports = [
`@import url('${antdLibPath}/lib/style/themes/${theme}.less');`,
`@import url('${antdLibPath}/dist/antd.less');`,
`@import url('${inFilePath}');`
].join('');
const compile = () => {
console.log("Generating theme...");
less.render(imports, {javascriptEnabled: true})
.then(
({css}) => {
try {
fs.writeFileSync(outFilePath, css);
console.log(`AntDesign theme (${outFilePath}) successfully generated.`);
} catch(e) {
console.error(`Could not write into file (${outFilePath}):`, e);
}
},
(error) => console.error(error)
);
};
compile();
if(shouldWatch){
const watcher = watch(inFilePath, ()=> {
console.log(`Watcher:: ${inFilePath} changed, recompiling.`);
compile();
});
const closeWatcher = () => {
console.log('Watcher:: Removing watcher...');
watcher.close();
};
process.on('SIGTERM', closeWatcher);
process.on('SIGINT', closeWatcher);
process.on('SIGQUIT', closeWatcher);
};