-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
64 lines (53 loc) · 1.87 KB
/
scripts.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
/* Auxiliary scripts for use in package.json scripts */
const fs = require('fs');
const path = require('path');
const PACKAGE = require(path.join(__dirname, 'package.json'));
const banner = [
`/**`,
` * ${PACKAGE.name}`,
` * @version ${PACKAGE.version}`,
` * @link ${PACKAGE.homepage}`,
` * @license ${PACKAGE.license}`,
` */`,
//
].join('\n');
function removeFiles(...paths) {
for (const path of paths) {
fs.rmSync(path, { recursive: true, force: true });
}
}
function addBanner(file) {
if (!fs.existsSync(file)) return;
const contents = [banner, fs.readFileSync(file, { encoding: 'utf8' })];
fs.writeFileSync(file, contents.join('\n\n'), { encoding: 'utf8' });
}
const scripts = {
/** Add a banner with version info to the built files */
'add-banners': () => {
addBanner(`build/${PACKAGE.name}.js`);
addBanner(`build/${PACKAGE.name}.js.LICENSE.txt`);
addBanner(`build/${PACKAGE.name}.css`);
},
/** Move a file */
'mv': (src, dest) => {
if (src === undefined) throw new Error('`src` parameter missing');
if (dest === undefined) throw new Error('`dest` parameter missing');
console.log('Moving file:', src, '->', dest);
fs.renameSync(src, dest);
},
/** Remove files */
'rm': (...paths) => {
console.log('Removing files:', paths);
removeFiles(...paths);
},
};
// Parse command-line arguments
const [_node, _file, scriptName, ...params] = process.argv;
// Check if script name is provided and valid
if (!(scriptName in scripts)) {
console.error(`usage: scripts.js [-h] {${Object.keys(scripts).join(',')}} [params ...]`);
console.error(`scripts.js: error: argument script_name: invalid choice: ${scriptName} (choose from ${Object.keys(scripts).join(', ')})`);
process.exit(2);
}
// Run selected script
scripts[scriptName](...params);