-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli-mode.ts
More file actions
300 lines (283 loc) · 13.2 KB
/
cli-mode.ts
File metadata and controls
300 lines (283 loc) · 13.2 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// SPDX-FileCopyrightText: 2025 Digg - Agency for Digital Government
//
// SPDX-License-Identifier: EUPL-1.2
import * as fs from 'node:fs';
import { join } from 'path';
import Parsers from '@stoplight/spectral-parsers';
import { Document } from '@stoplight/spectral-core';
import { importAndCreateRuleInstances, getRuleModules } from './util/ruleUtil.js'; // Import the helper function
import util from 'util';
import { RapLPCustomSpectral } from './util/RapLPCustomSpectral.js';
import { DiagnosticReport, RapLPDiagnostic } from './util/RapLPDiagnostic.js';
import { AggregateError } from './util/RapLPCustomErrorInfo.js';
import chalk from 'chalk';
import { ExcelReportProcessor } from './util/excelReportProcessor.js';
import type { IParser } from '@stoplight/spectral-parsers';
import { Document as SpectralDocument } from '@stoplight/spectral-core';
import { Issue } from './util/Issue.js';
import { parseApiSpecInput,detectSpecFormatPreference, ParseResult} from './util/validateUtil.js';
import { SpecParseError } from './util/RapLPSpecParseError.js';
import * as path from 'node:path';
import { RuleExecutionContext } from './util/RuleExecutionContext.js';
declare var AggregateError: {
prototype: AggregateError;
new (errors: any[], message?: string): AggregateError;
};
const writeFileAsync = util.promisify(fs.writeFile);
const appendFileAsync = util.promisify(fs.appendFile);
export type CliArgs = {
file?: string;
categories?: string;
logError?: string;
append: boolean;
logDiagnostic?: string;
dex?: string;
strict?: boolean;
};
export async function execCLI<T extends CliArgs>(argv: T) {
try {
// Parse command-line arguments using yargs
const apiSpecFileName = (argv.file as string) || '';
const ruleCategories = argv.categories ? (argv.categories as string).split(',') : undefined;
const logErrorFilePath = argv.logError as string | undefined;
const logDiagnosticFilePath = argv.logDiagnostic as string | undefined;
const strict = argv.strict as boolean ?? false;
const context = new RuleExecutionContext();
// Schemevalidation and Spectral Document creation ----------
let apiSpecDocument: SpectralDocument;
let parseResult: ParseResult;
try {
const prefer = detectSpecFormatPreference(apiSpecFileName,undefined,'auto');
parseResult = await parseApiSpecInput(
{filePath: apiSpecFileName},{
strict: strict,
preferJsonError: prefer
}
);
// Issue handling ----------
if (parseResult.strictIssues && parseResult.strictIssues.length > 0) {
console.error('Strict validation reported issues:');
parseResult.strictIssues.forEach((iss: Issue) =>
console.error(chalk.yellow(`- ${iss.type} at ${iss.path} : ${iss.message} ${iss.line ? `(line ${iss.line})` : ''}`)),
);
process.exitCode = 2;
return;
}
} catch (err: any) {
// Parse handling
if (err instanceof SpecParseError) {
const formattedDate = new Date().toISOString();
const logData = {
timeStamp: formattedDate,
message: 'Fel vid parsing av API-specifikationen.',
error: err.toJSON ? err.toJSON() : { message: String(err) },
};
if (logErrorFilePath) {
try {
let existingLogs: any[] = [];
if (argv.append && fs.existsSync(logErrorFilePath)) {
const fileContent = await fs.promises.readFile(logErrorFilePath, 'utf8');
try {
existingLogs = JSON.parse(fileContent);
if (!Array.isArray(existingLogs)) existingLogs = [existingLogs];
} catch {
existingLogs = [];
}
}
existingLogs.push(logData);
const updatedContent = JSON.stringify(existingLogs, null, 2);
await writeFileAsync(logErrorFilePath, Buffer.from(updatedContent, 'utf8'));
console.log(chalk.green(`Parserfel loggat till ${logErrorFilePath}`));
} catch (fileErr: any) {
console.error(chalk.red('Misslyckades att skriva parserfel till loggfilen:'), fileErr.message);
}
} else {
// No log file specified - write to stdout
console.error(chalk.red('<<< Parserfel i API-specifikationen >>>'));
console.error(chalk.red(`Fel: ${err.message}`));
if (err.line || err.column) {
console.error(chalk.yellow(`Rad: ${err.line ?? '-'}, Kolumn: ${err.column ?? '-'}`));
}
if (err.snippet && !err.message.includes(err.snippet)) {
console.error(chalk.gray('--- snippet ---'));
console.error(chalk.gray(err.snippet));
console.error(chalk.gray('---------------'));
}
}
process.exitCode = 1;
return; // terminate main gracefully
}
// Övrigt oväntat fel
logErrorToFile(err);
console.error(chalk.red('Ett fel uppstod vid inläsning/parsing av spec-filen. Se felloggen för mer information.'));
process.exitCode = 1;
return;
}
try {
// Import and create rule instances in RAP-LP
const enabledRulesAndCategorys = await importAndCreateRuleInstances(context,ruleCategories);
// Load API specification into a Document object
const parser: IParser<any> = (parseResult.format === 'json' ? Parsers.Json : Parsers.Yaml) as unknown as IParser<any>;
apiSpecDocument = new SpectralDocument(parseResult.raw, parser, apiSpecFileName);
try {
/**
* CustomSpectral
*/
const customSpectral = new RapLPCustomSpectral();
customSpectral.setCategorys(enabledRulesAndCategorys.instanceCategoryMap);
customSpectral.setRuleset(enabledRulesAndCategorys.rules);
const result = await customSpectral.run(apiSpecDocument);
const customDiagnostic = new RapLPDiagnostic(context);
customDiagnostic.processRuleExecutionInformation(result, enabledRulesAndCategorys.instanceCategoryMap);
const diagnosticReports: DiagnosticReport[] = customDiagnostic.processDiagnosticInformation();
if (argv.dex != null) {
const reportHandler = new ExcelReportProcessor({
outputFilePath: argv.dex,
});
reportHandler.generateReportDocument(customDiagnostic);
}
/**
* Chalk impl.
* @param allvarlighetsgrad
* @returns
*/
// Run Spectral on the API specification and log the result
const colorizeSeverity = (allvarlighetsgrad: string) => {
switch (allvarlighetsgrad) {
case 'ERROR': // Error
return chalk.red('Error');
case 'WARNING': // Warning
return chalk.yellow('Warning');
case 'HINT': // Info
return chalk.greenBright('Hint');
default:
return chalk.white('Info');
}
};
const formatLintingResult = (result: any) => {
return `\nallvarlighetsgrad: ${colorizeSeverity(result.allvarlighetsgrad)} \nid: ${result.id} \nkrav: ${
result.krav
} \nområde: ${result.område} \nsökväg:[${result.sökväg}] \nomfattning:${JSON.stringify(
result.omfattning,
null,
2,
)}\ndesignregel: ${result.helpUrl} `;
};
//Check specified option from yargs input
const currentDate = new Date(); //.toISOString(); // Get current date and time in ISO format
const formattedDate = `${currentDate.getFullYear()}-${padZero(currentDate.getMonth() + 1)}-${padZero(
currentDate.getDate(),
)} ${padZero(currentDate.getHours())}:${padZero(currentDate.getMinutes())}:${padZero(
currentDate.getSeconds(),
)}`;
function padZero(num: number): string {
return num < 10 ? `0${num}` : `${num}`;
}
if (logDiagnosticFilePath) {
let allDiagnosticReports = JSON.stringify(diagnosticReports, null, 2);
let logEntry = `${formattedDate}\n${allDiagnosticReports}\n`; // Prepend datestamp to log entry
let utf8EncodedContent = Buffer.from(logEntry, 'utf8');
//Log to disc
await writeFileAsync(logDiagnosticFilePath, utf8EncodedContent);
console.log(chalk.green(`Skriver diagnostiseringsinformation från RAP-LP till ${logDiagnosticFilePath}`));
} else {
//STDOUT
if (
customDiagnostic.diagnosticInformation.executedUniqueRules != undefined &&
customDiagnostic.diagnosticInformation.executedUniqueRules.length > 0
) {
console.log(chalk.green('<<<Verkställda och godkända regler - RAP-LP>>>\r'));
console.log(chalk.whiteBright('STATUS\tOMRÅDE') + ' / ' + chalk.whiteBright('IDENTIFIKATIONSNUMMER'));
customDiagnostic.diagnosticInformation.executedUniqueRules
.sort((a, b) => a.id.localeCompare(b.id, 'sv'))
.forEach((item) => {
console.log(chalk.bgGreen('OK') + '\t' + item.område + ' / ' + item.id);
});
}
if (
customDiagnostic.diagnosticInformation.executedUniqueRulesWithError != undefined &&
customDiagnostic.diagnosticInformation.executedUniqueRulesWithError.length > 0
) {
console.log(chalk.green('<<<Verkställda och ej godkända regler - RAP-LP>>>\r'));
console.log(chalk.whiteBright('STATUS\tOMRÅDE') + ' / ' + chalk.whiteBright('IDENTIFIKATIONSNUMMER'));
customDiagnostic.diagnosticInformation.executedUniqueRulesWithError
.sort((a, b) => a.id.localeCompare(b.id, 'sv'))
.forEach((item) => {
console.log(chalk.bgRed('EJ OK') + '\t' + item.område + ' / ' + item.id);
});
}
if (
customDiagnostic.diagnosticInformation.notApplicableRules != undefined &&
customDiagnostic.diagnosticInformation.notApplicableRules.length > 0
) {
console.log(chalk.grey('<<<Ej tillämpade regler - RAP-LP>>>\r'));
console.log(chalk.whiteBright('STATUS\tOMRÅDE') + ' / ' + chalk.whiteBright('IDENTIFIKATIONSNUMMER'));
customDiagnostic.diagnosticInformation.notApplicableRules
.sort((a, b) => a.id.localeCompare(b.id, 'sv'))
.forEach((item) => {
console.log(chalk.bgGrey('N/A') + '\t' + item.område + '/' + item.id);
});
}
}
if (logErrorFilePath) {
let content = JSON.stringify(result, null, 2);
let logEntry = `${formattedDate}\n${content}\n`; // Prepend datestamp to log entry
let utf8EncodedContent = Buffer.from(logEntry, 'utf8');
if (argv.append) {
await appendFileAsync(logErrorFilePath, utf8EncodedContent);
console.log(chalk.green(`Skriver inspektion/valideringsinformation från RAP-LP till ${logErrorFilePath}`));
} else {
//Log to disc
await writeFileAsync(logErrorFilePath, utf8EncodedContent);
console.log(chalk.green(`Skriver inspektion/valideringsinformation från RAP-LP till ${logErrorFilePath}`));
}
} else {
//Verbose error logging goes here with detailed result
console.log(chalk.whiteBright('\n<<Regelutfall RAP-LP>> \n'));
result
.sort((a, b) => {
const idA = a.id ?? '';
const idB = b.id ?? '';
return idA.localeCompare(idB, 'sv');
})
.forEach((item) => {
console.log(formatLintingResult(item));
});
}
} catch (spectralError: any) {
logErrorToFile(spectralError); // Log stack
console.error(
chalk.red(
'Ett fel uppstod vid initiering/körning av regelklasser! Undersök felloggen för RAP-LP för mer information om felet',
),
);
}
} catch (initializingError: any) {
logErrorToFile(initializingError);
console.error(
chalk.red(
'Ett fel uppstod vid inläsning av moduler och skapande av regelklasser! Undersök felloggen för RAP-LP för mer information om felet',
),
);
}
} catch (error: any) {
logErrorToFile(error);
console.error(
chalk.red('Ett oväntat fel uppstod! Undersök felloggen för RAP-LP för mer information om felet', error.message),
);
}
function logErrorToFile(error: any) {
const errorMessage = `${new Date().toISOString()} - ${error.stack}\n`;
fs.appendFileSync('rap-lp-error.log', errorMessage);
if (error.errors) {
const detailedMessage = `${new Date().toISOString()} - ${JSON.stringify(error.errors, null, 2)}\n`;
fs.appendFileSync('rap-lp-error.log', detailedMessage);
}
if (error instanceof AggregateError) {
error.errors.forEach((err: any, index: number) => {
const causeMessage = `Cause ${index + 1}: ${err.stack || err}\n`;
fs.appendFileSync('rap-lp-error.log', causeMessage);
});
}
}
}