X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FConfigurationUtils.ts;h=2a85bb0e2fd5686b603bf9a405181c3bc13f1b64;hb=90dc299a5f5c9990aa52911b2d8fdc401776972f;hp=50bad0fad6fe8be75da647b4c9d875d681d1f975;hpb=c20d5d72396a96028bee5492fa7ea2475b8a109e;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/ConfigurationUtils.ts b/src/utils/ConfigurationUtils.ts index 50bad0fa..2a85bb0e 100644 --- a/src/utils/ConfigurationUtils.ts +++ b/src/utils/ConfigurationUtils.ts @@ -1,88 +1,88 @@ -import { dirname, join, resolve } from 'node:path'; -import { fileURLToPath } from 'node:url'; +import { dirname, join, resolve } from 'node:path' +import { fileURLToPath } from 'node:url' -import chalk from 'chalk'; +import chalk from 'chalk' -import { Constants } from './Constants'; -import { isNotEmptyString, logPrefix as utilsLogPrefix } from './Utils'; -import { FileType, StorageType } from '../types'; -import { WorkerProcessType } from '../worker'; +import { type ElementsPerWorkerType, type FileType, StorageType } from '../types/index.js' +import { WorkerProcessType } from '../worker/index.js' +import { Constants } from './Constants.js' +import { isNotEmptyString, logPrefix as utilsLogPrefix } from './Utils.js' export const logPrefix = (): string => { - return utilsLogPrefix(' Simulator configuration |'); -}; + return utilsLogPrefix(' Simulator configuration |') +} -export const buildPerformanceUriFilePath = (file: string) => { - return `file://${join(resolve(dirname(fileURLToPath(import.meta.url)), '../'), file)}`; -}; +export const buildPerformanceUriFilePath = (file: string): string => { + return `file://${join(resolve(dirname(fileURLToPath(import.meta.url)), '../'), file)}` +} -export const getDefaultPerformanceStorageUri = (storageType: StorageType) => { +export const getDefaultPerformanceStorageUri = (storageType: StorageType): string => { switch (storageType) { case StorageType.JSON_FILE: return buildPerformanceUriFilePath( - `${Constants.DEFAULT_PERFORMANCE_DIRECTORY}/${Constants.DEFAULT_PERFORMANCE_RECORDS_FILENAME}`, - ); + `${Constants.DEFAULT_PERFORMANCE_DIRECTORY}/${Constants.DEFAULT_PERFORMANCE_RECORDS_FILENAME}` + ) case StorageType.SQLITE: return buildPerformanceUriFilePath( - `${Constants.DEFAULT_PERFORMANCE_DIRECTORY}/${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`, - ); + `${Constants.DEFAULT_PERFORMANCE_DIRECTORY}/${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db` + ) default: - throw new Error(`Unsupported storage type '${storageType}'`); + throw new Error(`Unsupported storage type '${storageType}'`) } -}; +} export const handleFileException = ( file: string, fileType: FileType, error: NodeJS.ErrnoException, - logPfx: string, + logPfx: string ): void => { - const prefix = isNotEmptyString(logPfx) ? `${logPfx} ` : ''; - let logMsg: string; + const prefix = isNotEmptyString(logPfx) ? `${logPfx} ` : '' + let logMsg: string switch (error.code) { case 'ENOENT': - logMsg = `${fileType} file ${file} not found: `; - break; + logMsg = `${fileType} file ${file} not found: ` + break case 'EEXIST': - logMsg = `${fileType} file ${file} already exists: `; - break; + logMsg = `${fileType} file ${file} already exists: ` + break case 'EACCES': - logMsg = `${fileType} file ${file} access denied: `; - break; + logMsg = `${fileType} file ${file} access denied: ` + break case 'EPERM': - logMsg = `${fileType} file ${file} permission denied: `; - break; + logMsg = `${fileType} file ${file} permission denied: ` + break default: - logMsg = `${fileType} file ${file} error: `; + logMsg = `${fileType} file ${file} error: ` } - console.error(`${chalk.green(prefix)}${chalk.red(logMsg)}`, error); - throw error; -}; + console.error(`${chalk.green(prefix)}${chalk.red(logMsg)}`, error) + throw error +} export const checkWorkerProcessType = (workerProcessType: WorkerProcessType): void => { if (!Object.values(WorkerProcessType).includes(workerProcessType)) { throw new SyntaxError( - `Invalid worker process type '${workerProcessType}' defined in configuration`, - ); + `Invalid worker process type '${workerProcessType}' defined in configuration` + ) } -}; +} export const checkWorkerElementsPerWorker = ( - elementsPerWorker: number | 'auto' | 'all' | undefined, + elementsPerWorker: ElementsPerWorkerType | undefined ): void => { if ( - elementsPerWorker !== undefined && + elementsPerWorker != null && elementsPerWorker !== 'auto' && elementsPerWorker !== 'all' && !Number.isSafeInteger(elementsPerWorker) ) { throw new SyntaxError( - `Invalid number of elements per worker '${elementsPerWorker}' defined in configuration`, - ); + `Invalid number of elements per worker '${elementsPerWorker}' defined in configuration` + ) } if (Number.isSafeInteger(elementsPerWorker) && (elementsPerWorker as number) <= 0) { throw RangeError( - `Invalid negative or zero number of elements per worker '${elementsPerWorker}' defined in configuration`, - ); + `Invalid negative or zero number of elements per worker '${elementsPerWorker}' defined in configuration` + ) } -}; +}