This repository has been archived by the owner on Jan 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.mjs
72 lines (65 loc) · 2.29 KB
/
build.mjs
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
import { promises as FS } from 'fs';
import * as Path from 'path';
import {minify as Minify} from 'terser';
import {Settings} from './settings.mjs';
import {Formatters} from './build-formatters.mjs';
Promise.resolve()
.then(() => Settings.clearTerminal())
.then(() => Settings.printBorder())
.then(() => console.log.bold.white(`Building MVW ${Settings.package.version}:\n`))
.then(() => clean())
.then(() => buildAll())
.then(() => console.log.bold.green(`Build Complete!`))
.catch(e => {
console.log.bold.red(`Build Failed:`);
console.error(e);
})
.then(() => Settings.printBorder());
async function clean() {
console.log.light(`Cleaning Debug Folder...`);
await FS.rm(Settings.debugOutput, {recursive: true, force: true});
await FS.mkdir(Settings.debugOutput, {recursive: true});
console.log.light(`Cleaning Build Folder...`);
await FS.rm(Settings.buildOutput, {recursive: true, force: true});
await FS.mkdir(Settings.buildOutput, {recursive: true});
console.log('');
}
async function buildAll() {
for (var config of Settings.buildConfigs) {
await build(config.name, config.sources);
console.log('');
}
}
async function build(name, sources) {
console.log.yellow(`Building '${name}'...`);
var document = '';
for (var source of sources) {
var items = [source];
while (items.length) {
var item = items.shift();
if ((await FS.lstat(item)).isFile()) {
var code = await FS.readFile(item, 'utf8');
document += Formatters.formatModule(item, code);
} else {
items.push(
...(await FS.readdir(item))
.map(fname => Path.join(item, fname))
);
}
}
}
name = `${Settings.package.name}.${name}`;
var debugFN = `${name}.js`;
var debugPT = Path.join(Settings.debugOutput, debugFN);
var debug = Formatters.formatBuild(debugPT, document);
await FS.writeFile(debugPT, debug);
console.log.bold.green(`Writing ${debugPT}:\t${debug.length} bytes`);
for (var profile of Settings.buildProfiles) {
var codeFN = `${name}${profile.extension}`;
var codePT = Path.join(Settings.buildOutput, codeFN);
var code = (await Minify(debug, profile.settings)).code;
console.log.bold.green(`Writing ${codePT}:\t${code.length} bytes`);
await FS.writeFile(codePT, code);
}
console.log('');
}