From 51022aa0d811eec79514fbeb56cb9556e7168cd7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 20 May 2023 14:29:28 +0200 Subject: [PATCH] build(simulator): don't preserve modules MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .vscode/settings.json | 1 + docker/Dockerfile | 2 +- docker/start.sh | 2 +- manifest-cf-template.yml | 2 +- mikro-orm.config-template.ts | 3 +- package.json | 23 +++++---- rollup.config.mjs | 8 ++- src/charging-station/Bootstrap.ts | 5 +- src/charging-station/ChargingStation.ts | 9 ++-- src/charging-station/ChargingStationUtils.ts | 2 +- src/charging-station/IdTagsCache.ts | 6 +-- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 34 ++++++------- .../ocpp/1.6/OCPP16RequestService.ts | 20 ++++---- .../ocpp/1.6/OCPP16ResponseService.ts | 50 +++++++++--------- .../ocpp/1.6/OCPP16ServiceUtils.ts | 5 +- .../ocpp/2.0/OCPP20IncomingRequestService.ts | 2 +- .../ocpp/2.0/OCPP20RequestService.ts | 6 +-- .../ocpp/2.0/OCPP20ResponseService.ts | 8 +-- .../ocpp/2.0/OCPP20ServiceUtils.ts | 5 +- src/charging-station/ocpp/OCPPServiceUtils.ts | 9 ++-- src/performance/storage/JsonFileStorage.ts | 8 +-- src/utils/Configuration.ts | 4 +- src/utils/ErrorUtils.ts | 43 ++++++++++++++++ src/utils/FileUtils.ts | 51 +++++-------------- src/utils/index.ts | 1 + 25 files changed, 162 insertions(+), 147 deletions(-) create mode 100644 src/utils/ErrorUtils.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index d32d9402..b619a9aa 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -16,6 +16,7 @@ "cacheable", "commitlint", "CSMS", + "emobility", "evse", "evses", "iccid", diff --git a/docker/Dockerfile b/docker/Dockerfile index 96fefe3b..d1f2135f 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,4 +1,4 @@ -FROM node:lts-alpine as builder +FROM node:lts-alpine AS builder # Build simulator WORKDIR /usr/builder diff --git a/docker/start.sh b/docker/start.sh index fa3b0b95..a244fcaf 100755 --- a/docker/start.sh +++ b/docker/start.sh @@ -1,4 +1,4 @@ #!/usr/bin/env sh -node -r source-map-support/register dist/start.mjs & +node -r source-map-support/register dist/start.js & node webui/start.js diff --git a/manifest-cf-template.yml b/manifest-cf-template.yml index a7d07501..4a4a2258 100644 --- a/manifest-cf-template.yml +++ b/manifest-cf-template.yml @@ -9,7 +9,7 @@ applications: routes: - route: e-mobility-charging-stations-simulator.cfapps.sap.hana.ondemand.com health-check-type: process - command: node -r source-map-support/register dist/start.mjs + command: node -r source-map-support/register dist/start.js env: # OPTIMIZE_MEMORY: true NODE_OPTIONS: --stack-trace-limit=1024 --max-old-space-size=768 diff --git a/mikro-orm.config-template.ts b/mikro-orm.config-template.ts index 069d2728..b89b975f 100644 --- a/mikro-orm.config-template.ts +++ b/mikro-orm.config-template.ts @@ -1,4 +1,5 @@ import path from 'node:path'; +import { fileURLToPath } from 'node:url'; import { TsMorphMetadataProvider } from '@mikro-orm/reflection'; @@ -10,7 +11,7 @@ export default { entities: [PerformanceRecord, PerformanceData], type: 'sqlite', clientUrl: `file://${path.join( - path.resolve(__dirname), + path.dirname(fileURLToPath(import.meta.url)), `${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db` )}`, }; diff --git a/package.json b/package.json index e68aa9f8..a75431fd 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ ], "type": "module", "exports": [ - "./dist/start.mjs" + "./dist/start.js" ], "auto-changelog": { "commitUrl": "https://github.com/sap/e-mobility-charging-stations-simulator/commit/{id}", @@ -47,19 +47,20 @@ "preinstall": "node skip-preinstall.cjs || npx --yes only-allow pnpm", "prepare": "node prepare.cjs", "build-requirements": "node --no-warnings build-requirements.mjs", - "start": "pnpm build && cross-env NODE_ENV=production node -r source-map-support/register dist/start.mjs", - "start:dev": "pnpm build:dev && cross-env NODE_ENV=development node -r source-map-support/register dist/start.mjs", - "start:dev:debug": "pnpm build:dev && cross-env NODE_ENV=development node -r source-map-support/register --inspect dist/start.mjs", - "start:prof": "cross-env NODE_ENV=production node -r source-map-support/register --prof dist/start.mjs", - "start:doctorprof": "cross-env NODE_ENV=production clinic doctor -- node -r source-map-support/register dist/start.mjs", - "start:flameprof": "cross-env NODE_ENV=production clinic flame -- node -r source-map-support/register dist/start.mjs", - "start:bubbleprof": "cross-env NODE_ENV=production clinic bubbleprof -- node -r source-map-support/register dist/start.mjs", - "start:heapprofiler": "cross-env NODE_ENV=production clinic heapprofiler -- node -r source-map-support/register dist/start.mjs", + "start": "pnpm build && cross-env NODE_ENV=production node -r source-map-support/register dist/start.js", + "start:dev": "pnpm build:dev && cross-env NODE_ENV=development node -r source-map-support/register dist/start.js", + "start:dev:debug": "pnpm build:dev && cross-env NODE_ENV=development node -r source-map-support/register --inspect dist/start.js", + "start:prof": "cross-env NODE_ENV=production node -r source-map-support/register --prof dist/start.js", + "start:doctorprof": "cross-env NODE_ENV=production clinic doctor -- node -r source-map-support/register dist/start.js", + "start:flameprof": "cross-env NODE_ENV=production clinic flame -- node -r source-map-support/register dist/start.js", + "start:bubbleprof": "cross-env NODE_ENV=production clinic bubbleprof -- node -r source-map-support/register dist/start.js", + "start:heapprofiler": "cross-env NODE_ENV=production clinic heapprofiler -- node -r source-map-support/register dist/start.js", "rollup": "pnpm build-requirements && rollup --config", "build": "pnpm rollup", "build:cf": "pnpm clean:node_modules && npx cross-env SKIP_PREINSTALL=1 npm install && pnpm rollup", - "build:dev": "pnpm rollup -- --environment BUILD:development", - "build:dev:watch": "pnpm rollup -- --environment BUILD:development --watch", + "build:dev": "pnpm rollup --environment BUILD:development", + "build:dev:analyze": "pnpm rollup --environment ANALYZE,BUILD:development", + "build:dev:watch": "pnpm rollup --environment BUILD:development --watch", "clean:dist": "npx rimraf dist", "clean:node_modules": "npx rimraf node_modules", "lint": "cross-env TIMING=1 eslint --cache --ext .js,.cjs,.mjs,.ts src", diff --git a/rollup.config.mjs b/rollup.config.mjs index a251ff5d..8ca037fb 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -7,6 +7,7 @@ import copy from 'rollup-plugin-copy'; import del from 'rollup-plugin-delete'; const isDevelopmentBuild = process.env.BUILD === 'development'; +const isAnalyzeBuild = process.env.ANALYZE; export default { input: ['src/start.ts', 'src/charging-station/ChargingStationWorker.ts'], @@ -16,10 +17,7 @@ export default { dir: 'dist', format: 'esm', exports: 'auto', - sourcemap: true, - preserveModules: true, - preserveModulesRoot: 'src', - entryFileNames: '[name].mjs', + ...(isDevelopmentBuild && { sourcemap: true }), ...(!isDevelopmentBuild && { plugins: [terser({ maxWorkers: 2 })] }), }, ], @@ -71,6 +69,6 @@ export default { copy({ targets: [{ src: 'src/assets', dest: 'dist/' }], }), - isDevelopmentBuild && analyze(), + isAnalyzeBuild && analyze(), ], }; diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index cdfb26a6..0fd80dde 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -53,8 +53,7 @@ export class Bootstrap { this.initializeCounters(); this.workerImplementation = null; this.workerScript = path.join( - path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../'), - 'charging-station', + path.dirname(fileURLToPath(import.meta.url)), `ChargingStationWorker${path.extname(fileURLToPath(import.meta.url))}` ); Configuration.getUIServer().enabled === true && @@ -277,7 +276,7 @@ export class Bootstrap { await this.workerImplementation?.addElement({ index, templateFile: path.join( - path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../'), + path.dirname(fileURLToPath(import.meta.url)), 'assets', 'station-templates', stationTemplateUrl.file diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index d730a2cb..e1823893 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -91,6 +91,7 @@ import { Configuration, Constants, DCElectricUtils, + ErrorUtils, FileUtils, Utils, logger, @@ -931,7 +932,7 @@ export class ChargingStation { this.templateFileHash = template.templateHash; } } catch (error) { - FileUtils.handleFileException( + ErrorUtils.handleFileException( this.templateFile, FileType.ChargingStationTemplate, error as NodeJS.ErrnoException, @@ -1510,7 +1511,7 @@ export class ChargingStation { this.configurationFileHash = configuration.configurationHash; } } catch (error) { - FileUtils.handleFileException( + ErrorUtils.handleFileException( this.configurationFile, FileType.ChargingStationConfiguration, error as NodeJS.ErrnoException, @@ -1604,7 +1605,7 @@ export class ChargingStation { this.configurationFileHash = configurationHash; }) .catch((error) => { - FileUtils.handleFileException( + ErrorUtils.handleFileException( this.configurationFile, FileType.ChargingStationConfiguration, error as NodeJS.ErrnoException, @@ -1622,7 +1623,7 @@ export class ChargingStation { ); } } catch (error) { - FileUtils.handleFileException( + ErrorUtils.handleFileException( this.configurationFile, FileType.ChargingStationConfiguration, error as NodeJS.ErrnoException, diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index 2a60bb41..93e34ec6 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -550,7 +550,7 @@ export class ChargingStationUtils { return ( stationInfo.idTagsFile && path.join( - path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../'), + path.dirname(fileURLToPath(import.meta.url)), 'assets', path.basename(stationInfo.idTagsFile) ) diff --git a/src/charging-station/IdTagsCache.ts b/src/charging-station/IdTagsCache.ts index ceea0e5b..7d766a40 100644 --- a/src/charging-station/IdTagsCache.ts +++ b/src/charging-station/IdTagsCache.ts @@ -3,7 +3,7 @@ import fs from 'node:fs'; import type { ChargingStation } from './ChargingStation'; import { ChargingStationUtils } from './ChargingStationUtils'; import { FileType, IdTagDistribution } from '../types'; -import { FileUtils, Utils, logger } from '../utils'; +import { ErrorUtils, FileUtils, Utils, logger } from '../utils'; type IdTagsCacheValueType = { idTags: string[]; @@ -114,7 +114,7 @@ export class IdTagsCache { this.deleteIdTagsCache(file); this.deleteIdTagsCacheIndexes(file); } catch (error) { - FileUtils.handleFileException( + ErrorUtils.handleFileException( file, FileType.Authorization, error as NodeJS.ErrnoException, @@ -158,7 +158,7 @@ export class IdTagsCache { // Load id tags file idTags = JSON.parse(fs.readFileSync(file, 'utf8')) as string[]; } catch (error) { - FileUtils.handleFileException( + ErrorUtils.handleFileException( file, FileType.Authorization, error as NodeJS.ErrnoException, diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index a33d0c26..9e4ff3e0 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -143,7 +143,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { [ OCPP16IncomingRequestCommand.RESET, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/Reset.json', + 'assets/json-schemas/ocpp/1.6/Reset.json', moduleName, 'constructor' ), @@ -151,7 +151,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { [ OCPP16IncomingRequestCommand.CLEAR_CACHE, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/ClearCache.json', + 'assets/json-schemas/ocpp/1.6/ClearCache.json', moduleName, 'constructor' ), @@ -159,7 +159,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { [ OCPP16IncomingRequestCommand.UNLOCK_CONNECTOR, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/UnlockConnector.json', + 'assets/json-schemas/ocpp/1.6/UnlockConnector.json', moduleName, 'constructor' ), @@ -167,7 +167,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { [ OCPP16IncomingRequestCommand.GET_CONFIGURATION, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/GetConfiguration.json', + 'assets/json-schemas/ocpp/1.6/GetConfiguration.json', moduleName, 'constructor' ), @@ -175,7 +175,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { [ OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/ChangeConfiguration.json', + 'assets/json-schemas/ocpp/1.6/ChangeConfiguration.json', moduleName, 'constructor' ), @@ -183,7 +183,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { [ OCPP16IncomingRequestCommand.GET_DIAGNOSTICS, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/GetDiagnostics.json', + 'assets/json-schemas/ocpp/1.6/GetDiagnostics.json', moduleName, 'constructor' ), @@ -191,7 +191,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { [ OCPP16IncomingRequestCommand.GET_COMPOSITE_SCHEDULE, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/GetCompositeSchedule.json', + 'assets/json-schemas/ocpp/1.6/GetCompositeSchedule.json', moduleName, 'constructor' ), @@ -199,7 +199,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { [ OCPP16IncomingRequestCommand.SET_CHARGING_PROFILE, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/SetChargingProfile.json', + 'assets/json-schemas/ocpp/1.6/SetChargingProfile.json', moduleName, 'constructor' ), @@ -207,7 +207,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { [ OCPP16IncomingRequestCommand.CLEAR_CHARGING_PROFILE, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/ClearChargingProfile.json', + 'assets/json-schemas/ocpp/1.6/ClearChargingProfile.json', moduleName, 'constructor' ), @@ -215,7 +215,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { [ OCPP16IncomingRequestCommand.CHANGE_AVAILABILITY, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/ChangeAvailability.json', + 'assets/json-schemas/ocpp/1.6/ChangeAvailability.json', moduleName, 'constructor' ), @@ -223,7 +223,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { [ OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/RemoteStartTransaction.json', + 'assets/json-schemas/ocpp/1.6/RemoteStartTransaction.json', moduleName, 'constructor' ), @@ -231,7 +231,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { [ OCPP16IncomingRequestCommand.REMOTE_STOP_TRANSACTION, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/RemoteStopTransaction.json', + 'assets/json-schemas/ocpp/1.6/RemoteStopTransaction.json', moduleName, 'constructor' ), @@ -239,7 +239,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { [ OCPP16IncomingRequestCommand.TRIGGER_MESSAGE, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/TriggerMessage.json', + 'assets/json-schemas/ocpp/1.6/TriggerMessage.json', moduleName, 'constructor' ), @@ -247,7 +247,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { [ OCPP16IncomingRequestCommand.DATA_TRANSFER, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/DataTransfer.json', + 'assets/json-schemas/ocpp/1.6/DataTransfer.json', moduleName, 'constructor' ), @@ -255,7 +255,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { [ OCPP16IncomingRequestCommand.UPDATE_FIRMWARE, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/UpdateFirmware.json', + 'assets/json-schemas/ocpp/1.6/UpdateFirmware.json', moduleName, 'constructor' ), @@ -1244,7 +1244,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { let ftpClient: Client; try { const logFiles = fs - .readdirSync(path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../../../')) + .readdirSync(path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../')) .filter((file) => file.endsWith('.log')) .map((file) => path.join('./', file)); const diagnosticsArchive = `${chargingStation.stationInfo.chargingStationId}_logs.tar.gz`; @@ -1282,7 +1282,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { }); uploadResponse = await ftpClient.uploadFrom( path.join( - path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../../../'), + path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../'), diagnosticsArchive ), `${uri.pathname}${diagnosticsArchive}` diff --git a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts index 8ff6bea4..7ff1fdb8 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -42,7 +42,7 @@ export class OCPP16RequestService extends OCPPRequestService { [ OCPP16RequestCommand.AUTHORIZE, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/Authorize.json', + 'assets/json-schemas/ocpp/1.6/Authorize.json', moduleName, 'constructor' ), @@ -50,7 +50,7 @@ export class OCPP16RequestService extends OCPPRequestService { [ OCPP16RequestCommand.BOOT_NOTIFICATION, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/BootNotification.json', + 'assets/json-schemas/ocpp/1.6/BootNotification.json', moduleName, 'constructor' ), @@ -58,7 +58,7 @@ export class OCPP16RequestService extends OCPPRequestService { [ OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotification.json', + 'assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotification.json', moduleName, 'constructor' ), @@ -66,7 +66,7 @@ export class OCPP16RequestService extends OCPPRequestService { [ OCPP16RequestCommand.HEARTBEAT, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/Heartbeat.json', + 'assets/json-schemas/ocpp/1.6/Heartbeat.json', moduleName, 'constructor' ), @@ -74,7 +74,7 @@ export class OCPP16RequestService extends OCPPRequestService { [ OCPP16RequestCommand.METER_VALUES, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/MeterValues.json', + 'assets/json-schemas/ocpp/1.6/MeterValues.json', moduleName, 'constructor' ), @@ -82,7 +82,7 @@ export class OCPP16RequestService extends OCPPRequestService { [ OCPP16RequestCommand.STATUS_NOTIFICATION, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/StatusNotification.json', + 'assets/json-schemas/ocpp/1.6/StatusNotification.json', moduleName, 'constructor' ), @@ -90,7 +90,7 @@ export class OCPP16RequestService extends OCPPRequestService { [ OCPP16RequestCommand.START_TRANSACTION, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/StartTransaction.json', + 'assets/json-schemas/ocpp/1.6/StartTransaction.json', moduleName, 'constructor' ), @@ -98,7 +98,7 @@ export class OCPP16RequestService extends OCPPRequestService { [ OCPP16RequestCommand.STOP_TRANSACTION, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/StopTransaction.json', + 'assets/json-schemas/ocpp/1.6/StopTransaction.json', moduleName, 'constructor' ), @@ -106,7 +106,7 @@ export class OCPP16RequestService extends OCPPRequestService { [ OCPP16RequestCommand.DATA_TRANSFER, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/DataTransfer.json', + 'assets/json-schemas/ocpp/1.6/DataTransfer.json', moduleName, 'constructor' ), @@ -114,7 +114,7 @@ export class OCPP16RequestService extends OCPPRequestService { [ OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/FirmwareStatusNotification.json', + 'assets/json-schemas/ocpp/1.6/FirmwareStatusNotification.json', moduleName, 'constructor' ), diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index 8703c7e5..2aaeb23d 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -85,7 +85,7 @@ export class OCPP16ResponseService extends OCPPResponseService { [ OCPP16RequestCommand.BOOT_NOTIFICATION, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/BootNotificationResponse.json', + 'assets/json-schemas/ocpp/1.6/BootNotificationResponse.json', moduleName, 'constructor' ), @@ -93,7 +93,7 @@ export class OCPP16ResponseService extends OCPPResponseService { [ OCPP16RequestCommand.HEARTBEAT, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/HeartbeatResponse.json', + 'assets/json-schemas/ocpp/1.6/HeartbeatResponse.json', moduleName, 'constructor' ), @@ -101,7 +101,7 @@ export class OCPP16ResponseService extends OCPPResponseService { [ OCPP16RequestCommand.AUTHORIZE, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/AuthorizeResponse.json', + 'assets/json-schemas/ocpp/1.6/AuthorizeResponse.json', moduleName, 'constructor' ), @@ -109,7 +109,7 @@ export class OCPP16ResponseService extends OCPPResponseService { [ OCPP16RequestCommand.START_TRANSACTION, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/StartTransactionResponse.json', + 'assets/json-schemas/ocpp/1.6/StartTransactionResponse.json', moduleName, 'constructor' ), @@ -117,7 +117,7 @@ export class OCPP16ResponseService extends OCPPResponseService { [ OCPP16RequestCommand.STOP_TRANSACTION, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/StopTransactionResponse.json', + 'assets/json-schemas/ocpp/1.6/StopTransactionResponse.json', moduleName, 'constructor' ), @@ -125,7 +125,7 @@ export class OCPP16ResponseService extends OCPPResponseService { [ OCPP16RequestCommand.STATUS_NOTIFICATION, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/StatusNotificationResponse.json', + 'assets/json-schemas/ocpp/1.6/StatusNotificationResponse.json', moduleName, 'constructor' ), @@ -133,7 +133,7 @@ export class OCPP16ResponseService extends OCPPResponseService { [ OCPP16RequestCommand.METER_VALUES, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/MeterValuesResponse.json', + 'assets/json-schemas/ocpp/1.6/MeterValuesResponse.json', moduleName, 'constructor' ), @@ -141,7 +141,7 @@ export class OCPP16ResponseService extends OCPPResponseService { [ OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotificationResponse.json', + 'assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotificationResponse.json', moduleName, 'constructor' ), @@ -149,7 +149,7 @@ export class OCPP16ResponseService extends OCPPResponseService { [ OCPP16RequestCommand.DATA_TRANSFER, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/DataTransferResponse.json', + 'assets/json-schemas/ocpp/1.6/DataTransferResponse.json', moduleName, 'constructor' ), @@ -157,7 +157,7 @@ export class OCPP16ResponseService extends OCPPResponseService { [ OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/FirmwareStatusNotificationResponse.json', + 'assets/json-schemas/ocpp/1.6/FirmwareStatusNotificationResponse.json', moduleName, 'constructor' ), @@ -167,7 +167,7 @@ export class OCPP16ResponseService extends OCPPResponseService { [ OCPP16IncomingRequestCommand.RESET, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/ResetResponse.json', + 'assets/json-schemas/ocpp/1.6/ResetResponse.json', moduleName, 'constructor' ), @@ -175,7 +175,7 @@ export class OCPP16ResponseService extends OCPPResponseService { [ OCPP16IncomingRequestCommand.CLEAR_CACHE, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/ClearCacheResponse.json', + 'assets/json-schemas/ocpp/1.6/ClearCacheResponse.json', moduleName, 'constructor' ), @@ -183,7 +183,7 @@ export class OCPP16ResponseService extends OCPPResponseService { [ OCPP16IncomingRequestCommand.CHANGE_AVAILABILITY, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/ChangeAvailabilityResponse.json', + 'assets/json-schemas/ocpp/1.6/ChangeAvailabilityResponse.json', moduleName, 'constructor' ), @@ -191,7 +191,7 @@ export class OCPP16ResponseService extends OCPPResponseService { [ OCPP16IncomingRequestCommand.UNLOCK_CONNECTOR, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/UnlockConnectorResponse.json', + 'assets/json-schemas/ocpp/1.6/UnlockConnectorResponse.json', moduleName, 'constructor' ), @@ -199,7 +199,7 @@ export class OCPP16ResponseService extends OCPPResponseService { [ OCPP16IncomingRequestCommand.GET_CONFIGURATION, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/GetConfigurationResponse.json', + 'assets/json-schemas/ocpp/1.6/GetConfigurationResponse.json', moduleName, 'constructor' ), @@ -207,7 +207,7 @@ export class OCPP16ResponseService extends OCPPResponseService { [ OCPP16IncomingRequestCommand.CHANGE_CONFIGURATION, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/ChangeConfigurationResponse.json', + 'assets/json-schemas/ocpp/1.6/ChangeConfigurationResponse.json', moduleName, 'constructor' ), @@ -215,7 +215,7 @@ export class OCPP16ResponseService extends OCPPResponseService { [ OCPP16IncomingRequestCommand.GET_COMPOSITE_SCHEDULE, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/GetCompositeScheduleResponse.json', + 'assets/json-schemas/ocpp/1.6/GetCompositeScheduleResponse.json', moduleName, 'constructor' ), @@ -223,7 +223,7 @@ export class OCPP16ResponseService extends OCPPResponseService { [ OCPP16IncomingRequestCommand.SET_CHARGING_PROFILE, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/SetChargingProfileResponse.json', + 'assets/json-schemas/ocpp/1.6/SetChargingProfileResponse.json', moduleName, 'constructor' ), @@ -231,7 +231,7 @@ export class OCPP16ResponseService extends OCPPResponseService { [ OCPP16IncomingRequestCommand.CLEAR_CHARGING_PROFILE, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/ClearChargingProfileResponse.json', + 'assets/json-schemas/ocpp/1.6/ClearChargingProfileResponse.json', moduleName, 'constructor' ), @@ -239,7 +239,7 @@ export class OCPP16ResponseService extends OCPPResponseService { [ OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/RemoteStartTransactionResponse.json', + 'assets/json-schemas/ocpp/1.6/RemoteStartTransactionResponse.json', moduleName, 'constructor' ), @@ -247,7 +247,7 @@ export class OCPP16ResponseService extends OCPPResponseService { [ OCPP16IncomingRequestCommand.REMOTE_STOP_TRANSACTION, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/RemoteStopTransactionResponse.json', + 'assets/json-schemas/ocpp/1.6/RemoteStopTransactionResponse.json', moduleName, 'constructor' ), @@ -255,7 +255,7 @@ export class OCPP16ResponseService extends OCPPResponseService { [ OCPP16IncomingRequestCommand.GET_DIAGNOSTICS, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/GetDiagnosticsResponse.json', + 'assets/json-schemas/ocpp/1.6/GetDiagnosticsResponse.json', moduleName, 'constructor' ), @@ -263,7 +263,7 @@ export class OCPP16ResponseService extends OCPPResponseService { [ OCPP16IncomingRequestCommand.TRIGGER_MESSAGE, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/TriggerMessageResponse.json', + 'assets/json-schemas/ocpp/1.6/TriggerMessageResponse.json', moduleName, 'constructor' ), @@ -271,7 +271,7 @@ export class OCPP16ResponseService extends OCPPResponseService { [ OCPP16IncomingRequestCommand.DATA_TRANSFER, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/DataTransferResponse.json', + 'assets/json-schemas/ocpp/1.6/DataTransferResponse.json', moduleName, 'constructor' ), @@ -279,7 +279,7 @@ export class OCPP16ResponseService extends OCPPResponseService { [ OCPP16IncomingRequestCommand.UPDATE_FIRMWARE, OCPP16ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/1.6/UpdateFirmwareResponse.json', + 'assets/json-schemas/ocpp/1.6/UpdateFirmwareResponse.json', moduleName, 'constructor' ), diff --git a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts index 68f4a6ea..23381f9e 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts @@ -1,8 +1,5 @@ // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. -import path from 'node:path'; -import { fileURLToPath } from 'node:url'; - import type { JSONSchemaType } from 'ajv'; import type { ChargingStation } from '../../../charging-station'; @@ -833,7 +830,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { methodName?: string ): JSONSchemaType { return super.parseJsonSchemaFile( - path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath), + relativePath, OCPPVersion.VERSION_16, moduleName, methodName diff --git a/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts b/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts index 143eee88..3fdb5ced 100644 --- a/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts @@ -35,7 +35,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService { [ OCPP20IncomingRequestCommand.CLEAR_CACHE, OCPP20ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/2.0/ClearCacheRequest.json', + 'assets/json-schemas/ocpp/2.0/ClearCacheRequest.json', moduleName, 'constructor' ), diff --git a/src/charging-station/ocpp/2.0/OCPP20RequestService.ts b/src/charging-station/ocpp/2.0/OCPP20RequestService.ts index 9faa7952..6e857089 100644 --- a/src/charging-station/ocpp/2.0/OCPP20RequestService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20RequestService.ts @@ -35,7 +35,7 @@ export class OCPP20RequestService extends OCPPRequestService { [ OCPP20RequestCommand.BOOT_NOTIFICATION, OCPP20ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/2.0/BootNotificationRequest.json', + 'assets/json-schemas/ocpp/2.0/BootNotificationRequest.json', moduleName, 'constructor' ), @@ -43,7 +43,7 @@ export class OCPP20RequestService extends OCPPRequestService { [ OCPP20RequestCommand.HEARTBEAT, OCPP20ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/2.0/HeartbeatRequest.json', + 'assets/json-schemas/ocpp/2.0/HeartbeatRequest.json', moduleName, 'constructor' ), @@ -51,7 +51,7 @@ export class OCPP20RequestService extends OCPPRequestService { [ OCPP20RequestCommand.STATUS_NOTIFICATION, OCPP20ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/2.0/StatusNotificationRequest.json', + 'assets/json-schemas/ocpp/2.0/StatusNotificationRequest.json', moduleName, 'constructor' ), diff --git a/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts b/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts index 535014ff..50566300 100644 --- a/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts @@ -48,7 +48,7 @@ export class OCPP20ResponseService extends OCPPResponseService { [ OCPP20RequestCommand.BOOT_NOTIFICATION, OCPP20ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/2.0/BootNotificationResponse.json', + 'assets/json-schemas/ocpp/2.0/BootNotificationResponse.json', moduleName, 'constructor' ), @@ -56,7 +56,7 @@ export class OCPP20ResponseService extends OCPPResponseService { [ OCPP20RequestCommand.HEARTBEAT, OCPP20ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/2.0/HeartbeatResponse.json', + 'assets/json-schemas/ocpp/2.0/HeartbeatResponse.json', moduleName, 'constructor' ), @@ -64,7 +64,7 @@ export class OCPP20ResponseService extends OCPPResponseService { [ OCPP20RequestCommand.STATUS_NOTIFICATION, OCPP20ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/2.0/StatusNotificationResponse.json', + 'assets/json-schemas/ocpp/2.0/StatusNotificationResponse.json', moduleName, 'constructor' ), @@ -74,7 +74,7 @@ export class OCPP20ResponseService extends OCPPResponseService { [ OCPP20IncomingRequestCommand.CLEAR_CACHE, OCPP20ServiceUtils.parseJsonSchemaFile( - '../../../assets/json-schemas/ocpp/2.0/ClearCacheResponse.json', + 'assets/json-schemas/ocpp/2.0/ClearCacheResponse.json', moduleName, 'constructor' ), diff --git a/src/charging-station/ocpp/2.0/OCPP20ServiceUtils.ts b/src/charging-station/ocpp/2.0/OCPP20ServiceUtils.ts index 38b80968..8d6ccdf5 100644 --- a/src/charging-station/ocpp/2.0/OCPP20ServiceUtils.ts +++ b/src/charging-station/ocpp/2.0/OCPP20ServiceUtils.ts @@ -1,8 +1,5 @@ // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. -import path from 'node:path'; -import { fileURLToPath } from 'node:url'; - import type { JSONSchemaType } from 'ajv'; import { type JsonType, OCPPVersion } from '../../../types'; @@ -15,7 +12,7 @@ export class OCPP20ServiceUtils extends OCPPServiceUtils { methodName?: string ): JSONSchemaType { return super.parseJsonSchemaFile( - path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath), + relativePath, OCPPVersion.VERSION_20, moduleName, methodName diff --git a/src/charging-station/ocpp/OCPPServiceUtils.ts b/src/charging-station/ocpp/OCPPServiceUtils.ts index 9e8db34e..55a1017a 100644 --- a/src/charging-station/ocpp/OCPPServiceUtils.ts +++ b/src/charging-station/ocpp/OCPPServiceUtils.ts @@ -1,4 +1,6 @@ import fs from 'node:fs'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; import type { DefinedError, ErrorObject, JSONSchemaType } from 'ajv'; @@ -27,7 +29,7 @@ import { type StatusNotificationRequest, type StatusNotificationResponse, } from '../../types'; -import { Constants, FileUtils, Utils, logger } from '../../utils'; +import { Constants, ErrorUtils, Utils, logger } from '../../utils'; export class OCPPServiceUtils { protected constructor() { @@ -252,15 +254,16 @@ export class OCPPServiceUtils { } protected static parseJsonSchemaFile( - filePath: string, + relativePath: string, ocppVersion: OCPPVersion, moduleName?: string, methodName?: string ): JSONSchemaType { + const filePath = path.join(path.dirname(fileURLToPath(import.meta.url)), relativePath); try { return JSON.parse(fs.readFileSync(filePath, 'utf8')) as JSONSchemaType; } catch (error) { - FileUtils.handleFileException( + ErrorUtils.handleFileException( filePath, FileType.JsonSchema, error as NodeJS.ErrnoException, diff --git a/src/performance/storage/JsonFileStorage.ts b/src/performance/storage/JsonFileStorage.ts index e94d79b9..50d1a8b9 100644 --- a/src/performance/storage/JsonFileStorage.ts +++ b/src/performance/storage/JsonFileStorage.ts @@ -5,7 +5,7 @@ import path from 'node:path'; import { Storage } from './Storage'; import { FileType, type Statistics } from '../../types'; -import { AsyncLock, AsyncLockType, Constants, FileUtils, Utils } from '../../utils'; +import { AsyncLock, AsyncLockType, Constants, ErrorUtils, Utils } from '../../utils'; export class JsonFileStorage extends Storage { private fd: number | null = null; @@ -31,7 +31,7 @@ export class JsonFileStorage extends Storage { ); }) .catch((error) => { - FileUtils.handleFileException( + ErrorUtils.handleFileException( this.dbName, FileType.PerformanceRecords, error as NodeJS.ErrnoException, @@ -52,7 +52,7 @@ export class JsonFileStorage extends Storage { this.fd = fs.openSync(this.dbName, 'a+'); } } catch (error) { - FileUtils.handleFileException( + ErrorUtils.handleFileException( this.dbName, FileType.PerformanceRecords, error as NodeJS.ErrnoException, @@ -68,7 +68,7 @@ export class JsonFileStorage extends Storage { this.fd = null; } } catch (error) { - FileUtils.handleFileException( + ErrorUtils.handleFileException( this.dbName, FileType.PerformanceRecords, error as NodeJS.ErrnoException, diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index 0ed294c0..257f5f65 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -23,7 +23,7 @@ import { WorkerConstants, WorkerProcessType } from '../worker'; export class Configuration { private static configurationFile = path.join( - path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../'), + path.dirname(fileURLToPath(import.meta.url)), 'assets', 'config.json' ); @@ -426,7 +426,7 @@ export class Configuration { private static buildPerformanceUriFilePath(file: string) { return `file://${path.join( - path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../'), + path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../'), file )}`; } diff --git a/src/utils/ErrorUtils.ts b/src/utils/ErrorUtils.ts new file mode 100644 index 00000000..0589fa19 --- /dev/null +++ b/src/utils/ErrorUtils.ts @@ -0,0 +1,43 @@ +import chalk from 'chalk'; + +import { logger } from './Logger'; +import { Utils } from './Utils'; +import type { EmptyObject, FileType, HandleErrorParams } from '../types'; + +export class ErrorUtils { + private constructor() { + // This is intentional + } + + public static handleFileException( + file: string, + fileType: FileType, + error: NodeJS.ErrnoException, + logPrefix: string, + params: HandleErrorParams = { throwError: true, consoleOut: false } + ): void { + const prefix = Utils.isNotEmptyString(logPrefix) ? `${logPrefix} ` : ''; + let logMsg: string; + switch (error.code) { + case 'ENOENT': + logMsg = `${fileType} file ${file} not found:`; + break; + case 'EEXIST': + logMsg = `${fileType} file ${file} already exists:`; + break; + case 'EACCES': + logMsg = `${fileType} file ${file} access denied:`; + break; + default: + logMsg = `${fileType} file ${file} error:`; + } + if (params?.consoleOut) { + console.warn(`${chalk.green(prefix)}${chalk.yellow(`${logMsg} `)}`, error); + } else { + logger.warn(`${prefix}${logMsg}`, error); + } + if (params?.throwError) { + throw error; + } + } +} diff --git a/src/utils/FileUtils.ts b/src/utils/FileUtils.ts index e08fb128..4e556a4b 100644 --- a/src/utils/FileUtils.ts +++ b/src/utils/FileUtils.ts @@ -1,10 +1,9 @@ import fs from 'node:fs'; -import chalk from 'chalk'; - +import { ErrorUtils } from './ErrorUtils'; import { logger } from './Logger'; import { Utils } from './Utils'; -import type { EmptyObject, FileType, HandleErrorParams, JsonType } from '../types'; +import type { FileType, JsonType } from '../types'; export class FileUtils { private constructor() { @@ -22,9 +21,15 @@ export class FileUtils { logger.debug(`${logPrefix} ${fileType} file ${file} have changed, reload`); refreshedVariable && (refreshedVariable = JSON.parse(fs.readFileSync(file, 'utf8')) as T); } catch (error) { - FileUtils.handleFileException(file, fileType, error as NodeJS.ErrnoException, logPrefix, { - throwError: false, - }); + ErrorUtils.handleFileException( + file, + fileType, + error as NodeJS.ErrnoException, + logPrefix, + { + throwError: false, + } + ); } } } @@ -33,7 +38,7 @@ export class FileUtils { try { return fs.watch(file, listener); } catch (error) { - FileUtils.handleFileException(file, fileType, error as NodeJS.ErrnoException, logPrefix, { + ErrorUtils.handleFileException(file, fileType, error as NodeJS.ErrnoException, logPrefix, { throwError: false, }); } @@ -41,36 +46,4 @@ export class FileUtils { logger.info(`${logPrefix} No ${fileType} file to watch given. Not monitoring its changes`); } } - - public static handleFileException( - file: string, - fileType: FileType, - error: NodeJS.ErrnoException, - logPrefix: string, - params: HandleErrorParams = { throwError: true, consoleOut: false } - ): void { - const prefix = Utils.isNotEmptyString(logPrefix) ? `${logPrefix} ` : ''; - let logMsg: string; - switch (error.code) { - case 'ENOENT': - logMsg = `${fileType} file ${file} not found:`; - break; - case 'EEXIST': - logMsg = `${fileType} file ${file} already exists:`; - break; - case 'EACCES': - logMsg = `${fileType} file ${file} access denied:`; - break; - default: - logMsg = `${fileType} file ${file} error:`; - } - if (params?.consoleOut) { - console.warn(`${chalk.green(prefix)}${chalk.yellow(`${logMsg} `)}`, error); - } else { - logger.warn(`${prefix}${logMsg}`, error); - } - if (params?.throwError) { - throw error; - } - } } diff --git a/src/utils/index.ts b/src/utils/index.ts index 6a68f64f..32d351bc 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -3,6 +3,7 @@ export { AsyncLock, AsyncLockType } from './AsyncLock'; export { CircularArray } from './CircularArray'; export { Configuration } from './Configuration'; export { Constants } from './Constants'; +export { ErrorUtils } from './ErrorUtils'; export { FileUtils } from './FileUtils'; export { Utils } from './Utils'; export { logger } from './Logger'; -- 2.34.1