-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbuild.js
More file actions
68 lines (61 loc) · 1.4 KB
/
build.js
File metadata and controls
68 lines (61 loc) · 1.4 KB
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
const os = require('os');
const cp = require('child_process');
const TAG = '[build]';
const isdev = process.argv.slice(2).includes('--watch');
; (async () => {
if (isdev) {
dev();
} else {
const status = await build();
if (status.every(e => !e)) {
console.log(TAG, 'build success');
}
}
})();
function dev() {
stdio(error(run('dev:es')));
stdio(error(run('dev:cjs')));
}
/**
* @type {() => Promise<(number | null)[]>}
*/
function build() {
return new Promise(resolve => {
const status = [undefined, undefined];
const done = () => {
if (status.every(e => e !== undefined)) {
resolve(status);
}
};
stdio(error(run('build:es'))).on('exit', code => {
status[0] = code;
done();
});
stdio(error(run('build:cjs'))).on('exit', code => {
status[1] = code;
done();
});
});
}
function run(args = []) {
const npm = os.platform() === 'win32' ? 'npm.cmd' : 'npm';
return cp.spawn(npm, ['run'].concat(args));
}
/**
* @type {(cp: import('child_process').ChildProcessWithoutNullStreams) => typeof cp}
*/
function stdio(cp) {
cp.stdout.pipe(process.stdout);
cp.stderr.pipe(process.stderr);
return cp;
}
/**
* @type {(cp: import('child_process').ChildProcessWithoutNullStreams) => typeof cp}
*/
function error(cp) {
cp.on('error', error => {
console.error(TAG, error);
process.exit(1);
});
return cp;
}