All files / scripts/roll-forward-pirs cli.ts

97.97% Statements 97/99
85.52% Branches 65/76
80% Functions 4/5
98.86% Lines 87/88

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162                                          27x 27x 45x 45x 31x 17x 13x 9x 7x 6x 6x 6x 1x   5x 5x 3x   2x     23x       17x 17x 17x   17x 17x   1x 1x 1x               16x 13x     13x   13x 1x 1x 1x   12x 12x 12x 13x 13x 13x 13x 1x 1x 1x   3x 2x 1x 1x 1x   1x 1x 1x   1x 1x 1x     1x 1x       1x         1x 1x       11x 11x   1x 1x 1x       10x 10x   1x 1x 1x     9x 9x 9x 9x   9x 1x 1x     8x 8x 8x   8x 16x       8x 10x 5x 5x 5x 5x          
/**
 * @module roll-forward-pirs/cli
 * @description CLI argument parsing and the `runMain` orchestrator that
 * ties together source discovery, validation, roll-forward, and emission.
 *
 * @author Hack23 AB
 * @license Apache-2.0
 */
 
import fs from 'node:fs';
import path from 'node:path';
 
import { ANALYSIS_DIR, PIR_FILE, REPO_ROOT, VALID_CYCLES } from './constants.js';
import { emitRollforwardMd } from './emitter.js';
import { isLongHorizon } from './horizon.js';
import { rollForward } from './roll-forward.js';
import { findLatestSource } from './source-locator.js';
import type { CliArgs, CycleType, PirStatusFile, RunIO } from './types.js';
import { validateSource } from './validator.js';
 
export function parseArgs(argv: string[]): CliArgs {
  const args: CliArgs = { dryRun: false, maxLookback: 14, emitRollforwardMd: false };
  for (let i = 0; i < argv.length; i++) {
    const arg = argv[i];
    if (arg === '--from') args.from = argv[++i];
    else if (arg === '--to') args.to = argv[++i];
    else if (arg === '--date') args.date = argv[++i];
    else if (arg === '--cycle') args.cycle = argv[++i] as CycleType;
    else if (arg === '--dry-run') args.dryRun = true;
    else if (arg === '--emit-rollforward-md') args.emitRollforwardMd = true;
    else Eif (arg === '--max-lookback') {
      const raw = argv[++i];
      if (!raw || raw.startsWith('--')) {
        throw new Error('--max-lookback requires a positive integer value');
      }
      const parsed = Number.parseInt(raw, 10);
      if (!/^[0-9]+$/.test(raw.trim()) || !Number.isFinite(parsed) || parsed < 1) {
        throw new Error(`--max-lookback must be a positive integer (received '${raw}')`);
      }
      args.maxLookback = parsed;
    }
  }
  return args;
}
 
export function runMain(argv: string[], io: RunIO = {}): void {
  const out = io.stdout ?? process.stdout;
  const err = io.stderr ?? process.stderr;
  const exit = io.exit ?? ((c: number): never => process.exit(c));
  let args: CliArgs;
  try {
    args = parseArgs(argv);
  } catch (e) {
    err.write(`Argument error: ${String(e instanceof Error ? e.message : e)}\n`);
    exit(1);
    return;
  }
 
  let sourcePath: string;
  let targetDir: string;
  let targetDate: string;
  let targetCycle: CycleType;
 
  if (args.from && args.to) {
    sourcePath = path.isAbsolute(args.from)
      ? path.join(args.from, PIR_FILE)
      : path.join(REPO_ROOT, args.from, PIR_FILE);
    targetDir = path.isAbsolute(args.to) ? args.to : path.join(REPO_ROOT, args.to);
 
    if (!fs.existsSync(sourcePath)) {
      err.write(`Source not found: ${sourcePath}\n`);
      exit(1);
      return;
    }
    const parts = targetDir.replace(/\\/g, '/').split('/');
    const dailyIdx = parts.indexOf('daily');
    const datePart = dailyIdx >= 0 && dailyIdx + 1 < parts.length ? (parts[dailyIdx + 1] ?? '') : '';
    const cyclePart = dailyIdx >= 0 && dailyIdx + 2 < parts.length ? (parts[dailyIdx + 2] ?? '') : '';
    targetDate = datePart;
    targetCycle = cyclePart as CycleType;
    if (!/^\d{4}-\d{2}-\d{2}$/.test(targetDate) || !VALID_CYCLES.has(targetCycle)) {
      err.write(`Cannot derive date/cycle from --to path: ${args.to}\n`);
      exit(1);
      return;
    }
  } else if (args.date && args.cycle) {
    if (!VALID_CYCLES.has(args.cycle)) {
      err.write(`Unknown cycle: ${args.cycle}. Valid: ${[...VALID_CYCLES].join(', ')}\n`);
      exit(1);
      return;
    }
    targetDate = args.date;
    targetCycle = args.cycle;
    targetDir = path.join(ANALYSIS_DIR, targetDate, targetCycle);
 
    const found = findLatestSource(targetCycle, targetDate, args.maxLookback);
    Eif (!found) {
      err.write(
        `No previous pir-status.json found for cycle '${targetCycle}' within ${args.maxLookback} days before ${targetDate}. Exiting with code 1.\n`,
      );
      exit(1);
      return;
    }
    sourcePath = found;
  } else {
    err.write(
      'Usage:\n' +
        '  roll-forward-pirs --from <dir> --to <dir>\n' +
        '  roll-forward-pirs --date YYYY-MM-DD --cycle <cycle> [--dry-run] [--max-lookback N]\n',
    );
    exit(1);
    return;
  }
 
  let rawSource: unknown;
  try {
    rawSource = JSON.parse(fs.readFileSync(sourcePath, 'utf-8'));
  } catch (e) {
    err.write(`Failed to read source: ${String(e)}\n`);
    exit(2);
    return;
  }
 
  let source: PirStatusFile;
  try {
    source = validateSource(rawSource, sourcePath);
  } catch (e) {
    err.write(`Schema validation error: ${String(e)}\n`);
    exit(2);
    return;
  }
 
  const opts: { now?: () => Date } = {};
  if (io.now) opts.now = io.now;
  const output = rollForward(source, sourcePath, targetDate, targetCycle, opts);
  const json = JSON.stringify(output, null, 2) + '\n';
 
  if (args.dryRun) {
    out.write(json);
    return;
  }
 
  if (!fs.existsSync(targetDir)) fs.mkdirSync(targetDir, { recursive: true });
  const targetPath = path.join(targetDir, PIR_FILE);
  fs.writeFileSync(targetPath, json, 'utf-8');
 
  out.write(
    `✅ Rolled forward ${source.pirs.filter((p) => p.status === 'open').length} open PIR(s) ` +
      `from ${sourcePath.replace(REPO_ROOT + path.sep, '')} → ${targetPath.replace(REPO_ROOT + path.sep, '')}\n`,
  );
 
  if (args.emitRollforwardMd || isLongHorizon(targetCycle)) {
    const sourcePirIds = new Set(source.pirs.map((p) => p.pir_id));
    const md = emitRollforwardMd(output, sourcePath, targetDate, { sourcePirIds });
    const mdPath = path.join(targetDir, 'horizon-pir-rollforward.md');
    fs.writeFileSync(mdPath, md, 'utf-8');
    out.write(
      `📄 Emitted ${mdPath.replace(REPO_ROOT + path.sep, '')}\n`,
    );
  }
}