summaryrefslogtreecommitdiff
path: root/ofborg/ofborg-viewer/build.js
blob: bcdcbc889f5560a6d54eb756fdd18c23a9e940c2 (plain)
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
#!/usr/bin/env node
/**
 * Build script for tickborg-viewer.
 * Replaces ancient webpack 3 + Node 6 setup with esbuild.
 */
const { execSync } = require("child_process");
const fs = require("fs");
const path = require("path");

const ROOT = __dirname;
const DIST = path.join(ROOT, "dist");

// Ensure dist/
if (!fs.existsSync(DIST)) {
    fs.mkdirSync(DIST, { recursive: true });
}

// 1. Get git revision + version
let GIT_REVISION = "";
const revFile = path.join(ROOT, ".git-revision");
if (fs.existsSync(revFile)) {
    GIT_REVISION = fs.readFileSync(revFile, "utf-8").trim();
} else {
    try {
        GIT_REVISION = execSync("git rev-parse --short HEAD", { cwd: ROOT, encoding: "utf-8" }).trim();
    } catch (e) {
        // ignore
    }
}
const VERSION = JSON.parse(fs.readFileSync(path.join(ROOT, "package.json"), "utf-8")).version;

// 2. Compile LESS → CSS
console.log("Compiling LESS...");
const lessFile = path.join(ROOT, "src/styles/index.less");
const cssOutput = execSync(`npx lessc ${lessFile}`, { encoding: "utf-8" });

// 3. Build JS bundle with esbuild
console.log("Bundling JS...");
execSync(
    `npx esbuild src/index.js --bundle --outfile=dist/bundle.js --format=iife --target=es2018 --minify ` +
    `--define:GIT_REVISION='"${GIT_REVISION}"' --define:VERSION='"${VERSION}"'`,
    { cwd: ROOT, stdio: "inherit" }
);

// 4. Generate index.html
console.log("Generating HTML...");
const html = `<!DOCTYPE html>
<html>
<head>
    <meta name="theme-color" content="#AFFFFF">
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta charset="utf-8">
    <title>TickBorg Log Viewer</title>
    <style>
${cssOutput}
    </style>
</head>
<body>
    <div id="tickborg-logviewer">
        <div class="app">
            <div class="loading">
                <strong>Loading...</strong>
                <em>you may need to enable some JavaScript for this to work.</em>
            </div>
        </div>
    </div>
    <script src="bundle.js"></script>
</body>
</html>`;

fs.writeFileSync(path.join(DIST, "index.html"), html);
console.log(`Build complete → ${DIST}/`);