delivery-friction-analyzer 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,85 @@
1
+ import { mkdir, readFile, writeFile } from "node:fs/promises";
2
+ import { dirname } from "node:path";
3
+ import { pathToFileURL } from "node:url";
4
+ import { generateRepositoryFrictionReport, renderRepositoryFrictionMarkdown } from "./friction-report.js";
5
+
6
+ const ALLOWED_OPTIONS = new Set(["metrics-summary", "json-out", "markdown-out"]);
7
+
8
+ const USAGE = `Usage:
9
+ node src/report/generate-report.js --metrics-summary <path> --json-out <path> --markdown-out <path>
10
+ `;
11
+
12
+ function parseArgs(argv) {
13
+ const options = {};
14
+ for (let index = 0; index < argv.length; index += 1) {
15
+ const arg = argv[index];
16
+ if (arg === "--help" || arg === "-h") {
17
+ return { help: true };
18
+ }
19
+ if (!arg.startsWith("--")) {
20
+ throw new Error(`Unexpected argument: ${arg}`);
21
+ }
22
+ const key = arg.slice(2);
23
+ if (!ALLOWED_OPTIONS.has(key)) {
24
+ throw new Error(`Unknown option: ${arg}`);
25
+ }
26
+ const value = argv[index + 1];
27
+ if (!value || value.startsWith("--")) {
28
+ throw new Error(`Missing value for ${arg}`);
29
+ }
30
+ options[key] = value;
31
+ index += 1;
32
+ }
33
+
34
+ return {
35
+ metricsSummaryPath: options["metrics-summary"],
36
+ jsonOutPath: options["json-out"],
37
+ markdownOutPath: options["markdown-out"],
38
+ };
39
+ }
40
+
41
+ function requireOptions(options) {
42
+ const missing = [];
43
+ if (!options.metricsSummaryPath) missing.push("--metrics-summary");
44
+ if (!options.jsonOutPath) missing.push("--json-out");
45
+ if (!options.markdownOutPath) missing.push("--markdown-out");
46
+ if (missing.length) {
47
+ throw new Error(`Missing required option(s): ${missing.join(", ")}`);
48
+ }
49
+ }
50
+
51
+ async function writeTextFile(path, contents) {
52
+ await mkdir(dirname(path), { recursive: true });
53
+ await writeFile(path, contents, "utf8");
54
+ }
55
+
56
+ export async function writeRepositoryFrictionReport(options) {
57
+ requireOptions(options);
58
+ const metricsSummary = JSON.parse(await readFile(options.metricsSummaryPath, "utf8"));
59
+ const report = generateRepositoryFrictionReport(metricsSummary);
60
+ const markdown = renderRepositoryFrictionMarkdown(report);
61
+
62
+ await Promise.all([
63
+ writeTextFile(options.jsonOutPath, `${JSON.stringify(report, null, 2)}\n`),
64
+ writeTextFile(options.markdownOutPath, markdown),
65
+ ]);
66
+
67
+ return report;
68
+ }
69
+
70
+ async function main(argv) {
71
+ const options = parseArgs(argv);
72
+ if (options.help) {
73
+ process.stdout.write(USAGE);
74
+ return;
75
+ }
76
+
77
+ await writeRepositoryFrictionReport(options);
78
+ }
79
+
80
+ if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
81
+ main(process.argv.slice(2)).catch(error => {
82
+ process.stderr.write(`${error.message}\n\n${USAGE}`);
83
+ process.exitCode = 1;
84
+ });
85
+ }