-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.js
68 lines (55 loc) · 2.15 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
const fs = require("fs").promises;
const exec = require('child_process').exec;
const version = process.argv[2];
if (!version) {
console.error("Must provide a version as the first argument");
return;
}
async function bumpVersion(fileName, fnc) {
console.info("Updating " + fileName);
const package = JSON.parse(await fs.readFile(fileName, "utf8"));
fnc(package);
await fs.writeFile(fileName, JSON.stringify(package, null, 2));
}
function execCommand(command) {
return new Promise((resolve, reject) => {
const process = exec(command, { stdio: ['inherit', 'inherit', 'pipe'] });
let stderr = '';
process.stderr.on('data', (data) => {
stderr += data.toString();
});
process.on('exit', (code) => {
if (code === 0) {
resolve();
} else {
reject(new Error(`Command "${command}" exited with code ${code}\n${stderr}`));
}
});
process.on('error', (error) => {
reject(error);
});
});
}
(async () => {
const originalNodeEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'production';
try {
await bumpVersion("package.json", data => data.version = version);
await bumpVersion("package-lock.json", data => data.packages[""].version = data.version = version);
console.info("Executing TypeScript ES2020 build");
await execCommand("tsc --project tsconfig.es2020.json");
console.info("Executing rollup");
await execCommand("./node_modules/rollup/dist/bin/rollup -c --bundleConfigAsCjs");
console.info("Renaming files");
await fs.rename("vidyano.js", `vidyano.es2020.js`);
await fs.rename("vidyano.d.ts", `vidyano.es2020.d.ts`);
console.info("Executing TypeScript build");
await execCommand("tsc --project tsconfig.json");
console.info("Executing rollup");
await execCommand("./node_modules/rollup/dist/bin/rollup -c --bundleConfigAsCjs");
} catch (error) {
console.error("Error during execution:", error);
} finally {
process.env.NODE_ENV = originalNodeEnv;
}
})();