-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
build.ts
185 lines (172 loc) · 4.73 KB
/
build.ts
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
import * as fsp from 'fs/promises';
import * as path from 'path';
import { build, BuildOptions, BuildResult, context, Plugin } from "esbuild";
import ImportGlobPlugin from 'esbuild-plugin-import-glob';
import { program } from 'commander';
import archiver from 'archiver';
import type { ModInfo } from './src/vscode/ModPackageProvider';
//@ts-ignore
import readdirGlob from 'readdir-glob';
function FactorioModPlugin():Plugin {
return {
name: 'factoriomod',
setup(build) {
build.onResolve({ filter: /^factoriomod:/ }, args=>{
if (args.resolveDir === '') {
return; // Ignore unresolvable paths
}
const apath = args.path.substring("factoriomod:".length);
return {
path: path.isAbsolute(apath) ? apath : path.join(args.resolveDir, apath),
namespace: 'factoriomod',
};
});
build.onLoad({ filter: /.*/, namespace: 'factoriomod' }, async (args)=>{
const packagejsonPath = path.join(process.argv[1], "../package.json");
const version = JSON.parse(await fsp.readFile(packagejsonPath, "utf8")).version;
const archive = archiver('zip', { zlib: { level: 9 }});
const templatePath = path.join(args.path, "info.template.json");
const info = <ModInfo>JSON.parse(await fsp.readFile(templatePath, "utf8"));
info.version = version;
await fsp.writeFile(path.join(args.path, "info.json"), JSON.stringify(info));
const files:string[] = [packagejsonPath, templatePath];
const globber = readdirGlob(args.path, {pattern: '**', nodir: true, ignore: ["*.template.json"]});
globber.on('match', (match:{ relative:string; absolute:string })=>{
files.push(match.absolute);
archive.file(match.absolute, { name: match.relative, prefix: `${info.name}_${info.version}` });
});
await new Promise<void>((resolve, reject)=>{
globber.on('end', ()=>{
resolve();
});
globber.on('error', (err:unknown)=>{
reject(err);
});
});
await archive.finalize();
const zip = archive.read();
return {
contents: zip,
loader: 'binary',
watchDirs: [ args.path ],
watchFiles: files,
};
});
},
};
}
class Watcher {
private activeBuilds = 0;
onStart() {
if (this.activeBuilds++ === 0) {
console.log("[watch] build started");
}
}
onEnd(result:BuildResult) {
result.errors.forEach((error)=>{
console.error(`> ${error.location?.file}:${error.location?.line}:${error.location?.column}: error: ${error.text}`);
});
if (--this.activeBuilds === 0) {
console.log("[watch] build finished");
}
}
plugin():Plugin {
const _this = this;
return {
name: 'watcher',
setup(build) {
build.onStart(()=>{ return _this.onStart(); });
build.onEnd((result)=>{ return _this.onEnd(result); });
},
};
}
}
const commonConfig:BuildOptions = {
tsconfig: "./tsconfig.json",
bundle: true,
outdir: "dist",
//logLevel: "info",
sourcemap: true,
sourcesContent: false,
};
const mainConfig:BuildOptions = {
...commonConfig,
entryPoints: {
fmtk: "./src/fmtk.ts",
},
external: [
"vscode",
],
loader: {
".html": "text",
".lua": "text",
},
platform: "node",
format: "cjs",
// `module` first for jsonc-parser
mainFields: ['module', 'main'],
plugins: [
ImportGlobPlugin(),
FactorioModPlugin(),
],
};
const webviewConfig:BuildOptions = {
...commonConfig,
entryPoints: {
Flamegraph: "./src/Profile/Flamegraph.ts",
ModSettingsWebview: "./src/ModSettings/ModSettingsWebview.ts",
ScriptDatWebview: "./src/ScriptDat/ScriptDatWebview.ts",
},
external: [
"vscode-webview",
],
loader: {
".ttf": "copy",
},
platform: "browser",
format: "esm",
plugins: [
],
};
program
.option("--watch")
.option("--meta")
.option("--minify")
.action(async (options:{watch?:boolean; meta?:boolean; minify?:boolean})=>{
if (options.watch) {
const watcher = new Watcher();
mainConfig.plugins!.push(watcher.plugin());
webviewConfig.plugins!.push(watcher.plugin());
}
const optionsConfig:BuildOptions = {
metafile: options.meta,
minify: options.minify,
};
if (options.watch) {
const contexts = await Promise.all([
context({
...mainConfig,
...optionsConfig,
}),
context({
...webviewConfig,
...optionsConfig,
}),
]);
await Promise.all(contexts.map(c=>c.watch()));
} else {
const result = await Promise.all([
build({
...mainConfig,
...optionsConfig,
}),
build({
...webviewConfig,
...optionsConfig,
}),
]);
if (options.meta) {
await Promise.all(result.map((result, i)=>fsp.writeFile(`./out/meta_${i}.json`, JSON.stringify(result.metafile))));
}
}
}).parseAsync();