build: properly workaround Ajv TS type definitions bug
[e-mobility-charging-stations-simulator.git] / src / utils / ConfigurationUtils.ts
CommitLineData
4354af5a
JB
1import { dirname, join, resolve } from 'node:path';
2import { fileURLToPath } from 'node:url';
3
4import chalk from 'chalk';
5
a6ef1ece
JB
6import { Constants } from './Constants.js';
7import { isNotEmptyString, logPrefix as utilsLogPrefix } from './Utils.js';
8import { type ElementsPerWorkerType, FileType, StorageType } from '../types/index.js';
9import { WorkerProcessType } from '../worker/index.js';
4354af5a
JB
10
11export const logPrefix = (): string => {
12 return utilsLogPrefix(' Simulator configuration |');
13};
14
15export const buildPerformanceUriFilePath = (file: string) => {
16 return `file://${join(resolve(dirname(fileURLToPath(import.meta.url)), '../'), file)}`;
17};
18
19export const getDefaultPerformanceStorageUri = (storageType: StorageType) => {
20 switch (storageType) {
21 case StorageType.JSON_FILE:
22 return buildPerformanceUriFilePath(
23 `${Constants.DEFAULT_PERFORMANCE_DIRECTORY}/${Constants.DEFAULT_PERFORMANCE_RECORDS_FILENAME}`,
24 );
25 case StorageType.SQLITE:
26 return buildPerformanceUriFilePath(
27 `${Constants.DEFAULT_PERFORMANCE_DIRECTORY}/${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`,
28 );
29 default:
30 throw new Error(`Unsupported storage type '${storageType}'`);
31 }
32};
33
34export const handleFileException = (
35 file: string,
36 fileType: FileType,
37 error: NodeJS.ErrnoException,
38 logPfx: string,
39): void => {
40 const prefix = isNotEmptyString(logPfx) ? `${logPfx} ` : '';
41 let logMsg: string;
42 switch (error.code) {
43 case 'ENOENT':
44 logMsg = `${fileType} file ${file} not found: `;
45 break;
46 case 'EEXIST':
47 logMsg = `${fileType} file ${file} already exists: `;
48 break;
49 case 'EACCES':
50 logMsg = `${fileType} file ${file} access denied: `;
51 break;
52 case 'EPERM':
53 logMsg = `${fileType} file ${file} permission denied: `;
54 break;
55 default:
56 logMsg = `${fileType} file ${file} error: `;
57 }
58 console.error(`${chalk.green(prefix)}${chalk.red(logMsg)}`, error);
59 throw error;
60};
61
62export const checkWorkerProcessType = (workerProcessType: WorkerProcessType): void => {
63 if (!Object.values(WorkerProcessType).includes(workerProcessType)) {
64 throw new SyntaxError(
65 `Invalid worker process type '${workerProcessType}' defined in configuration`,
66 );
67 }
68};
69
70export const checkWorkerElementsPerWorker = (
65a1157a 71 elementsPerWorker: ElementsPerWorkerType | undefined,
4354af5a
JB
72): void => {
73 if (
74 elementsPerWorker !== undefined &&
75 elementsPerWorker !== 'auto' &&
c20d5d72 76 elementsPerWorker !== 'all' &&
4354af5a
JB
77 !Number.isSafeInteger(elementsPerWorker)
78 ) {
79 throw new SyntaxError(
80 `Invalid number of elements per worker '${elementsPerWorker}' defined in configuration`,
81 );
82 }
83 if (Number.isSafeInteger(elementsPerWorker) && (elementsPerWorker as number) <= 0) {
84 throw RangeError(
85 `Invalid negative or zero number of elements per worker '${elementsPerWorker}' defined in configuration`,
86 );
87 }
88};