forked from BaliBalo/ScoreSaber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compress.js
44 lines (40 loc) · 1.19 KB
/
compress.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
const { spawn } = require('child_process');
async function run(command) {
return new Promise((resolve, reject) => {
const script = spawn('bash', ['-c', command]);
script.stdout.on('data', data => console.log(data));
script.stderr.on('data', data => console.error(data));
script.on('close', code => {
if (!code) {
return resolve();
}
reject(code);
});
});
}
const filePattern = 'pages/*.js';
const command = (file) => 'npx terser "' + file + '" --compress --mangle -o "client/$(basename "' + file + '" .js).min.js"';
(async () => {
if (!process.argv.includes('--watch')) {
try {
await run('for f in ' + filePattern + '; do ' + command('$f') + '; done');
} catch(code) {
console.log('Error - exit code:', code);
return process.exit(code);
}
console.log('All JS files compressed!');
return process.exit();
}
// Watch mode
const chokidar = require('chokidar');
let recompile = async file => {
try {
await run(command(file));
console.log('Recompiled ' + file);
} catch(code) {
console.log('Error compiling ' + file + ' - code:', code);
}
};
chokidar.watch(filePattern).on('change', recompile).on('add', recompile);
console.log('Watching JS files!');
})();