|
| 1 | +import { cpSync, createReadStream, mkdtempSync } from "fs"; |
| 2 | +import { join } from "path"; |
| 3 | +// eslint-disable-next-line import/no-namespace |
| 4 | +import * as badnessMetrics from "./log-processors/badness-metrics"; |
| 5 | +// eslint-disable-next-line import/no-namespace |
| 6 | +import * as expensivePredicates from "./log-processors/expensive-predicates"; |
| 7 | +// eslint-disable-next-line import/no-namespace |
| 8 | +import * as logSummary from "./log-processors/log-summary"; |
| 9 | +// eslint-disable-next-line import/no-namespace |
| 10 | +import * as stageTimings from "./log-processors/stage-timings"; |
| 11 | +// eslint-disable-next-line import/no-namespace |
| 12 | +import * as tupleSums from "./log-processors/tuple-sums"; |
| 13 | +import { log } from "./util"; |
| 14 | + |
| 15 | +/** |
| 16 | + * Minimal CLI interface for running the evaluator log processing locally. |
| 17 | + * |
| 18 | + * Intended for use in development and debugging. |
| 19 | + * This is not intended to be a full-featured CLI tool, nor as a replacement for ordinary testing. |
| 20 | + * |
| 21 | + * Sample use: |
| 22 | + * |
| 23 | + * ``` |
| 24 | + * $ ts-node cli.ts badness-metrics codeql ~/Downloads/codeql-evaluator-log.json |
| 25 | + * ``` |
| 26 | + */ |
| 27 | +async function main(args: string[]) { |
| 28 | + const positionals = args.filter((arg) => !arg.startsWith("--")); |
| 29 | + const [operation, codeqlPath, logPath] = positionals; |
| 30 | + const options = args.filter((arg) => arg.startsWith("--")); |
| 31 | + const verbose = options.includes("--verbose"); |
| 32 | + const explicitOutputFile = options |
| 33 | + .find((arg) => arg.startsWith("--output=")) |
| 34 | + ?.split("=")[1]; |
| 35 | + const help = options.includes("--help"); |
| 36 | + // dear future reader. Please consider using a proper CLI library instead of this ad hoc parsing. |
| 37 | + const usage = [ |
| 38 | + "Usage: cli <badness-metrics|expensive-predicates|overall-summary|predicates-summary|stage-timings|tuple-sums> <codeql-path> <summary-log-path> [--verbose] [--output=<output-file>]", |
| 39 | + ].join("\n"); |
| 40 | + |
| 41 | + if (help) { |
| 42 | + console.log(usage); |
| 43 | + return; |
| 44 | + } |
| 45 | + if (!operation || !codeqlPath || !logPath) { |
| 46 | + throw new Error(`Missing arguments.\n\n${usage}`); |
| 47 | + } |
| 48 | + async function makeSummaryLogFile(format: "overall" | "predicates") { |
| 49 | + const summaryLogFile = `${logPath}.${format}.log`; |
| 50 | + await logSummary.process(codeqlPath, logPath, summaryLogFile, format); |
| 51 | + return summaryLogFile; |
| 52 | + } |
| 53 | + |
| 54 | + const implicitOutputFile = join( |
| 55 | + mkdtempSync("log-insights-"), |
| 56 | + "implicit-output.txt", |
| 57 | + ); |
| 58 | + const actualOutputFile = explicitOutputFile || implicitOutputFile; |
| 59 | + switch (operation) { |
| 60 | + case "badness-metrics": |
| 61 | + await badnessMetrics.process( |
| 62 | + codeqlPath, |
| 63 | + await makeSummaryLogFile("predicates"), |
| 64 | + actualOutputFile, |
| 65 | + ); |
| 66 | + break; |
| 67 | + case "expensive-predicates": |
| 68 | + await expensivePredicates.process( |
| 69 | + codeqlPath, |
| 70 | + await makeSummaryLogFile("overall"), |
| 71 | + actualOutputFile, |
| 72 | + ); |
| 73 | + break; |
| 74 | + case "overall-summary": |
| 75 | + await logSummary.process( |
| 76 | + codeqlPath, |
| 77 | + logPath, |
| 78 | + actualOutputFile, |
| 79 | + "overall", |
| 80 | + ); |
| 81 | + break; |
| 82 | + case "predicates-summary": { |
| 83 | + await logSummary.process( |
| 84 | + codeqlPath, |
| 85 | + logPath, |
| 86 | + actualOutputFile, |
| 87 | + "predicates", |
| 88 | + ); |
| 89 | + break; |
| 90 | + } |
| 91 | + case "stage-timings": |
| 92 | + await stageTimings.process( |
| 93 | + codeqlPath, |
| 94 | + await makeSummaryLogFile("predicates"), |
| 95 | + actualOutputFile, |
| 96 | + ); |
| 97 | + break; |
| 98 | + case "tuple-sums": |
| 99 | + await tupleSums.process( |
| 100 | + codeqlPath, |
| 101 | + await makeSummaryLogFile("predicates"), |
| 102 | + actualOutputFile, |
| 103 | + ); |
| 104 | + break; |
| 105 | + default: |
| 106 | + throw new Error(`Unknown operation: ${operation}.\n\n${usage}`); |
| 107 | + } |
| 108 | + if (verbose) { |
| 109 | + createReadStream(actualOutputFile).pipe(process.stdout); |
| 110 | + } |
| 111 | + if (explicitOutputFile) { |
| 112 | + cpSync(actualOutputFile, explicitOutputFile); |
| 113 | + } |
| 114 | + log(`Output is available in ${actualOutputFile}.`); |
| 115 | +} |
| 116 | +void main(process.argv.slice(2)); |
0 commit comments