gitworld / scripts /build.mjs
SNAPKITTYWEST's picture
push from SNAPKITTYWEST/gitworld
6281708 verified
Raw
History Blame Contribute Delete
1.86 kB
import { cp, mkdir, readFile, writeFile } from 'node:fs/promises';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
const root = dirname(dirname(fileURLToPath(import.meta.url)));
const dist = join(root, 'dist');
const referenceSource = join(root, 'phase2-unreal', 'ReferenceSource');
const publishedReferenceSource = join(dist, 'phase2-unreal', 'ReferenceSource');
await mkdir(dist, { recursive: true });
await cp(referenceSource, publishedReferenceSource, { recursive: true, force: true });
await copyIfChanged(join(root, 'styles.css'), join(dist, 'styles.css'));
await copyIfChanged(join(root, 'src', 'main.ts'), join(dist, 'main.js'));
for (const moduleName of ['shared.js', 'world-architect.js', 'ui-reality.js']) {
await copyIfChanged(join(root, 'src', moduleName), join(dist, moduleName));
}
const builtHtml = (await BunlessHtml(await import('node:fs/promises')))
.replace('./src/main.ts', './main.js');
await writeIfChanged(join(dist, 'index.html'), builtHtml);
console.log(`GitWorld build ready: ${dist}`);
async function copyIfChanged(source, destination) {
if (await filesMatch(source, destination)) {
return;
}
await cp(source, destination);
}
async function writeIfChanged(destination, contents) {
try {
if ((await readFile(destination, 'utf8')) === contents) {
return;
}
} catch {
// The destination does not exist yet.
}
await writeFile(destination, contents, 'utf8');
}
async function filesMatch(left, right) {
try {
const [leftContents, rightContents] = await Promise.all([
readFile(left),
readFile(right)
]);
return leftContents.equals(rightContents);
} catch {
return false;
}
}
async function BunlessHtml(fs) {
return fs.readFile(join(root, 'index.html'), 'utf8');
}