| 1 | import { dirname, join, resolve } from 'node:path' |
| 2 | import { fileURLToPath } from 'node:url' |
| 3 | |
| 4 | import chalk from 'chalk' |
| 5 | |
| 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' |
| 10 | |
| 11 | export const logPrefix = (): string => { |
| 12 | return utilsLogPrefix(' Simulator configuration |') |
| 13 | } |
| 14 | |
| 15 | export const buildPerformanceUriFilePath = (file: string): string => { |
| 16 | return `file://${join(resolve(dirname(fileURLToPath(import.meta.url)), '../'), file)}` |
| 17 | } |
| 18 | |
| 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}` |
| 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 | |
| 34 | export 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 | |
| 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` |
| 66 | ) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | export const checkWorkerElementsPerWorker = ( |
| 71 | elementsPerWorker: ElementsPerWorkerType | undefined |
| 72 | ): void => { |
| 73 | if ( |
| 74 | elementsPerWorker != null && |
| 75 | elementsPerWorker !== 'auto' && |
| 76 | elementsPerWorker !== 'all' && |
| 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 | } |