Files
Life-OS/scripts/build.mjs
T

23 lines
2.4 KiB
JavaScript

import { mkdir, readFile, rm, writeFile, copyFile } from "node:fs/promises";
const text = (file) => readFile(new URL(`../public/${file}`, import.meta.url), "utf8");
const dist = new URL("../dist/", import.meta.url);
const server = new URL("../dist/server/", import.meta.url);
const meta = new URL("../dist/.openai/", import.meta.url);
await rm(dist, { recursive: true, force: true });
await Promise.all([mkdir(server, { recursive: true }), mkdir(meta, { recursive: true })]);
const [rawIndexHtml, addonsJs, bootGuardJs, i18nJs, tasksJs, calendarJs, i18nRenderersJs, manifest, serviceWorker, icon] = await Promise.all([text("index.html"), text("addons.js"), text("boot-guard.js"), text("i18n.js"), text("tasks.js"), text("calendar.js"), text("i18n-renderers.js"), text("manifest.webmanifest"), text("service-worker.js"), text("life-os-icon.svg")]);
const release = "1.4.2";
// The original single-file prototype carried an obsolete English-to-Chinese
// DOM replacement block. Runtime localisation now lives in i18n.js and
// i18n-renderers.js, so exclude that dead block from the deployed document.
const withoutLegacyI18n = rawIndexHtml
.replace(/const ZH=.*?function browserLanguage/s, 'function browserLanguage')
.replace(/function localizeDynamicText.*?function i18n\(key,fallback=key\)\{return lang\(\)===\'zh\'\?\(ZH\[key\]\|\|key\):fallback\}/s, 'function i18n(key,fallback=key){return fallback}');
const indexHtml = withoutLegacyI18n.replace('src="addons.js"', `src="addons.js?v=${release}"`).replace('src="tasks.js"', `src="tasks.js?v=${release}"`).replace('src="calendar.js"', `src="calendar.js?v=${release}"`);
await writeFile(new URL("assets.js", server), `export const indexHtml=${JSON.stringify(indexHtml)};\nexport const addonsJs=${JSON.stringify(addonsJs)};\nexport const bootGuardJs=${JSON.stringify(bootGuardJs)};\nexport const i18nJs=${JSON.stringify(i18nJs)};\nexport const tasksJs=${JSON.stringify(tasksJs)};\nexport const calendarJs=${JSON.stringify(calendarJs)};\nexport const i18nRenderersJs=${JSON.stringify(i18nRenderersJs)};\nexport const manifest=${JSON.stringify(manifest)};\nexport const serviceWorker=${JSON.stringify(serviceWorker)};\nexport const icon=${JSON.stringify(icon)};\n`);
await copyFile(new URL("../worker/index.js", import.meta.url), new URL("index.js", server));
await copyFile(new URL("../.openai/hosting.json", import.meta.url), new URL("hosting.json", meta));
console.log("Built Life OS Worker artifact");