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