1 import { dirname
, join
, resolve
} from
'node:path'
2 import { fileURLToPath
} from
'node:url'
4 import chalk from
'chalk'
6 import { type ElementsPerWorkerType
, type FileType
, StorageType
} from
'../types/index.js'
7 import { WorkerProcessType
} from
'../worker/index.js'
8 import { Constants
} from
'./Constants.js'
9 import { isNotEmptyString
, logPrefix
as utilsLogPrefix
} from
'./Utils.js'
11 export const logPrefix
= (): string => {
12 return utilsLogPrefix(' Simulator configuration |')
15 export const buildPerformanceUriFilePath
= (file
: string): string => {
16 return `file://${join(resolve(dirname(fileURLToPath(import.meta.url)), '../'), file)}`
19 export const getDefaultPerformanceStorageUri
= (storageType
: StorageType
): string => {
20 switch (storageType
) {
21 case StorageType
.JSON_FILE
:
22 return buildPerformanceUriFilePath(
23 `${Constants.DEFAULT_PERFORMANCE_DIRECTORY}/${Constants.DEFAULT_PERFORMANCE_RECORDS_FILENAME}`
25 case StorageType
.SQLITE
:
26 return buildPerformanceUriFilePath(
27 `${Constants.DEFAULT_PERFORMANCE_DIRECTORY}/${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`
30 throw new Error(`Unsupported storage type '${storageType}'`)
34 export const handleFileException
= (
37 error
: NodeJS
.ErrnoException
,
40 const prefix
= isNotEmptyString(logPfx
) ? `${logPfx} ` : ''
44 logMsg
= `${fileType} file ${file} not found: `
47 logMsg
= `${fileType} file ${file} already exists: `
50 logMsg
= `${fileType} file ${file} access denied: `
53 logMsg
= `${fileType} file ${file} permission denied: `
56 logMsg
= `${fileType} file ${file} error: `
58 console
.error(`${chalk.green(prefix)}${chalk.red(logMsg)}`, error
)
62 export 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`
70 export const checkWorkerElementsPerWorker
= (
71 elementsPerWorker
: ElementsPerWorkerType
| undefined
74 elementsPerWorker
!= null &&
75 elementsPerWorker
!== 'auto' &&
76 elementsPerWorker
!== 'all' &&
77 !Number.isSafeInteger(elementsPerWorker
)
79 throw new SyntaxError(
80 `Invalid number of elements per worker '${elementsPerWorker}' defined in configuration`
83 if (Number.isSafeInteger(elementsPerWorker
) && (elementsPerWorker
as number) <= 0) {
85 `Invalid negative or zero number of elements per worker '${elementsPerWorker}' defined in configuration`