-
Notifications
You must be signed in to change notification settings - Fork 7
/
build.js
70 lines (62 loc) · 1.68 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
"use strict";
import ChildProcess from "child_process";
import Fs from "fs";
class BuildError extends Error {};
// Executes a shell command line.
// An error is thrown when the exit code of the command is not 0.
function shell (cmdLine) {
ChildProcess.execSync(cmdLine, {stdio: "inherit"});
}
function copyToDist (fileNames) {
for (const fileName of fileNames) {
Fs.copyFileSync(fileName, "dist/" + fileName);
}
}
function delDir (dirName) {
Fs.rmSync(dirName, {recursive: true, force: true});
}
function main2() {
const argv = process.argv;
if (argv.length > 3) {
throw new BuildError("Extra command line parameters.");
}
let cmd = (argv.length > 2) ? argv[2] : "build";
switch (cmd) {
case "clean": {
delDir("dist");
break;
}
case "build": {
delDir("dist");
shell("tsc");
shell("eslint --ext .ts src/**");
copyToDist([".npmignore", "LICENSE.md", "README.md", "NOTICE.md", "package.json", "build.js"]);
console.log("Build completed.");
break;
}
case "verifyCurrentDirIsDist": {
if (!process.cwd().endsWith("dist")) {
console.log("Current directory is: " + process.cwd());
throw new BuildError("*** NPM pack/publish must be run in the dist directory! ***");
}
break;
}
default: {
throw new BuildError(`Invalid command parameter "${cmd}".`);
}
}
}
function main() {
try {
main2();
} catch (e) {
if (e instanceof BuildError) {
console.log(e.message);
} else {
console.log(e.toString());
}
process.exitCode = 99;
return;
}
}
main();