-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-cli.js
116 lines (105 loc) · 2.81 KB
/
build-cli.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { execSync } from "child_process";
import fs from "fs";
import path from "path";
let args = process.argv;
args.splice(0, 2);
const __dirname = path.resolve();
const command = args[0].toLowerCase().trim();
let exitCode;
switch (command) {
case "build":
exitCode = build();
break;
case "test":
exitCode = test();
break;
case "publish":
exitCode = publish();
break;
default:
console.error(`Unknown command ${command}`);
exitCode = 1;
break;
}
process.exit(exitCode);
function build() {
const testResponse = test();
if (testResponse !== 0) {
//throw new Error(`Build failed because test execution failed!`);
}
console.log(`Building...`);
let opts;
opts = {
cwd: `${__dirname}`,
stdio: [0, 1, 2],
};
opts = {
cwd: `${__dirname}`,
stdio: [0, 1, 2],
};
try {
execSync(`tsc`, opts);
} catch (err) {
console.log("ec", exitCode);
console.error(`Error building project, check output above!`);
return 1;
}
opts = {
cwd: __dirname,
stdio: [0, 1, 2],
};
console.log(`Updating package.json version and copying package.json.`);
try {
const jsonFile = `${__dirname}\\package.json`;
let packageJsonStr = fs.readFileSync(jsonFile, {
encoding: "utf-8",
});
const packageJson = JSON.parse(packageJsonStr);
const version = (packageJson.version || "1.0.0").split(".");
version[2] = parseInt(version[2]) + 1;
packageJson.version = version.join(".");
console.log(`New package.json version is ${packageJson.version}.`);
packageJsonStr = JSON.stringify(packageJson, null, 4);
fs.writeFileSync(jsonFile, packageJsonStr);
execSync(`node node_modules/cpy-cli/cli.js package.json dist`, opts);
execSync(`node node_modules/cpy-cli/cli.js readme.md dist`, opts);
} catch (err) {
console.error(`Error copying package.json / readme.md!`, err);
return 1;
}
return 0;
}
function test() {
console.log(`Testing...`);
let opts;
opts = {
cwd: `${__dirname}`,
stdio: [0, 1, 2],
};
try {
execSync(`npm run test:coverage`, opts);
} catch (err) {
console.error(`Error running tests, check output above!`);
return 1;
}
return 0;
}
function publish() {
console.log(`Publishing...`);
const buildCode = build();
if (buildCode !== 0) {
return buildCode;
}
let opts;
opts = {
cwd: `${__dirname}\\dist`,
stdio: [0, 1, 2],
};
try {
execSync(`npm publish --access public`, opts);
} catch (err) {
console.error(`Error publishing, check output above!`);
return 1;
}
return 0;
}