-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.ts
117 lines (112 loc) · 4.65 KB
/
cli.ts
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
import fs from "node:fs/promises";
import yargs from "yargs";
import { GenericParser } from "./parser/generic-parser.js";
import { Configuration } from "./parser/configuration.js";
import { updateNodeTypesMappingFile } from "./commands/import-grammars/import-node-types.js";
import { outputAsJson } from "./commands/output-metrics.js";
export const parser = yargs()
.command(
"import-grammars",
"(re-)import grammar expression types for supported languages",
{},
async () => {
await updateNodeTypesMappingFile();
},
)
.command(
"parse [sources-path]",
"parse file or folders recursively by given path and calculate metrics",
(cmdYargs) => {
return cmdYargs
.positional("sources-path", {
describe: "path to sources",
type: "string",
})
.option("output-path", {
alias: "o",
type: "string",
description: "Output file path (required)",
})
.option("relative-paths", {
alias: "r",
type: "boolean",
default: false,
description:
"Write relative instead of absolute paths to the analyzed files in the output",
})
.option("exclusions", {
alias: "e",
type: "string",
description: "Exclude folders from scanning for files (comma separated list)",
default: "node_modules,.idea,dist,build,out,vendor",
})
.option("parse-h-as-c", {
alias: "hc",
type: "boolean",
description: "Parse all .h files as C instead of C++ (defaults to C++)",
default: false,
})
.option("parse-some-h-as-c", {
alias: "shc",
type: "string",
description:
"For the specified folders/files (comma separated list), parse .h files as C instead of C++. " +
"Ignored if parse-h-as-c is set.",
default: "",
})
.option("compress", {
alias: "c",
type: "boolean",
description: "output .gz-zipped file",
default: false,
})
.option("parse-dependencies", {
type: "boolean",
default: false,
description:
"EXPERIMENTAL: flag to enable dependency parsing (dependencies will be appended to the output file)",
})
.demandOption(["sources-path", "output-path"]);
},
async (argv) => {
const configuration = new Configuration({
/* eslint-disable @typescript-eslint/dot-notation */
sourcesPath: await fs.realpath(argv["sources-path"]),
outputPath: argv["output-path"],
parseDependencies: argv["parse-dependencies"],
exclusions: argv["exclusions"],
parseAllHAsC: argv["parse-h-as-c"],
parseSomeHAsC: argv["parse-some-h-as-c"],
compress: argv["compress"],
relativePaths: argv["relative-paths"],
/* eslint-enable @typescript-eslint/dot-notation */
});
await parseSourceCode(configuration);
},
)
.demandCommand()
.strictCommands()
.strictOptions();
async function parseSourceCode(configuration: Configuration): Promise<void> {
try {
console.time("Time to complete");
const parser = new GenericParser(configuration);
const results = await parser.calculateMetrics();
console.log("#####################################");
console.log("Metrics calculation finished.");
console.timeEnd("Time to complete");
outputAsJson({
fileMetrics: results.fileMetrics,
unsupportedFiles: results.unsupportedFiles,
errorFiles: results.errorFiles,
relationshipMetrics: results.couplingMetrics,
outputFilePath: configuration.outputPath,
compress: configuration.compress,
});
} catch (error) {
console.error("#####################################");
console.error("#####################################");
console.error("Metrics calculation failed with the following error:");
console.error(error);
}
}