-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
75 lines (59 loc) · 1.93 KB
/
build.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
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env node
const { babelify } = require('./babelify');
const { tscfy } = require('./tscfy');
const chalk = require('chalk');
const fs = require('fs-extra');
const log = require('npmlog');
const path = require('path');
const readPkgUp = require('read-pkg-up');
const shell = require('shelljs');
const distFolder = 'lib';
async function removeDist() {
await fs.remove(distFolder);
}
async function cleanup() {
// remove files after babel --copy-files output
if (await fs.pathExists(path.join(process.cwd(), distFolder))) {
const filesToRemove = shell.find(distFolder).filter((filePath) => {
// Remove all copied TS files (but not the .d.ts)
if (/\.tsx?$/.test(filePath) && !/\.d\.ts$/.test(filePath)) {
return true;
}
return false;
});
if (filesToRemove.length) {
shell.rm('-f', ...filesToRemove);
}
}
}
function logError(type, packageJson, errorLogs) {
log.error(`FAILED (${type}) : ${errorLogs}`);
log.error(
`FAILED to compile ${type}: ${chalk.bold(`${packageJson.name}@${packageJson.version}`)}`
);
}
async function build({ cwd, flags }) {
const modules = true;
const { packageJson } = await readPkgUp(cwd);
const message = chalk.gray(`Built: ${chalk.bold(`${packageJson.name}@${packageJson.version}`)}`);
console.time(message);
if (flags.includes('--reset')) {
await removeDist();
}
await Promise.all([
babelify({
modules,
watch: flags.includes('--watch'),
errorCallback: (errorLogs) => logError('js', packageJson, errorLogs),
}),
tscfy({
watch: flags.includes('--watch'),
errorCallback: (errorLogs) => logError('ts', packageJson, errorLogs),
}),
]);
await cleanup();
console.timeEnd(message);
}
const flags = process.argv.slice(2);
const cwd = process.cwd();
build({ cwd, flags });