diff --git a/diigo-cache/scripts/compare-export.ts b/diigo-cache/scripts/compare-export.ts index 43bf797..9715ac0 100644 --- a/diigo-cache/scripts/compare-export.ts +++ b/diigo-cache/scripts/compare-export.ts @@ -5,7 +5,7 @@ * * node scripts/compare-export.ts */ -import * as nodeFs from "node:fs"; +import * as nodeFs from "node:fs/promises"; import { basename, join } from "node:path"; import { DatabaseSync } from "node:sqlite"; import { itemDescription, toRss } from "../src/rss.ts"; @@ -95,11 +95,11 @@ export function cacheItems(db: { prepare(sql: string): { all(): unknown[] } }) { } /** write access to files directly inside one directory: attenuates fs */ -export function outDir(fs: Pick, dir: string) { +export function outDir(fs: Pick, dir: string) { return { - write(name: string, text: string): void { + async write(name: string, text: string): Promise { if (basename(name) !== name) throw new Error(`not a plain file name: ${name}`); - fs.writeFileSync(join(dir, name), text); + await fs.writeFile(join(dir, name), text); }, }; } @@ -116,10 +116,11 @@ export function outDir(fs: Pick, dir: string) { * {@link outDir} — before anything else uses them. Ends by reporting an * exit code (2: usage) through `exit`. */ -export function main( +export async function main( argv = process.argv.slice(2), { fs = nodeFs, + // node:sqlite has only a synchronous API; one read of a local file connect = (path: string) => new DatabaseSync(path, { readOnly: true }), now = () => new Date(), stdout = process.stdout, @@ -128,14 +129,14 @@ export function main( process.exitCode = code; }, }: { - fs?: Pick; + fs?: Pick; connect?: (path: string) => { prepare(sql: string): { all(): unknown[] } }; now?: () => Date; stdout?: { write(text: string): unknown }; stderr?: { write(text: string): unknown }; exit?: (code: number) => void; } = {}, -): void { +): Promise { const [dbPath, exportPath, outPath] = argv; if (!dbPath || !exportPath || !outPath) { stderr.write("usage: node scripts/compare-export.ts \n"); @@ -143,13 +144,13 @@ export function main( } const cache = cacheItems(connect(dbPath)); const out = outDir(fs, outPath); - const exportXml = fs.readFileSync(exportPath, "utf8"); + const exportXml = await fs.readFile(exportPath, "utf8"); const ours = cache.all(); - out.write("from-cache.rss", toRss(ours, { title: "from the cache", pubDate: now().toUTCString() })); + await out.write("from-cache.rss", toRss(ours, { title: "from the cache", pubDate: now().toUTCString() })); const theirs = parseExport(exportXml); const { complete, report } = compare(ours, theirs); - out.write("report.json", JSON.stringify(report, null, 1)); + await out.write("report.json", JSON.stringify(report, null, 1)); const counts = { export: theirs.length, cache: ours.length, @@ -161,4 +162,4 @@ export function main( } // run only as a program, not when imported (e.g. by a test) -if (import.meta.main) main(); +if (import.meta.main) await main();