-
Notifications
You must be signed in to change notification settings - Fork 11
/
build.mjs
76 lines (62 loc) · 2.2 KB
/
build.mjs
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
/* eslint-disable @typescript-eslint/no-unsafe-argument, @typescript-eslint/naming-convention, no-underscore-dangle */
import colors from '@colors/colors/safe.js';
import { existsSync, mkdirSync, rmSync, readFileSync, writeFileSync } from 'fs';
import { dirname, resolve as pathResolve } from 'path';
import { fileURLToPath } from 'url';
import { exec } from 'child_process';
import cpy from 'cpy';
const { green, magenta } = colors;
const __dirname = dirname(fileURLToPath(import.meta.url));
const DIST_PATH = pathResolve(__dirname, './dist');
const log = str => console.log(magenta(str));
const execCmd = (cmd, opts) => new Promise((resolve, reject) => {
exec(cmd, opts, (err, stdout, stderr) => {
if (err) {
console.error(stdout, stderr);
return reject(err);
}
return resolve(stdout);
});
});
const cleanDir = path => new Promise(resolve => {
const exists = existsSync(path);
if (exists) {
rmSync(path, { recursive: true, cwd: __dirname });
}
// Gives time to rmSync to unlock the file on Windows
setTimeout(() => {
mkdirSync(path, { recursive: true, cwd: __dirname });
resolve();
}, exists ? 1000 : 0);
});
const copyAssets = async () => {
await cpy('README.md', DIST_PATH, { flat: true });
await cpy('LICENSE', DIST_PATH, { flat: true });
await cpy('package.json', DIST_PATH, { flat: true });
};
const customizePackageJson = () => {
const pkgJsonPath = pathResolve(DIST_PATH, 'package.json');
const pkgJson = JSON.parse(readFileSync(pkgJsonPath, { encoding: 'utf8' }));
delete pkgJson.scripts;
delete pkgJson.devDependencies;
writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 4), { encoding: 'utf8' });
};
const build = async () => {
log('> Cleaning..');
await cleanDir(DIST_PATH);
log('> Building library..');
await execCmd('tsc --project tsconfig.json', { cwd: __dirname });
log('> Copying assets..');
await copyAssets();
log('> Customizing package.json..');
customizePackageJson();
log(`> ${green('Done!')}\n`);
};
void (async () => {
try {
await build();
} catch (err) {
console.error(err);
process.exit(1);
}
})();