|
12 | 12 | * https://dev.dataportal.se/rest-api-profil |
13 | 13 | * |
14 | 14 | **************************************************************/ |
15 | | -import yargs from "yargs"; |
16 | | -import { hideBin } from "yargs/helpers"; |
| 15 | +import yargs from 'yargs'; |
| 16 | +import { hideBin } from 'yargs/helpers'; |
17 | 17 | import { execCLI } from './cli-mode.js'; |
18 | 18 | import { startServer } from './api-mode.js'; |
19 | | -import * as path from "node:path"; |
20 | | -import { getRuleModules } from "./util/ruleUtil.js"; |
| 19 | +import * as path from 'node:path'; |
| 20 | +import { getRuleModules } from './util/ruleUtil.js'; |
21 | 21 |
|
22 | 22 | async function main() { |
23 | | - const argv = await yargs(hideBin(process.argv)).version("1.2.0") |
| 23 | + const argv = await yargs(hideBin(process.argv)) |
| 24 | + .version('1.2.0') |
24 | 25 | .option('mode', { |
25 | 26 | alias: 'm', |
26 | | - describe: 'Körläget för applikationen (cli eller api)', |
27 | | - choices: ['cli', 'api'], |
28 | | - demandOption: true, |
| 27 | + describe: 'Körläget för applikationen api', |
| 28 | + choices: ['api'], |
29 | 29 | }) |
30 | | - .option("file", { |
31 | | - alias: "f", |
32 | | - describe: "[cli mode] Path to the YAML file", |
33 | | - type: "string", |
| 30 | + .option('file', { |
| 31 | + alias: 'f', |
| 32 | + describe: '[cli mode] Path to the YAML file', |
| 33 | + type: 'string', |
34 | 34 | coerce: (file: string) => path.resolve(file), // convert to absolute path |
35 | 35 | }) |
36 | | - .option("categories", { |
37 | | - alias: "c", |
38 | | - describe: `[cli mode] Regelkategorier separerade med kommatecken.\nAvailable categories:\r ${getRuleModules().join(",")}`, |
39 | | - type: "string", |
| 36 | + .option('categories', { |
| 37 | + alias: 'c', |
| 38 | + describe: `[cli mode] Regelkategorier separerade med kommatecken.\nAvailable categories:\r ${getRuleModules().join( |
| 39 | + ',', |
| 40 | + )}`, |
| 41 | + type: 'string', |
40 | 42 | }) |
41 | | - .option("logError", { |
42 | | - alias: "l", |
43 | | - 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.', |
| 43 | + .option('logError', { |
| 44 | + alias: 'l', |
| 45 | + describe: |
| 46 | + '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.', |
44 | 47 | type: 'string', |
45 | 48 | }) |
46 | | - .option("append", { |
47 | | - alias: "a", |
48 | | - describe: "Utöka loginformationen i filen för felloggningsiformation. Utökda loginformation till befintlig fil för loggning av fel( om specificerad ).", |
49 | | - type: "boolean", |
| 49 | + .option('append', { |
| 50 | + alias: 'a', |
| 51 | + describe: |
| 52 | + 'Utöka loginformationen i filen för felloggningsiformation. Utökda loginformation till befintlig fil för loggning av fel( om specificerad ).', |
| 53 | + type: 'boolean', |
50 | 54 | default: false, |
51 | | - }) |
52 | | - .option("logDiagnostic", { |
53 | | - alias: "d", |
54 | | - 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.', |
| 55 | + }) |
| 56 | + .option('logDiagnostic', { |
| 57 | + alias: 'd', |
| 58 | + describe: |
| 59 | + '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.', |
55 | 60 | type: 'string', |
56 | 61 | }) |
57 | | - .option("dex", { |
58 | | - 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.', |
| 62 | + .option('dex', { |
| 63 | + describe: |
| 64 | + '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.', |
59 | 65 | type: 'string', |
60 | 66 | }) |
61 | | - .option("enableUrlValidation", { |
| 67 | + .option('enableUrlValidation', { |
62 | 68 | type: 'boolean', |
63 | | - describe: "[api-mode] Möjliggör validering av filer givet url." |
| 69 | + describe: '[api-mode] Möjliggör validering av filer givet url.', |
64 | 70 | }) |
65 | | - .option("urlValidationConfigFile", { |
| 71 | + .option('urlValidationConfigFile', { |
66 | 72 | type: 'string', |
67 | | - describe: "[api-mode] Sökväg till fil för configuration av urlValidation funktionalliteten faller tillbaka på ./urlValidationConfig.cjs", |
| 73 | + describe: |
| 74 | + '[api-mode] Sökväg till fil för configuration av urlValidation funktionalliteten faller tillbaka på ./urlValidationConfig.cjs', |
68 | 75 | }) |
69 | 76 | .check(function (argv) { |
70 | | - if (!argv.mode) { |
71 | | - console.error('Missing required argument: mode ') |
72 | | - } |
73 | | - if (argv.mode === 'cli' && !argv.file) { |
74 | | - console.error('Missing required argument for cli mode: file') |
75 | | - return false; |
76 | | - } |
77 | | - else if(argv.mode === 'api') { |
78 | | - if (argv.file) { |
79 | | - console.error('Argument "file" is only applicable in CLI mode') |
80 | | - return false; |
| 77 | + if (argv.mode !== 'api') { |
| 78 | + if (!argv.file) { |
| 79 | + throw new Error('Saknar obligatoriskt argument för cli-läge: --file <path>'); |
81 | 80 | } |
| 81 | + return true; |
| 82 | + } |
| 83 | + |
| 84 | + if(argv.mode === 'api') { |
| 85 | + const apiForbiddenArgs = new Set(['f', 'd', 'l', 'c', 'dex']); |
| 86 | + const hasForbiddenArgs = Object.keys(argv).filter( |
| 87 | + (k) => apiForbiddenArgs.has(k) && Object.prototype.hasOwnProperty.call(argv, k), |
| 88 | + ); |
82 | 89 |
|
83 | | - if(argv.categories) { |
84 | | - console.error(`Argument "categories" is only applicable in CLI mode`) |
85 | | - return false |
| 90 | + if (hasForbiddenArgs.length > 0) { |
| 91 | + throw new Error('I API-läge är endast --enableUrlValidation och --urlValidationConfigFile tillåtna. '); |
86 | 92 | } |
87 | 93 | } |
| 94 | + |
88 | 95 | return true; |
89 | | - }) |
90 | | - .argv; |
| 96 | + }).argv; |
91 | 97 |
|
92 | 98 | const mode = argv.mode; |
93 | | - |
94 | | - if (mode === 'cli') { |
95 | | - await execCLI(argv); // Starta CLI-läget |
96 | | - } else if (mode === 'api') { |
| 99 | + |
| 100 | + if (mode === 'api') { |
97 | 101 | startServer(argv); // Starta API-läget |
| 102 | + } else { |
| 103 | + await execCLI(argv); // Starta CLI-läget |
98 | 104 | } |
99 | 105 | } |
100 | 106 |
|
101 | 107 | // Starta huvudprocessen |
102 | 108 | main().catch((err) => { |
103 | | - console.error("Ett oväntat fel uppstod:", err); |
| 109 | + console.error('Ett oväntat fel uppstod:', err); |
104 | 110 | }); |
0 commit comments