1 import { dirname
, join
, resolve
} from
'node:path';
2 import { fileURLToPath
} from
'node:url';
4 import chalk from
'chalk';
6 import { Constants
} from
'./Constants';
7 import { isNotEmptyString
, logPrefix
as utilsLogPrefix
} from
'./Utils';
8 import { FileType
, StorageType
} from
'../types';
9 import type { elementsPerWorkerType
} from
'../types/ConfigurationData';
10 import { WorkerProcessType
} from
'../worker';
12 export const logPrefix
= (): string => {
13 return utilsLogPrefix(' Simulator configuration |');
16 export const buildPerformanceUriFilePath
= (file
: string) => {
17 return `file://${join(resolve(dirname(fileURLToPath(import.meta.url)), '../'), file)}`;
20 export const getDefaultPerformanceStorageUri
= (storageType
: StorageType
) => {
21 switch (storageType
) {
22 case StorageType
.JSON_FILE
:
23 return buildPerformanceUriFilePath(
24 `${Constants.DEFAULT_PERFORMANCE_DIRECTORY}/${Constants.DEFAULT_PERFORMANCE_RECORDS_FILENAME}`,
26 case StorageType
.SQLITE
:
27 return buildPerformanceUriFilePath(
28 `${Constants.DEFAULT_PERFORMANCE_DIRECTORY}/${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`,
31 throw new Error(`Unsupported storage type '${storageType}'`);
35 export const handleFileException
= (
38 error
: NodeJS
.ErrnoException
,
41 const prefix
= isNotEmptyString(logPfx
) ? `${logPfx} ` : '';
45 logMsg
= `${fileType} file ${file} not found: `;
48 logMsg
= `${fileType} file ${file} already exists: `;
51 logMsg
= `${fileType} file ${file} access denied: `;
54 logMsg
= `${fileType} file ${file} permission denied: `;
57 logMsg
= `${fileType} file ${file} error: `;
59 console
.error(`${chalk.green(prefix)}${chalk.red(logMsg)}`, error
);
63 export const checkWorkerProcessType
= (workerProcessType
: WorkerProcessType
): void => {
64 if (!Object.values(WorkerProcessType
).includes(workerProcessType
)) {
65 throw new SyntaxError(
66 `Invalid worker process type '${workerProcessType}' defined in configuration`,
71 export const checkWorkerElementsPerWorker
= (
72 elementsPerWorker
: elementsPerWorkerType
| undefined,
75 elementsPerWorker
!== undefined &&
76 elementsPerWorker
!== 'auto' &&
77 elementsPerWorker
!== 'all' &&
78 !Number.isSafeInteger(elementsPerWorker
)
80 throw new SyntaxError(
81 `Invalid number of elements per worker '${elementsPerWorker}' defined in configuration`,
84 if (Number.isSafeInteger(elementsPerWorker
) && (elementsPerWorker
as number) <= 0) {
86 `Invalid negative or zero number of elements per worker '${elementsPerWorker}' defined in configuration`,