60 lines
2.5 KiB
TypeScript
60 lines
2.5 KiB
TypeScript
/**
|
|
* `pnpm library:verify <id…> --by <name>` — the author's command (day-sentence.md P8).
|
|
*
|
|
* Run it only after reading each sentence at its `check.url` and confirming the words
|
|
* and their public-domain status yourself. It moves those candidates from
|
|
* content/candidates/ into content/library/, stamped with your name and today's
|
|
* date, and refuses (changing nothing) if the library would then fail
|
|
* `validate:library`.
|
|
*
|
|
* **Agents never run this.** `verifiedBy` records that a person checked the source
|
|
* (phase0-workspace.md D4); an agent that runs it makes that record false.
|
|
*/
|
|
import { readFileSync, readdirSync, writeFileSync, existsSync } from 'node:fs';
|
|
import { join } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { verifyCandidates, type Files } from '@rosea/content/candidates';
|
|
import { localDay } from '@rosea/engine';
|
|
|
|
const root = fileURLToPath(new URL('..', import.meta.url));
|
|
const libraryDir = join(root, 'content', 'library');
|
|
const candidatesDir = join(root, 'content', 'candidates');
|
|
|
|
const args = process.argv.slice(2);
|
|
const byAt = args.indexOf('--by');
|
|
const by = byAt >= 0 ? (args[byAt + 1] ?? '') : '';
|
|
const ids = args.filter((a, i) => a !== '--by' && i !== byAt + 1 && !a.startsWith('--'));
|
|
|
|
if (byAt < 0 || ids.length === 0) {
|
|
console.error('usage: pnpm library:verify <id> [<id>…] --by <your name>');
|
|
process.exit(2);
|
|
}
|
|
|
|
const read = (dir: string): Files => {
|
|
if (!existsSync(dir)) return {};
|
|
const files: Record<string, unknown[]> = {};
|
|
for (const f of readdirSync(dir).filter((f) => f.endsWith('.json')).sort()) {
|
|
const data: unknown = JSON.parse(readFileSync(join(dir, f), 'utf8'));
|
|
if (!Array.isArray(data)) throw new Error(`${dir}/${f}: must be an array`);
|
|
files[f] = data;
|
|
}
|
|
return files;
|
|
};
|
|
|
|
// The day on this machine's own clock and zone, as a person would write it.
|
|
const day = localDay(Date.now(), Intl.DateTimeFormat().resolvedOptions().timeZone);
|
|
const result = verifyCandidates({ candidates: read(candidatesDir), library: read(libraryDir), ids, by, day });
|
|
|
|
if (!result.ok) {
|
|
for (const p of result.problems) console.error(p);
|
|
console.error('Nothing was moved.');
|
|
process.exit(1);
|
|
}
|
|
|
|
const write = (dir: string, files: Files): void => {
|
|
for (const [f, entries] of Object.entries(files)) writeFileSync(join(dir, f), `${JSON.stringify(entries, null, 2)}\n`);
|
|
};
|
|
write(libraryDir, result.library);
|
|
write(candidatesDir, result.candidates);
|
|
console.log(`Verified by ${by.trim()} on ${day}: ${result.moved.join(', ')}. Run pnpm validate:library, then commit.`);
|