#!/usr/bin/env node
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const ROOT = path.dirname(fileURLToPath(import.meta.url));
const DEST = path.join(ROOT, 'public');
const DATA_FILES = ['quark_weights.json', 'quark_weights.bin', 'tokenizer.json', 'reference.json', 'corpus.json'];
const REQUIRED = [
'index.html',
'css/style.css',
'js/main.js',
'lib/katex/katex.min.css',
'lib/katex/katex.min.js',
'data/quark_weights.json',
'data/quark_weights.bin',
'data/tokenizer.json',
'data/reference.json',
];
const log = (s) => console.log(s);
const fail = (msg) => { console.error('\n[FAILED] ' + msg); process.exit(1); };
const rel = (p) => path.relative(ROOT, p);
const exists = (p) => fs.existsSync(p);
function rmSyncRecursive(p) {
if (!fs.existsSync(p)) return;
for (const e of fs.readdirSync(p, { withFileTypes: true })) {
const c = path.join(p, e.name);
if (e.isDirectory()) rmSyncRecursive(c);
else fs.unlinkSync(c);
}
}
if (!exists(path.join(ROOT, 'index.html'))) {
fail('index.html not found next to this script. Keep this file in the repository root.');
}
rmSyncRecursive(DEST);
for (const d of [
DEST,
path.join(DEST, 'css'),
path.join(DEST, 'js'),
path.join(DEST, 'lib', 'katex', 'fonts'),
path.join(DEST, 'data'),
]) {
fs.mkdirSync(d, { recursive: true });
}
function copyFile(relPath) {
const src = path.join(ROOT, relPath);
const dst = path.join(DEST, relPath);
if (!exists(src)) { console.log(' [warn] missing source, skipped: ' + relPath); return false; }
fs.copyFileSync(src, dst);
return true;
}
function copyDir(srcRel, dstRel = srcRel) {
const src = path.join(ROOT, srcRel);
const dst = path.join(DEST, dstRel);
if (!exists(src)) { console.log(' [warn] missing source dir, skipped: ' + srcRel); return; }
fs.mkdirSync(dst, { recursive: true });
for (const e of fs.readdirSync(src, { withFileTypes: true })) {
const s = path.join(src, e.name);
const d = path.join(dst, e.name);
if (e.isDirectory()) copyDir(path.join(srcRel, e.name), path.join(dstRel, e.name));
else fs.copyFileSync(s, d);
}
}
console.log('============================================================');
console.log(' public - build the EdgeOne deploy folder');
console.log(' source : ' + rel(ROOT) + '/');
console.log(' target : ' + rel(DEST) + '/');
console.log('============================================================');
console.log('');
copyFile('index.html');
copyDir('css');
copyDir('js');
copyFile('lib/katex/katex.min.css');
copyFile('lib/katex/katex.min.js');
copyDir('lib/katex/fonts');
for (const f of DATA_FILES) copyFile(path.join('data', f));
let missing = 0;
for (const r of REQUIRED) {
if (!exists(path.join(DEST, r))) { console.log(' [MISSING] ' + r); missing++; }
}
if (missing) fail('required files are missing -- the deploy folder is incomplete.');
log(' all 9 required entry files present [OK]');
const topFiles = (dir) => fs.readdirSync(dir, { withFileTypes: true }).filter((e) => e.isFile()).map((e) => e.name);
const strayRoot = topFiles(DEST).filter((f) => f !== 'index.html');
if (strayRoot.length) fail('unexpected file(s) at deploy root: ' + strayRoot.join(', '));
const strayData = topFiles(path.join(DEST, 'data')).filter((f) => !DATA_FILES.includes(f));
if (strayData.length) fail('unexpected file(s) in data/: ' + strayData.join(', '));
const katexDir = path.join(DEST, 'lib', 'katex');
const strayKatex = fs.readdirSync(katexDir, { withFileTypes: true }).filter(
(e) => !((e.isFile() && (e.name === 'katex.min.css' || e.name === 'katex.min.js')) || (e.isDirectory() && e.name === 'fonts')),
);
if (strayKatex.length) fail('unexpected entry in lib/katex/: ' + strayKatex.map((e) => e.name).join(', '));
log(' no stray files [OK]');
function countFiles(dir) {
let n = 0;
for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
const p = path.join(dir, e.name);
if (e.isDirectory()) n += countFiles(p);
else n++;
}
return n;
}
const N = countFiles(DEST);
log('');
log('[DONE] deploy folder is ready: ' + N + ' files');
log(' ' + rel(DEST) + '/');
log(' Upload that folder as-is. No config file needed for a static site.');