feat: make the site database authoritative for documents and catalog
Verify database-backed documents / verify (pull_request) Failing after 6m6s
Verify database-backed documents / verify (pull_request) Failing after 6m6s
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user