recovery-rate-calc/bin/recovery-rate-calc.js
2026-11-26 17:44:06 +03:00

78 lines
2.1 KiB
JavaScript

#!/usr/bin/env node
"use strict";
const fs = require("fs");
const path = require("path");
const { parseCsv } = require("../lib/parse-csv");
const { normalizeRecords } = require("../lib/normalize");
const { buildReport, formatReportText } = require("../lib/report");
const USAGE = `recovery-rate-calc — failed-payment recovery rate, from a CSV you already have
Usage:
recovery-rate-calc <path-to-export.csv> [--json]
recovery-rate-calc --demo [--json]
Works with a failed-payment / invoice CSV export from Stripe, Dodo Payments
or Paddle. No API key, no account, no upload — everything runs locally on
the file you point it at.
Options:
--demo run on the bundled sample file instead of a real export
--json print machine-readable JSON instead of the plain-text report
--help show this message
Exit codes: 0 on success, 1 on a usage or file error.`;
function main(argv) {
const args = argv.slice(2);
if (args.includes("--help") || args.includes("-h") || args.length === 0) {
console.log(USAGE);
process.exit(args.length === 0 ? 1 : 0);
return;
}
const wantsJson = args.includes("--json");
const wantsDemo = args.includes("--demo");
const filePathArg = args.find((a) => !a.startsWith("--"));
const inputPath = wantsDemo
? path.join(__dirname, "..", "test", "fixtures", "sample-stripe-export.csv")
: filePathArg;
if (!inputPath) {
console.error("error: no CSV file given. Pass a file path, or use --demo.\n");
console.error(USAGE);
process.exit(1);
return;
}
let text;
try {
text = fs.readFileSync(inputPath, "utf8");
} catch (err) {
console.error(`error: could not read "${inputPath}": ${err.message}`);
process.exit(1);
return;
}
const { headers, records } = parseCsv(text);
if (headers.length === 0) {
console.error(`error: "${inputPath}" does not look like a CSV file (no header row found).`);
process.exit(1);
return;
}
const events = normalizeRecords(records, headers);
const report = buildReport(events);
if (wantsJson) {
console.log(JSON.stringify(report, null, 2));
} else {
console.log(formatReportText(report));
}
}
main(process.argv);