45 lines
2.1 KiB
TypeScript
45 lines
2.1 KiB
TypeScript
import { readFileSync, writeFileSync } from "node:fs";
|
|
import { openSiteDatabase } from "../src/lib/site-database";
|
|
import { allDocuments, readDocument, saveDocument, importDocuments, documentHistory } from "../src/lib/docs";
|
|
|
|
process.umask(0o077);
|
|
const [command, argument] = process.argv.slice(2);
|
|
try {
|
|
switch (command) {
|
|
case "backup": {
|
|
if (!argument) throw Error("Provide a private backup filename");
|
|
const db = openSiteDatabase();
|
|
try { db.prepare("VACUUM INTO ?").run(argument); } finally { db.close(); }
|
|
console.log("Full site database backup created; existing files are never overwritten"); break;
|
|
}
|
|
case "list":
|
|
console.log(JSON.stringify(allDocuments().map(({ content: _, ...doc }) => { void _; return doc; }), null, 2)); break;
|
|
case "get": {
|
|
const doc = readDocument(argument ?? "");
|
|
if (!doc) throw Error("Document not found");
|
|
console.log(JSON.stringify(doc, null, 2)); break;
|
|
}
|
|
case "save": {
|
|
const { revision, ...input } = JSON.parse(readFileSync(0, "utf8"));
|
|
const doc = saveDocument(input, revision);
|
|
console.log(JSON.stringify({ slug: doc.slug, revision: doc.revision })); break;
|
|
}
|
|
case "history":
|
|
console.log(JSON.stringify(documentHistory(argument ?? ""), null, 2)); break;
|
|
case "import": {
|
|
const bundle = JSON.parse(readFileSync(0, "utf8"));
|
|
console.log(JSON.stringify({ imported: importDocuments(bundle, argument === "--if-empty") })); break;
|
|
}
|
|
case "export": {
|
|
if (!argument) throw Error("Provide a private export filename");
|
|
writeFileSync(argument, JSON.stringify({ format: "tabularium-documents-v1", documents: allDocuments() }),
|
|
{ flag: "wx", mode: 0o600 });
|
|
console.log("Document export created; existing files are never overwritten"); break;
|
|
}
|
|
default: throw Error("Usage: docs backup <private-file> | list | get <slug> | save < input.json | history <sourceSlug> | import [--if-empty] < export.json | export <private-file>");
|
|
}
|
|
} catch (error) {
|
|
console.error(error instanceof Error ? error.message : "Document operation failed");
|
|
process.exitCode = 1;
|
|
}
|