-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
process_static.js
107 lines (97 loc) · 2.72 KB
/
process_static.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
import esbuild from "esbuild";
import { spawn } from "node:child_process";
import fs from "node:fs/promises";
import path from "node:path";
/**
* @typedef ProcessOptions
* @property {string} rootDir
* @property {string[]} entryPoints
* @property {string} outDir
* @property {boolean} production
*/
async function processStatic() {
const rootDir = "viewer";
const outDir = "app/src/main/assets/viewer";
const outDirDebug = "app/src/debug/assets/viewer";
await commandLine(getCommand("node_modules/.bin/eslint"), ".");
await processScripts({
rootDir,
entryPoints: ["js/index.js", "js/worker.js"],
outDir,
production: true,
});
await processScripts({
rootDir,
entryPoints: ["js/index.js", "js/worker.js"],
outDir: outDirDebug,
production: false,
});
await processStyles({
rootDir,
entryPoints: ["main.css"],
outDir,
production: true,
});
await processHtml({
rootDir,
entryPoints: ["index.html"],
outDir,
production: true,
});
}
/**
* @param {string} command
* @param {string} winExt
* @returns {string}
*/
function getCommand(command, winExt = "cmd") {
return path.resolve(globalThis.process.platform === "win32" ? `${command}.${winExt}` : command);
}
/**
* @param {string} command
* @param {...string} args
* @returns {Promise<void>}
*/
function commandLine(command, ...args) {
return new Promise((resolve, reject) => {
const subprocess = spawn(command, args, { shell: false, stdio: "inherit" });
subprocess.on("close", (code) => code === 0 ? resolve() : reject());
});
}
/**
* @param {ProcessOptions} options
*/
async function processScripts(options) {
const entryPoints = options.entryPoints.map((filepath) => path.join(options.rootDir, filepath));
await esbuild.build({
entryPoints,
bundle: true,
format: "esm",
platform: "browser",
target: "es2022",
outdir: path.join(options.outDir, "js"),
minify: options.production,
sourcemap: options.production ? false : "inline",
});
}
/**
* @param {ProcessOptions} options
*/
async function processStyles(options) {
const entryPoints = options.entryPoints.map((filepath) => path.join(options.rootDir, filepath));
await esbuild.build({
entryPoints,
bundle: true,
outdir: options.outDir,
minify: options.production,
});
}
/**
* @param {ProcessOptions} options
*/
async function processHtml(options) {
for (const entryPoint of options.entryPoints) {
await fs.copyFile(path.join(options.rootDir, entryPoint), path.join(options.outDir, entryPoint));
}
}
await processStatic();