-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
188 lines (187 loc) · 7.06 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const CONFIG = {
destination: "dist",
includes: ["package.json", "LICENSE.txt", "README.md", "assets/icon.png"]
};
"use strict";
const fs = require("fs");
const fsp = require("fs/promises");
class BuildError {
name = BuildError.prototype.constructor.name;
message;
constructor(message) {
this.message = message;
}
}
async function build(config) {
let tasks = [];
if (!fs.existsSync(config.destination))
fs.mkdirSync(config.destination);
let includes = (config.includes instanceof Array) ? config.includes
: config.includes[config.configuration];
if (includes.length === 0)
console.log(` Nothing to include ¯\\_(ツ)_/¯`);
else {
console.log(` Copying includes -> ${config.destination}`);
tasks.push(...includes.map(include => fsp.cp(include, config.destination + `/` + include, { recursive: true })));
}
if (config.typescript) {
console.log(` Compiling...`);
tasks.push(spawnAsync(`tsc`, [`--build`, (config.release ? `tsconfig.release.json` : `tsconfig.json`)], { stdio: `inherit` })
.then(exitCode => exitCode !== 0 ? Promise.reject() : Promise.resolve()));
}
try {
await Promise.all(tasks);
}
catch (e) {
if (e instanceof Error) {
throw new BuildError(e.message);
}
else
throw e;
}
if (tasks.length > 0) {
config.buildArtifacts = fs.readdirSync(config.destination, { recursive: true })
.map(name => config.destination + `/` + name);
}
}
async function clean(config) {
let tasks = [];
if (!config.release)
return;
if (config.typescript) {
tasks.push(spawnAsync(`tsc`, [`--build`, `--clean`, `tsconfig.json`], { stdio: `inherit` }), spawnAsync(`tsc`, [`--build`, `--clean`, `tsconfig.release.json`], { stdio: `inherit` }));
}
tasks.push(fsp.rm(config.destination, { recursive: true, force: true }));
console.log(` Cleaning...`);
try {
await Promise.all(tasks);
}
catch (e) {
if (e instanceof Error) {
throw new BuildError(e.message);
}
else
throw e;
}
}
async function main() {
console.time(`Elapsed`);
console.log(`Build.js`);
try {
if (CONFIG == null)
throw new BuildError(`Build config is not defined.`);
CONFIG.destination ??= ``;
CONFIG.includes ??= [];
CONFIG.resources ??= {};
if (!fs.existsSync(`build.js`)) {
throw new BuildError(`'build.js' was not found in working directory, are you running in correct folder?`);
}
if (fs.existsSync(`package.json`)) {
let $package = JSON.parse(fs.readFileSync(`package.json`).toString());
CONFIG.package = $package;
CONFIG.npm = true;
}
let args = process.argv.slice(2);
CONFIG.options = args.filter(arg => !arg.startsWith(`-`));
CONFIG.parameters = args.filter(arg => arg.startsWith(`-`));
CONFIG.configuration = CONFIG.options.at(0);
CONFIG.release = CONFIG.parameters.includes(`--release`);
CONFIG.typescript = fs.existsSync(`tsconfig.json`);
console.log(`Building`
+ (CONFIG.configuration != null ? ` configuration '${CONFIG.configuration}'`
: ` without configuration`)
+ ` in ${CONFIG.release ? `release` : `debug`} mode:`);
await validate(CONFIG);
await clean(CONFIG);
await restore(CONFIG);
await build(CONFIG);
await postprocess(CONFIG);
console.log();
console.log(`\x1B[32mBuild succeeded.\x1B[0m`);
}
catch (e) {
if (e instanceof BuildError) {
if (e.message !== ``) {
console.log();
console.log(`\x1B[91mError: ${e.message}\x1B[0m`);
}
console.log();
console.log(`\x1B[91mBuild FAILED.\x1B[0m`);
process.exitCode = -1;
}
else
throw e;
}
console.log();
console.timeEnd(`Elapsed`);
}
main();
const buffer = require("buffer");
async function postprocess(config) {
console.log(` Post-processing...`);
await Promise.all(config.buildArtifacts.map(async (path) => {
let stat = await fsp.stat(path);
if (!stat.isFile())
return;
let content = await fsp.readFile(path);
if (!buffer.isUtf8(content))
return;
let contentString = content.toString();
let anyReplaced = false;
for (let replaced = false;; replaced = false) {
for (let [key, value] of Object.entries(config.resources)) {
let target = `$(` + key.toUpperCase() + `)`;
let replacement = value.startsWith(`file://`)
? (await fsp.readFile(value.slice(`file://`.length))).toString()
: value;
if (!replaced && contentString.includes(target))
anyReplaced = replaced = true;
contentString = contentString.replaceAll(target, replacement);
}
if (!replaced)
break;
}
if (anyReplaced)
await fsp.writeFile(path, contentString);
}));
}
async function restore(config) {
if (!config.npm)
return;
if (fs.existsSync(`node_modules`))
return;
let dependencies = config.package[`dependencies`] ?? [];
let devDependencies = config.package[`devDependencies`] ?? [];
if (dependencies.length === 0 && devDependencies.length === 0)
return;
console.log(` Restoring...`);
if (await spawnAsync(`npm`, [`install`], { stdio: [`inherit`, `ignore`, `inherit`] }) !== 0) {
throw new BuildError(``);
}
}
async function validate(config) {
if (config.destination === ``) {
throw new BuildError(`No destination specified.`);
}
if ([`.`, `./`, `./src`, `src`].includes(config.destination)) {
throw new BuildError(`Are you sure you want to delete all your sources on build ?) (why destination includes source files?)`);
}
if (fs.existsSync(config.destination) && !fs.readdirSync(config.destination).some(name => name.endsWith(`.js`))) {
throw new BuildError(`No .js files found in '${config.destination}', is destination specified correctly?\n` +
`Remove '${config.destination}' manually if you are sure that this is correct behaviour.`);
}
if (!(config.includes instanceof Array)) {
let configurations = Object.keys(config.includes);
if (config.configuration == null) {
throw new BuildError(`Configuration option was not provided, but multiple ['${configurations.join(`', '`)}'] configuratons exists.`);
}
if (!configurations.includes(config.configuration)) {
throw new BuildError(`Configuration '${config.configuration}' does not exists in ['${configurations.join(`', '`)}'].`);
}
}
}
const cp = require("child_process");
function spawnAsync(command, args, options) {
return new Promise(resolve => cp.spawn(command, args, options)
.on(`exit`, code => resolve(code)));
}