-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathstats.ts
More file actions
executable file
·140 lines (122 loc) · 3.88 KB
/
stats.ts
File metadata and controls
executable file
·140 lines (122 loc) · 3.88 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { performance } from 'perf_hooks';
import * as colors from 'colorette';
import {
normalizeTypes,
BaseResolver,
resolveDocument,
detectSpec,
getTypes,
normalizeVisitors,
walkDocument,
Stats,
bundle,
logger,
} from '@redocly/openapi-core';
import { getFallbackApisOrExit, printExecutionTime } from '../utils/miscellaneous.js';
import type { StatsAccumulator, StatsName, WalkContext, OutputFormat } from '@redocly/openapi-core';
import type { CommandArgs } from '../wrapper.js';
import type { VerifyConfigOptions } from '../types.js';
const statsAccumulator: StatsAccumulator = {
refs: { metric: '🚗 References', total: 0, color: 'red', items: new Set() },
externalDocs: { metric: '📦 External Documents', total: 0, color: 'magenta' },
schemas: { metric: '📈 Schemas', total: 0, color: 'white' },
parameters: { metric: '👉 Parameters', total: 0, color: 'yellow', items: new Set() },
links: { metric: '🔗 Links', total: 0, color: 'cyan', items: new Set() },
pathItems: { metric: '🔀 Path Items', total: 0, color: 'green' },
webhooks: { metric: '🎣 Webhooks', total: 0, color: 'green' },
operations: { metric: '👷 Operations', total: 0, color: 'yellow' },
tags: { metric: '🔖 Tags', total: 0, color: 'white', items: new Set() },
};
function printStatsStylish(statsAccumulator: StatsAccumulator) {
for (const node in statsAccumulator) {
const { metric, total, color } = statsAccumulator[node as StatsName];
logger.output(colors[color](`${metric}: ${total} \n`));
}
}
function printStatsJson(statsAccumulator: StatsAccumulator) {
const json: any = {};
for (const key of Object.keys(statsAccumulator)) {
json[key] = {
metric: statsAccumulator[key as StatsName].metric,
total: statsAccumulator[key as StatsName].total,
};
}
logger.output(JSON.stringify(json, null, 2));
}
function printStatsMarkdown(statsAccumulator: StatsAccumulator) {
let output = '| Feature | Count |\n| --- | --- |\n';
for (const key of Object.keys(statsAccumulator)) {
output +=
'| ' +
statsAccumulator[key as StatsName].metric +
' | ' +
statsAccumulator[key as StatsName].total +
' |\n';
}
logger.output(output);
}
function printStats(
statsAccumulator: StatsAccumulator,
api: string,
startedAt: number,
format: string
) {
logger.info(`Document: ${colors.magenta(api)} stats:\n\n`);
switch (format) {
case 'stylish':
printStatsStylish(statsAccumulator);
break;
case 'json':
printStatsJson(statsAccumulator);
break;
case 'markdown':
printStatsMarkdown(statsAccumulator);
break;
}
printExecutionTime('stats', startedAt, api);
}
export type StatsArgv = {
api?: string;
format: OutputFormat;
} & VerifyConfigOptions;
export async function handleStats({ argv, config, collectSpecData }: CommandArgs<StatsArgv>) {
const [{ path }] = await getFallbackApisOrExit(argv.api ? [argv.api] : [], config);
const externalRefResolver = new BaseResolver(config.resolve);
const { bundle: document } = await bundle({ config, ref: path });
collectSpecData?.(document.parsed);
const specVersion = detectSpec(document.parsed);
const types = normalizeTypes(
config.extendTypes(await getTypes(specVersion), specVersion),
config
);
const startedAt = performance.now();
const ctx: WalkContext = {
problems: [],
specVersion,
config,
visitorsData: {},
};
const resolvedRefMap = await resolveDocument({
rootDocument: document,
rootType: types.Root,
externalRefResolver,
});
const statsVisitor = normalizeVisitors(
[
{
severity: 'warn',
ruleId: 'stats',
visitor: Stats(statsAccumulator),
},
],
types
);
walkDocument({
document,
rootType: types.Root,
normalizedVisitors: statsVisitor,
resolvedRefMap,
ctx,
});
printStats(statsAccumulator, path, startedAt, argv.format);
}