Skip to content

Commit d2a33c1

Browse files
feat: default mode is cli, api mode is extra
Signed-off-by: Nicklas Silversved <nicklas.silversved@digg.se>
1 parent 3873810 commit d2a33c1

File tree

1 file changed

+60
-54
lines changed

1 file changed

+60
-54
lines changed

src/app.ts

Lines changed: 60 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -12,93 +12,99 @@
1212
* https://dev.dataportal.se/rest-api-profil
1313
*
1414
**************************************************************/
15-
import yargs from "yargs";
16-
import { hideBin } from "yargs/helpers";
15+
import yargs from 'yargs';
16+
import { hideBin } from 'yargs/helpers';
1717
import { execCLI } from './cli-mode.js';
1818
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';
2121

2222
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')
2425
.option('mode', {
2526
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'],
2929
})
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',
3434
coerce: (file: string) => path.resolve(file), // convert to absolute path
3535
})
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',
4042
})
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.',
4447
type: 'string',
4548
})
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',
5054
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.',
5560
type: 'string',
5661
})
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.',
5965
type: 'string',
6066
})
61-
.option("enableUrlValidation", {
67+
.option('enableUrlValidation', {
6268
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.',
6470
})
65-
.option("urlValidationConfigFile", {
71+
.option('urlValidationConfigFile', {
6672
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',
6875
})
6976
.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>');
8180
}
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+
);
8289

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. ');
8692
}
8793
}
94+
8895
return true;
89-
})
90-
.argv;
96+
}).argv;
9197

9298
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') {
97101
startServer(argv); // Starta API-läget
102+
} else {
103+
await execCLI(argv); // Starta CLI-läget
98104
}
99105
}
100106

101107
// Starta huvudprocessen
102108
main().catch((err) => {
103-
console.error("Ett oväntat fel uppstod:", err);
109+
console.error('Ett oväntat fel uppstod:', err);
104110
});

0 commit comments

Comments
 (0)