Files
final-wishes/build.js

83 lines
3.6 KiB
JavaScript

// Builds the single self-contained final-wishes.html.
//
// node build.js app only, no data (open/import a JSON file in the page)
// node build.js --data data/seed.json also encrypt that JSON and embed it
// node build.js --data X --out /path/final-wishes.html
//
// The passphrase is read from the FW_PASSPHRASE environment variable, or
// prompted for (hidden, entered twice). It is never accepted as an argument.
import { readFileSync, writeFileSync, mkdirSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { dirname, join, resolve } from 'node:path';
import { assemble } from './src/template.js';
import { encrypt, passphraseProblem } from './src/crypto.js';
import { assertValid } from './src/model.js';
const root = dirname(fileURLToPath(import.meta.url));
const read = (p) => readFileSync(join(root, 'src', p), 'utf8');
// Modules are concatenated (ES `import` lines and `export` keywords dropped) ahead of app.js into one scope.
const MODULES = ['crypto.js', 'images.js', 'model.js', 'ops.js', 'print.js', 'search.js', 'template.js', 'version.js'];
export function bundleJs() {
return MODULES.map((f) => read(f).replace(/^import .*$/gm, '').replace(/^export /gm, '')).concat(read('app.js')).join('\n');
}
function parseArgs(argv) {
const a = {};
for (let i = 0; i < argv.length; i++) {
if (argv[i] === '--data') a.data = argv[++i];
else if (argv[i] === '--out') a.out = argv[++i];
else throw new Error(`Unknown argument: ${argv[i]}`);
}
return a;
}
export function promptHidden(question) {
return new Promise((res, rej) => {
if (!process.stdin.isTTY) return rej(new Error('No terminal to prompt on: set FW_PASSPHRASE instead.'));
process.stdout.write(question);
let buf = '';
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.setEncoding('utf8');
const done = (fn, v) => { process.stdin.setRawMode(false); process.stdin.pause(); process.stdin.off('data', onData); process.stdout.write('\n'); fn(v); };
const onData = (chunk) => {
for (const ch of chunk) {
if (ch === '\r' || ch === '\n') return done(res, buf);
if (ch === '\u0003') return done(rej, new Error('Cancelled.'));
if (ch === '\u007f' || ch === '\b') buf = buf.slice(0, -1);
else buf += ch;
}
};
process.stdin.on('data', onData);
});
}
async function getPassphrase() {
if (process.env.FW_PASSPHRASE) return process.env.FW_PASSPHRASE;
const a = await promptHidden('Passphrase: ');
const b = await promptHidden('Repeat passphrase: ');
if (a !== b) throw new Error('Passphrases did not match.');
return a;
}
async function main() {
const args = parseArgs(process.argv.slice(2));
let payload = '';
if (args.data) {
const doc = assertValid(JSON.parse(readFileSync(args.data, 'utf8')));
const pass = await getPassphrase();
const problem = passphraseProblem(pass);
if (problem) throw new Error(problem);
payload = await encrypt(JSON.stringify(doc), pass);
}
const html = assemble({ css: read('style.css'), js: bundleJs(), payload });
const out = resolve(args.out || join(root, 'dist', 'final-wishes.html'));
mkdirSync(dirname(out), { recursive: true });
writeFileSync(out, html);
console.log(`Wrote ${out} (${html.length} bytes, ${payload ? 'encrypted data embedded' : 'no data embedded'})`);
if (args.data) console.log(`Note: ${args.data} is still plaintext. Keep it out of git and delete it when done.`);
}
if (process.argv[1] === fileURLToPath(import.meta.url)) main().catch((e) => { console.error(e.message); process.exit(1); });