-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
104 lines (92 loc) · 2.66 KB
/
main.ts
File metadata and controls
104 lines (92 loc) · 2.66 KB
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
#!/usr/bin/env deno
import { parseArgs } from "@std/cli";
import { resolve } from "@std/path";
import { analyzeTrace } from "./analyzer.ts";
import { printTerminalStats } from "./print-stats.ts";
import type { TraceEvent } from "./types.ts";
/** CLI arguments type */
interface Args {
output: string;
help: boolean;
_: (string | number)[];
}
// Parse command line arguments
const cliArgs = parseArgs(Deno.args, {
string: ["output"],
boolean: ["help"],
alias: {
h: "help",
o: "output",
},
default: {
output: "cli",
help: false,
},
}) as Args;
// Show help
if (cliArgs.help) {
console.log("TypeScript Build Trace Analyzer");
console.log("");
console.log("Usage:");
console.log(
" ts-tracelytics [input-file-path] Analyze TypeScript trace file",
);
console.log(" ts-tracelytics --help Show this help message");
console.log(
" ts-tracelytics --output <format> Specify output format (json|cli)",
);
console.log("");
console.log("Examples:");
console.log(" ts-tracelytics ~/project/trace.json");
console.log(
" ts-tracelytics ~/project/trace.json --output json > stats.json",
);
console.log(" ts-tracelytics ~/project/trace.json --output cli");
Deno.exit(0);
}
// Validate input file
if (cliArgs._.length !== 1) {
console.error("Error: Please provide exactly one input trace file path");
console.error("Use --help for usage information");
Deno.exit(1);
}
// Validate output format
if (cliArgs.output && !["json", "cli"].includes(cliArgs.output)) {
console.error("Error: Output format must be either 'json' or 'cli'");
console.error("Use --help for usage information");
Deno.exit(1);
}
// Do the thing!
async function main() {
try {
const inputPath = resolve(String(cliArgs._[0]));
verboseLog(`Reading trace file: ${inputPath}`);
const traceContent = await Deno.readTextFile(inputPath);
const traceData = JSON.parse(traceContent) as TraceEvent[];
verboseLog("Analyzing trace data...");
const statistics = analyzeTrace(traceData);
// Output based on format
if (cliArgs.output === "json") {
console.log(JSON.stringify(statistics, null, 2));
} else {
printTerminalStats(statistics);
}
} catch (error: unknown) {
if (error instanceof Error) {
console.error(`Error: ${error.message}`);
} else {
console.error(`Error: ${String(error)}`);
}
Deno.exit(1);
}
}
await main();
/**
* Print out information to the terminal if the output mode allows it
*/
function verboseLog(...args: unknown[]) {
// Don't log stuff in JSON mode to avoid malforming the output
if (cliArgs.output === "cli") {
console.log(...args);
}
}