-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.ts
More file actions
381 lines (356 loc) · 15.6 KB
/
app.ts
File metadata and controls
381 lines (356 loc) · 15.6 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// SPDX-FileCopyrightText: 2025 diggsweden/rest-api-profil-lint-processor
//
// SPDX-License-Identifier: EUPL-1.2
/*************************************************************
*
* RAP-LP
* Rest Api Profil - Lint Processor
*
* Linter for the swedish Rest API profile specification
* REST API-profil
* https://dev.dataportal.se/rest-api-profil
*
**************************************************************/
import yargs from 'yargs';
import * as path from 'node:path';
import * as fs from 'node:fs';
import Parsers from '@stoplight/spectral-parsers';
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 { parseApiSpecInput,detectSpecFormatPreference, ParseResult} from './util/validateUtil.js';
import { SpecParseError } from './util/RapLPSpecParseError.js';
import type { IParser } from '@stoplight/spectral-parsers';
import { Document as SpectralDocument } from '@stoplight/spectral-core';
import { Issue } from './util/RapLPIssueHelpers.js';
declare var AggregateError: {
prototype: AggregateError;
new (errors: any[], message?: string): AggregateError;
};
const writeFileAsync = util.promisify(fs.writeFile);
const appendFileAsync = util.promisify(fs.appendFile);
async function main(): Promise<void> {
try {
// Parse command-line arguments using yargs
const argv = await yargs(process.argv.slice(2))
.version('1.0.0')
.option('file', {
alias: 'f',
describe: 'Sökväg till OpenAPI specifikation(yaml,json)',
demandOption: true,
type: 'string',
coerce: (file: string) => path.resolve(file),
})
.option('categories', {
alias: 'c',
describe: `Regelkategorier separerade med kommatecken.Tillgängliga kategorier: ${getRuleModules().join(',')}`,
type: 'string',
})
.option('logError', {
alias: 'l',
describe:
'Sökväg till fil med information för eventuell felloggningsinformation från RAP-LP. Om ej specificerad, så kommer felet att skrivas ut till stdout.',
type: 'string',
})
.option('append', {
alias: 'a',
describe:
'Utöka loginformationen i filen för felloggningsiformation. Utökda loginformation till befintlig fil för loggning av fel( om specificerad ).',
type: 'boolean',
default: false,
})
.option('logDiagnostic', {
alias: 'd',
describe:
'Sökväg till fil för diagnostiseringsinformation från RAP-LP. Om en specificerad, så kommer diagnostiseringsinformationen att skrivas ut till angiven fil i JSON format.',
type: 'string',
})
.option('dex', {
describe:
'Sökväg till fil för diagnostiseringsinformation från RAP-LP. Om en specificerad, så kommer diagnostiseringsinformationen att skrivas ut till angiven fil i Excel format.',
type: 'string',
}).option('strict', {
describe:
'Aktivera strict mode för validering av semantik och struktur.',
type: 'boolean',
default: false,
}).argv;
// Extract arguments from 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;
// 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(ruleCategories);
// Load API specification into a Document object
try {
/**
* CustomSpectral
*/
const customSpectral = new RapLPCustomSpectral();
customSpectral.setCategorys(enabledRulesAndCategorys.instanceCategoryMap);
customSpectral.setRuleset(enabledRulesAndCategorys.rules);
//Use previous parseResult
const parser: IParser<any> = (parseResult.format === 'json' ? Parsers.Json : Parsers.Yaml) as unknown as IParser<any>;
apiSpecDocument = new SpectralDocument(parseResult.raw, parser, apiSpecFileName);
// Run ruleengine
const result = await customSpectral.run(apiSpecDocument);
const customDiagnostic = new RapLPDiagnostic();
customDiagnostic.processRuleExecutionInformation(result, enabledRulesAndCategorys.instanceCategoryMap);
const diagnosticReports: DiagnosticReport[] = customDiagnostic.processDiagnosticInformation();
if (argv.dex != null) {
try {
const reportHandler = new ExcelReportProcessor({
outputFilePath: argv.dex,
});
reportHandler.generateReportDocument(customDiagnostic);
} catch (dexError: any) {
logErrorToFile(dexError);
console.error(chalk.red('Misslyckades att skriva till excelfilen!'));
}
}
/**
* 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 `allvarlighetsgrad: ${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)} `;
};
//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) {
//Check if we gonna construct logData for diagnostic information
let logData: any;
logData = {
timeStamp: formattedDate,
result: diagnosticReports,
};
let logEntry = JSON.stringify(logData, null, 2) + '\n'; // Properly formatted JSON
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 {
//Log to 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.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.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.forEach((item) => {
console.log(chalk.bgGrey('N/A') + '\t' + item.område + '/' + item.id);
});
}
}
if (logErrorFilePath) {
//Check if we gonna construct some logData for logging purpose
let content: string;
let logData: any;
if (!result || result.length === 0) {
logData = {
timeStamp: formattedDate,
message: 'Inga valideringsfel förekom.',
};
} else {
logData = {
timestamp: formattedDate,
message: 'Valideringsfel upptäcktes. Detaljer följer nedan.',
errors: result,
};
}
try {
if (argv.append) {
// Check for appending logging information
let existingLogs: any[] = [];
if (fs.existsSync(logErrorFilePath)) {
// Does any previous file exists?
const fileContent = await fs.promises.readFile(logErrorFilePath, 'utf8');
try {
existingLogs = JSON.parse(fileContent); // Parse json into object
if (!Array.isArray(existingLogs)) {
existingLogs = [existingLogs]; // Only one object
}
} catch {
// No JSON-file → Ignore
existingLogs = [];
}
}
existingLogs.push(logData); // Push on stack
const updatedContent = JSON.stringify(existingLogs, null, 2);
await writeFileAsync(logErrorFilePath, Buffer.from(updatedContent, 'utf8'));
} else {
const content = JSON.stringify([logData], null, 2); // skriv alltid som array
await writeFileAsync(logErrorFilePath, Buffer.from(content, 'utf8'));
}
console.log(chalk.green(`Skriver inspektion/valideringsinformation från RAP-LP till ${logErrorFilePath}`));
} catch (fileError: any) {
logErrorToFile(fileError);
console.error(chalk.red('Misslyckades att skriva till loggfilen!'));
}
} else {
console.log(chalk.whiteBright('\n<<Regelutfall RAP-LP>>\n'));
if (!result || result.length === 0) {
console.log(chalk.green('Inga valideringsfel förekom.'));
} else {
result.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),
);
process.exitCode = 1;
}
}
// Kör main och fånga oväntade promise-rejections
main().catch((err) => {
logErrorToFile(err);
console.error(chalk.red('Oväntat fel i main:'), err);
process.exitCode = 1;
});
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);
});
}
}