-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.js
269 lines (264 loc) · 10.6 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
const CONFIG = {
resources: {
name: "Shikiplayer",
version: "5.4.1",
author: "Kaneko Qt",
homepage_url: "https://github.com/qt-kaneko/Shikiplayer",
description: "Adds Kodik player to Shikimori website",
description_ru: "Добавляет плеер Kodik на сайт Shikimori",
release_notes:
`- Добавление аниме в "Смотрю" и "Пересматриваю" нужно делать вручную.
- Время просмотра и эпизод сохраняются только если аниме находится в "Смотрю" или "Пересматриваю".
- Добавлена секция со списком изменений под плеером.
- Добавлена кнопка для пожертвований под плеером.
`,
unwrapped_window: "lib/UnwrappedWindow/inject.js",
kodik_token: "447d179e875efe44217f20d1ee2146be",
poster: "//raw.github.com/qt-kaneko/Shikiplayer/main/assets/poster.jpg",
userscript: "file://dist/Shikiplayer.js"
},
esbuild: {
entry: "src/index.ts",
outFile: "Shikiplayer.js"
},
destination: "dist",
includes: {
firefox: [
["manifest-firefox.json", "manifest.json"],
"LICENSE.txt",
"_locales",
"assets/icons/icon128.png"
],
chrome: [
["manifest-chrome.json", "manifest.json"],
"LICENSE.txt",
"_locales",
"assets/icons/icon128.png",
"lib/UnwrappedWindow/inject.js"
],
userscript: ["manifest.user.js"]
}
};
"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 {
tasks.push((async () => console.log(` Copying includes -> ${config.destination}`))());
tasks.push(...includes.map(include => {
let source;
let destination;
if (typeof include === `string`)
source = destination = include;
else
[source, destination] = [include[0], include[1]];
return fsp.cp(source, config.destination + `/` + destination, { recursive: true });
}));
}
if (config.typescript && !config.esbuild) {
tasks.push((async () => {
console.log(` Compiling...`);
let command = `tsc`;
let options = [
`--project`, (config.release && config.tsconfigRelease) ? `tsconfig.release.json` : `tsconfig.json`,
`--outDir`, config.destination
];
await spawnAsync(command, options, { stdio: `inherit` }).then(exitCode => exitCode !== 0 ? Promise.reject() : Promise.resolve());
})());
}
if (config.esbuild) {
tasks.push((async () => {
console.log(` Bundling...`);
let command = `esbuild`;
let options = [
config.esbuild.entry,
`--log-level=warning`
];
if (config.esbuild?.outFile)
options.push(`--bundle`, `--outfile=${config.destination}/${config.esbuild.outFile}`);
if (!config.esbuild?.outFile)
options.push(`--outdir=${config.destination}`);
if (config.release && config.tsconfigRelease)
options.push(`--tsconfig=tsconfig.release.json`);
if (!config.release && config.tsconfig)
options.push(`--tsconfig=tsconfig.json`);
if (!config.release)
options.push(`--sourcemap`);
await spawnAsync(command, options, { stdio: `inherit` }).then(exitCode => exitCode !== 0 ? Promise.reject() : Promise.resolve());
})());
}
await Promise.all(tasks);
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 && !config.esbuild) {
if (config.tsconfig)
tasks.push(spawnAsync(`tsc`, [`--build`, `--clean`, `tsconfig.json`], { stdio: `inherit` }));
if (config.tsconfigRelease)
tasks.push(spawnAsync(`tsc`, [`--build`, `--clean`, `tsconfig.release.json`], { stdio: `inherit` }));
}
tasks.push(fsp.rm(config.destination, { recursive: true, force: true }));
console.log(` Cleaning...`);
await Promise.all(tasks);
}
async function main() {
console.time(`Elapsed`);
console.log(`Build.js`);
try {
if (typeof CONFIG === `undefined`)
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?`);
}
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`);
if (fs.existsSync(`package.json`)) {
CONFIG.npm ??= true;
CONFIG.package = JSON.parse(fs.readFileSync(`package.json`).toString());
}
else
CONFIG.npm ??= false;
if (fs.existsSync(`tsconfig.json`)) {
CONFIG.typescript ??= true;
CONFIG.tsconfig = JSON.parse(fs.readFileSync(`tsconfig.json`).toString());
if (fs.existsSync(`tsconfig.release.json`)) {
CONFIG.tsconfigRelease = JSON.parse(fs.readFileSync(`tsconfig.release.json`).toString());
}
}
else
CONFIG.typescript ??= false;
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 (`message` in (e ?? {})) {
console.log();
console.log(`\x1B[91mError: ${e.message}\x1B[0m`);
}
console.log();
console.log(`\x1B[91mBuild FAILED.\x1B[0m`);
process.exitCode = -1;
}
console.log();
console.timeEnd(`Elapsed`);
}
main().catch(reason => { throw reason; });
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;
const filePrefix = `file://`;
if (value.startsWith(filePrefix)) {
let filePath = value.slice(filePrefix.length);
if (!fs.existsSync(filePath))
continue;
let fileReplacement = await fsp.readFile(filePath);
replacement = buffer.isUtf8(fileReplacement)
? fileReplacement.toString()
: fileReplacement.toString(`base64`);
}
else
replacement = 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.esbuild) {
if (config.esbuild.entry == null) {
throw new BuildError(`Entry point was not provided for ESBuild.`);
}
}
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)));
}