44 lines
2.2 KiB
JavaScript
44 lines
2.2 KiB
JavaScript
#!/usr/bin/env node
|
|
import path from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { dateRange, readCommits, readIssues, readSessions, summarize, formatTable, SourceError } from './ledger.mjs';
|
|
|
|
const usage = 'Usage: node packages/ledger/src/cli.mjs --since YYYY-MM-DD [--until YYYY-MM-DD] [--json] [--no-issues]';
|
|
export async function main(args, root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../..')) {
|
|
let since, until, json = false, noIssues = false;
|
|
const seen = new Set();
|
|
for (let i = 0; i < args.length; i++) {
|
|
const flag = args[i];
|
|
if (seen.has(flag)) throw new SourceError(`Duplicate option: ${flag}`);
|
|
seen.add(flag);
|
|
if (flag === '--help') { console.log(usage); return; }
|
|
if (flag === '--json') json = true;
|
|
else if (flag === '--no-issues') noIssues = true;
|
|
else if (flag === '--since' || flag === '--until') {
|
|
const value = args[++i];
|
|
if (!value || value.startsWith('--')) throw new SourceError(`${flag} requires a date`);
|
|
if (flag === '--since') since = value; else until = value;
|
|
} else throw new SourceError('Unknown option; ' + usage);
|
|
}
|
|
if (!since) throw new SourceError(usage);
|
|
const range = dateRange(since, until);
|
|
const commits = readCommits(root, range);
|
|
// Fixture tools may be placed first on PATH. The repository client is the
|
|
// default without requiring installation or reading auth material here.
|
|
const priorPath = process.env.PATH;
|
|
process.env.PATH = `${priorPath ?? ''}${path.delimiter}${path.join(root, 'scripts')}`;
|
|
let issues;
|
|
try { issues = noIssues ? null : readIssues(root, range); }
|
|
finally { if (priorPath === undefined) delete process.env.PATH; else process.env.PATH = priorPath; }
|
|
const sessions = await readSessions(root, range);
|
|
const report = summarize(range, commits, issues, sessions);
|
|
console.log(json ? JSON.stringify(report, null, 2) : formatTable(report));
|
|
return report;
|
|
}
|
|
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
|
main(process.argv.slice(2)).catch(error => {
|
|
console.error(error instanceof SourceError ? error.message : 'Ledger failed: cannot read source evidence');
|
|
process.exitCode = error.exitCode ?? 1;
|
|
});
|
|
}
|