Repository navigation
Expand file tree
/
Copy pathbuild.mjs
More file actions
43 lines (36 loc) · 2.26 KB
/
Copy pathbuild.mjs
File metadata and controls
43 lines (36 loc) · 2.26 KB
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
/* Build script: inline src/app.css + src modules into a single self-contained
* nephrotdm.html artifact (works offline from file://, no dependencies). */
import { readFileSync, writeFileSync } from 'fs';
import { resolve, dirname } from 'path';
import { fileURLToPath } from 'url';
const root = dirname(fileURLToPath(import.meta.url));
const FILES = ['src/version.js', 'src/model.js', 'src/tacrolimus.js', 'src/everolimus.js', 'src/tacped.js', 'src/mpaped.js', 'src/bayes.js', 'src/parallel.js', 'src/chart.js', 'src/diagnostics.js', 'src/texts_tac.js', 'src/texts_evr.js', 'src/texts_tacped.js', 'src/texts_mpaped.js', 'src/author_photo.js', 'src/report.js', 'src/visits.js', 'src/ui.js'];
// Central version check: src/version.js is the single source of truth; keep
// package.json in step with it (the build warns loudly if they drift).
const pkg = JSON.parse(readFileSync(resolve(root, 'package.json'), 'utf8'));
const verSrc = readFileSync(resolve(root, 'src/version.js'), 'utf8');
const verMatch = verSrc.match(/ECU\.VERSION\s*=\s*'([^']+)'/);
const appVersion = verMatch ? verMatch[1] : '0.0.0';
if (pkg.version !== appVersion) {
console.warn('WARNING: package.json version ' + pkg.version + ' does not match src/version.js ' + appVersion);
}
let html = readFileSync(resolve(root, 'index.html'), 'utf8');
for (const sheet of ['fonts', 'app']) {
const css = readFileSync(resolve(root, 'src/' + sheet + '.css'), 'utf8');
html = html.replace(new RegExp('<link rel="stylesheet" href="src/' + sheet + '\\.css">'),
() => '<style>\n/* ' + sheet + '.css (inlined) */\n' + css + '\n</style>');
}
for (const f of FILES) {
const js = readFileSync(resolve(root, f), 'utf8');
const marker = new RegExp('<script src="' + f.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + '"></script>');
if (!marker.test(html)) {
console.error('Missing script tag for ' + f);
process.exit(1);
}
html = html.replace(marker, () => '<script>\n/* ' + f + ' (inlined) */\n' + js + '\n</script>');
}
const stamp = new Date().toISOString().replace('T', ' ').slice(0, 16) + ' UTC';
html = html.replace(/__BUILD__/, stamp);
const out = resolve(root, 'nephrotdm.html');
writeFileSync(out, html);
console.log('Built ' + out + ' (v' + appVersion + ', ' + (html.length / 1024).toFixed(1) + ' KB, built ' + stamp + ')');