-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.cjs
50 lines (43 loc) · 1.83 KB
/
build.cjs
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
const fs = require('fs');
const path = require('path');
const childProcess = require('child_process');
fs.rmSync(path.join(process.cwd(), 'dist'), {force: true, recursive: true});
const spawn = (command, args) => {
return new Promise((resolve, reject) => {
const process = childProcess.spawn(command, args, {stdio: 'inherit', shell: true});
process.once('error', err => {
process.removeAllListeners('exit');
reject(err);
});
process.once('exit', (code, signal) => {
process.removeAllListeners('error');
if (code === 0) {
resolve();
} else {
reject(`Process exited with code ${code}, signal ${signal}`);
}
});
});
};
async function main() {
const promises = [];
for (const {moduleType, packageModuleType} of [
{moduleType: 'cjs', packageModuleType: 'commonjs'},
{moduleType: 'mjs', packageModuleType: 'module'}
]) {
promises.push((async() => {
await spawn('npm', ['exec', '--offline', '--', path.join(__dirname, 'node_modules/.bin/tsc'), '-p', `tsconfig-${moduleType}.json`, '--outDir', `./dist/${moduleType}`]);
await fs.promises.mkdir(path.join(process.cwd(), 'dist', moduleType), {recursive: true});
await fs.promises.writeFile(
path.join(process.cwd(), 'dist', moduleType, 'package.json'),
JSON.stringify({type: packageModuleType}, null, 2)
);
})());
}
promises.push((async() => {
await fs.promises.mkdir(path.join(process.cwd(), 'dist/types'), {recursive: true});
await spawn(path.join(__dirname, 'node_modules/.bin/tsc'), ['-p', `tsconfig-mjs.json`, '-d', '--emitDeclarationOnly', '--outDir', './dist/types']);
})());
await Promise.all(promises);
}
main();