Files
ln-bi/scripts/backfill-asset-reconstruction.ts

21 lines
1.5 KiB
TypeScript

// Does not load dotenv, database pools, application bootstrap or background jobs.
import { readFile } from 'node:fs/promises';
import { resolve, relative, sep } from 'node:path';
import { publishReconstruction, validateReconstruction } from '../src/server/routes/asset-history/reconstruction.js';
const args = process.argv.slice(2);
const inputIndex = args.indexOf('--input'), outputIndex = args.indexOf('--output');
if (inputIndex < 0 || !args[inputIndex + 1]) throw new Error('需要 --input 指定已确认的回溯文件');
const data = validateReconstruction(JSON.parse(await readFile(resolve(args[inputIndex + 1]), 'utf8')));
console.log(JSON.stringify({ action: args.includes('--apply') ? 'local-file-publication' : 'dry-run', months: data.periods.length,
firstMonth: data.periods[0]?.month, lastMonth: data.periods.at(-1)?.month, sourceAsOf: data.sourceAsOf,
workbookSha256: data.workbookSha256, historicalTotal: null, databaseWrites: 0 }));
if (args.includes('--apply')) {
if (outputIndex < 0 || !args[outputIndex + 1]) throw new Error('需要 --output 指定本地私有存储位置');
const output = resolve(args[outputIndex + 1]);
for (const folder of ['public', 'dist', 'src']) {
const within = relative(resolve(folder), output);
if (!within || (!within.startsWith(`..${sep}`) && within !== '..' && !within.startsWith(sep))) throw new Error('回溯文件必须保存于源码及公开静态目录之外');
}
console.log(await publishReconstruction(output, data));
}