-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.ts
More file actions
276 lines (265 loc) · 11.6 KB
/
app.ts
File metadata and controls
276 lines (265 loc) · 11.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
// 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 fs from 'node:fs';
import * as path from 'node:path';
import { join } from 'path';
import Parsers from '@stoplight/spectral-parsers';
import spectralCore 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';
declare var AggregateError: {
prototype: AggregateError;
new (errors: any[], message?: string): AggregateError;
};
const { Spectral, Document } = spectralCore;
const writeFileAsync = util.promisify(fs.writeFile);
const appendFileAsync = util.promisify(fs.appendFile);
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), // convert to absolute path
})
.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',
}).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;
try {
// Import and create rule instances in RAP-LP
const enabledRulesAndCategorys = await importAndCreateRuleInstances(ruleCategories);
// Load API specification into a Document object
const apiSpecDocument = new Document(
fs.readFileSync(join(apiSpecFileName), 'utf-8').trim(),
Parsers.Yaml,
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();
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 `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),
);
}
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);
});
}
}