build(simulator): don't preserve modules
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 20 May 2023 12:29:28 +0000 (14:29 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 20 May 2023 12:29:28 +0000 (14:29 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
25 files changed:
.vscode/settings.json
docker/Dockerfile
docker/start.sh
manifest-cf-template.yml
mikro-orm.config-template.ts
package.json
rollup.config.mjs
src/charging-station/Bootstrap.ts
src/charging-station/ChargingStation.ts
src/charging-station/ChargingStationUtils.ts
src/charging-station/IdTagsCache.ts
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/charging-station/ocpp/1.6/OCPP16RequestService.ts
src/charging-station/ocpp/1.6/OCPP16ResponseService.ts
src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts
src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts
src/charging-station/ocpp/2.0/OCPP20RequestService.ts
src/charging-station/ocpp/2.0/OCPP20ResponseService.ts
src/charging-station/ocpp/2.0/OCPP20ServiceUtils.ts
src/charging-station/ocpp/OCPPServiceUtils.ts
src/performance/storage/JsonFileStorage.ts
src/utils/Configuration.ts
src/utils/ErrorUtils.ts [new file with mode: 0644]
src/utils/FileUtils.ts
src/utils/index.ts

index d32d9402f00fe6598f4e92abeee9a4d2785d93a9..b619a9aac064c468a02e3f2fe0fa9c9efdf8d3f2 100644 (file)
@@ -16,6 +16,7 @@
     "cacheable",
     "commitlint",
     "CSMS",
+    "emobility",
     "evse",
     "evses",
     "iccid",
index 96fefe3b4325d83e31e4236157a0022e21f14d33..d1f2135fbf3fa4a9b42b456c2f4deccc83a5771b 100644 (file)
@@ -1,4 +1,4 @@
-FROM node:lts-alpine as builder
+FROM node:lts-alpine AS builder
 
 # Build simulator
 WORKDIR /usr/builder
index fa3b0b9530a90f1ddbf015f668f06e599ae15acd..a244fcafb83e6836b917c9995b292b6f6419ac75 100755 (executable)
@@ -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
index a7d07501739c05d270c36bae35eb62eaef72ecb1..4a4a2258df0e4303e8131a4b36ab8d97d8ef9cf4 100644 (file)
@@ -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
index 069d27280c949c0ddd8b520413d5589af0bf7bea..b89b975f2b1e6d7b918c34722eece50d8f2853b1 100644 (file)
@@ -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`
   )}`,
 };
index e68aa9f88f70f80d445b7f6e8517956f00dc6cd0..a75431fd3dfc4058cfcdb908ab616f7fe80bb40d 100644 (file)
@@ -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}",
     "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",
index a251ff5dab1ce283387aaac2246466b27be669ee..8ca037fb9f24753c042ee475b786197d2c36d51e 100644 (file)
@@ -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(),
   ],
 };
index cdfb26a6c615cd08e6e9de1140b29d0558f5f91a..0fd80dde46bfc349ee91a9d55c3d8f168fbc19fd 100644 (file)
@@ -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
index d730a2cb04f0bd5b37c825ce9b0a2def3b6fa16f..e1823893648d589eef6bc9761306b624cc38dc19 100644 (file)
@@ -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,
index 2a60bb41958c85048833a0b95b2b5b75370f40b4..93e34ec69024b8dfe7dbe7e86e50614eec5180be 100644 (file)
@@ -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)
       )
index ceea0e5bd084c3cada92d63c4c88d7905517d63e..7d766a40d93d6fb1eebf39bfdb873542a7cd7dba 100644 (file)
@@ -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,
index a33d0c26a60bb362f449c29d65b2bac57dd23e41..9e4ff3e0d9f19b839d5f17d91b92d02a43407838 100644 (file)
@@ -143,7 +143,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
       [
         OCPP16IncomingRequestCommand.RESET,
         OCPP16ServiceUtils.parseJsonSchemaFile<ResetRequest>(
-          '../../../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<OCPP16ClearCacheRequest>(
-          '../../../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<UnlockConnectorRequest>(
-          '../../../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<GetConfigurationRequest>(
-          '../../../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<ChangeConfigurationRequest>(
-          '../../../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<GetDiagnosticsRequest>(
-          '../../../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<OCPP16GetCompositeScheduleRequest>(
-          '../../../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<SetChargingProfileRequest>(
-          '../../../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<ClearChargingProfileRequest>(
-          '../../../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<ChangeAvailabilityRequest>(
-          '../../../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<RemoteStartTransactionRequest>(
-          '../../../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<RemoteStopTransactionRequest>(
-          '../../../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<OCPP16TriggerMessageRequest>(
-          '../../../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<OCPP16DataTransferRequest>(
-          '../../../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<OCPP16UpdateFirmwareRequest>(
-          '../../../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}`
index 8ff6bea4b08406cfbcca392d456d3eca5e79b67b..7ff1fdb8d43b27da48147a789a1612c87644d8a4 100644 (file)
@@ -42,7 +42,7 @@ export class OCPP16RequestService extends OCPPRequestService {
       [
         OCPP16RequestCommand.AUTHORIZE,
         OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16AuthorizeRequest>(
-          '../../../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<OCPP16BootNotificationRequest>(
-          '../../../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<OCPP16DiagnosticsStatusNotificationRequest>(
-          '../../../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<OCPP16HeartbeatRequest>(
-          '../../../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<OCPP16MeterValuesRequest>(
-          '../../../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<OCPP16StatusNotificationRequest>(
-          '../../../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<OCPP16StartTransactionRequest>(
-          '../../../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<OCPP16StopTransactionRequest>(
-          '../../../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<OCPP16DataTransferRequest>(
-          '../../../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<OCPP16FirmwareStatusNotificationRequest>(
-          '../../../assets/json-schemas/ocpp/1.6/FirmwareStatusNotification.json',
+          'assets/json-schemas/ocpp/1.6/FirmwareStatusNotification.json',
           moduleName,
           'constructor'
         ),
index 8703c7e5cda5a110daba61ae257ddc255ae19d3a..2aaeb23dccfb6107dcd4b1f4ea5ead4539b2965f 100644 (file)
@@ -85,7 +85,7 @@ export class OCPP16ResponseService extends OCPPResponseService {
       [
         OCPP16RequestCommand.BOOT_NOTIFICATION,
         OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16BootNotificationResponse>(
-          '../../../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<OCPP16HeartbeatResponse>(
-          '../../../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<OCPP16AuthorizeResponse>(
-          '../../../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<OCPP16StartTransactionResponse>(
-          '../../../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<OCPP16StopTransactionResponse>(
-          '../../../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<OCPP16StatusNotificationResponse>(
-          '../../../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<OCPP16MeterValuesResponse>(
-          '../../../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<OCPP16DiagnosticsStatusNotificationResponse>(
-          '../../../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<OCPP16DataTransferResponse>(
-          '../../../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<OCPP16FirmwareStatusNotificationResponse>(
-          '../../../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<GenericResponse>(
-          '../../../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<GenericResponse>(
-          '../../../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<ChangeAvailabilityResponse>(
-          '../../../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<UnlockConnectorResponse>(
-          '../../../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<GetConfigurationResponse>(
-          '../../../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<ChangeConfigurationResponse>(
-          '../../../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<OCPP16GetCompositeScheduleResponse>(
-          '../../../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<SetChargingProfileResponse>(
-          '../../../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<ClearChargingProfileResponse>(
-          '../../../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<GenericResponse>(
-          '../../../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<GenericResponse>(
-          '../../../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<GetDiagnosticsResponse>(
-          '../../../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<OCPP16TriggerMessageResponse>(
-          '../../../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<OCPP16DataTransferResponse>(
-          '../../../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<OCPP16UpdateFirmwareResponse>(
-          '../../../assets/json-schemas/ocpp/1.6/UpdateFirmwareResponse.json',
+          'assets/json-schemas/ocpp/1.6/UpdateFirmwareResponse.json',
           moduleName,
           'constructor'
         ),
index 68f4a6ea5a50271ecf1a65360d38079227e10fe5..23381f9ec3dd63e5e47faed0301fc6fecbc01f75 100644 (file)
@@ -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<T> {
     return super.parseJsonSchemaFile<T>(
-      path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath),
+      relativePath,
       OCPPVersion.VERSION_16,
       moduleName,
       methodName
index 143eee889aba3c47d07d8f76684c0af191fe5bda..3fdb5cedf1b784c5b90c93706e05596557f0afe5 100644 (file)
@@ -35,7 +35,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService {
       [
         OCPP20IncomingRequestCommand.CLEAR_CACHE,
         OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20ClearCacheRequest>(
-          '../../../assets/json-schemas/ocpp/2.0/ClearCacheRequest.json',
+          'assets/json-schemas/ocpp/2.0/ClearCacheRequest.json',
           moduleName,
           'constructor'
         ),
index 9faa7952455ab63605cabf2d2f2ac8e4530dfc2a..6e857089d96a567c99e0599de561dd56eaccb990 100644 (file)
@@ -35,7 +35,7 @@ export class OCPP20RequestService extends OCPPRequestService {
       [
         OCPP20RequestCommand.BOOT_NOTIFICATION,
         OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20BootNotificationRequest>(
-          '../../../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<OCPP20HeartbeatRequest>(
-          '../../../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<OCPP20StatusNotificationRequest>(
-          '../../../assets/json-schemas/ocpp/2.0/StatusNotificationRequest.json',
+          'assets/json-schemas/ocpp/2.0/StatusNotificationRequest.json',
           moduleName,
           'constructor'
         ),
index 535014ff7bea609eafdeedba69875d6f463b28ff..50566300231ddfe3347870f3234737d64d31df9e 100644 (file)
@@ -48,7 +48,7 @@ export class OCPP20ResponseService extends OCPPResponseService {
       [
         OCPP20RequestCommand.BOOT_NOTIFICATION,
         OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20BootNotificationResponse>(
-          '../../../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<OCPP20HeartbeatResponse>(
-          '../../../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<OCPP20StatusNotificationResponse>(
-          '../../../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<OCPP20ClearCacheResponse>(
-          '../../../assets/json-schemas/ocpp/2.0/ClearCacheResponse.json',
+          'assets/json-schemas/ocpp/2.0/ClearCacheResponse.json',
           moduleName,
           'constructor'
         ),
index 38b80968a4327cd5c00660321e3f4d77cb671a3a..8d6ccdf5b3ff9bd2b54d100beb7e3144fab65b26 100644 (file)
@@ -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<T> {
     return super.parseJsonSchemaFile<T>(
-      path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath),
+      relativePath,
       OCPPVersion.VERSION_20,
       moduleName,
       methodName
index 9e8db34edf795e5d3f882a6cbf8557d21c08efa7..55a1017a1cdbce5e46bdc397d5f08d44ad798d62 100644 (file)
@@ -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<T extends JsonType>(
-    filePath: string,
+    relativePath: string,
     ocppVersion: OCPPVersion,
     moduleName?: string,
     methodName?: string
   ): JSONSchemaType<T> {
+    const filePath = path.join(path.dirname(fileURLToPath(import.meta.url)), relativePath);
     try {
       return JSON.parse(fs.readFileSync(filePath, 'utf8')) as JSONSchemaType<T>;
     } catch (error) {
-      FileUtils.handleFileException(
+      ErrorUtils.handleFileException(
         filePath,
         FileType.JsonSchema,
         error as NodeJS.ErrnoException,
index e94d79b97d337826bcc4c7bc74d698d5211b1e04..50d1a8b925e0b46862c22c41d77b3fc1f9e5d078 100644 (file)
@@ -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,
index 0ed294c016385bc9d3993d6f55bde0cd52593e36..257f5f658fe9819be910ec5b3d63e6d9ab79f2ae 100644 (file)
@@ -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 (file)
index 0000000..0589fa1
--- /dev/null
@@ -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<EmptyObject> = { 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;
+    }
+  }
+}
index e08fb128be960b721d99645be66ea11f617419af..4e556a4bc1c3d642c8c9edecbb92049d5e880044 100644 (file)
@@ -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<EmptyObject> = { 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;
-    }
-  }
 }
index 6a68f64f7cdd7492f1e6d627a201d4a948fcb21e..32d351bca5c366d3b61669bb3ae245bfe3eebe48 100644 (file)
@@ -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';