From 1068742241b5ac538aa456555bfb84d25d7099f9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 25 Sep 2023 21:18:33 +0200 Subject: [PATCH] fix: ensure the ATG will start from its saved status MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- build-requirements.mjs | 7 ++++--- prepare.cjs | 6 ++++-- rollup.config.mjs | 6 ++++-- skip-preinstall.cjs | 8 +++++--- src/charging-station/AutomaticTransactionGenerator.ts | 3 +++ src/charging-station/Bootstrap.ts | 9 +++++---- src/charging-station/Helpers.ts | 3 ++- src/scripts/deleteChargingStations.cjs | 2 +- src/scripts/setCSPublicFlag.cjs | 2 +- src/utils/Configuration.ts | 3 ++- src/utils/Utils.ts | 3 ++- ui/web/.eslintrc.js | 5 +++-- ui/web/start.js | 9 +++++---- 13 files changed, 41 insertions(+), 25 deletions(-) diff --git a/build-requirements.mjs b/build-requirements.mjs index 44de1932..839be558 100644 --- a/build-requirements.mjs +++ b/build-requirements.mjs @@ -1,20 +1,21 @@ import chalk from 'chalk'; import semVer from 'semver'; import packageJson from './package.json' assert { type: 'json' }; +import { version, exit } from 'node:process'; /** * Check if the current node version match the required engines version. */ export function checkNodeVersion() { const enginesNodeVersion = packageJson.engines.node; - if (semVer.satisfies(process.version, enginesNodeVersion) === false) { + if (semVer.satisfies(version, enginesNodeVersion) === false) { console.error( chalk.red( - `Required node version ${enginesNodeVersion} not satisfied with current version ${process.version}.`, + `Required node version ${enginesNodeVersion} not satisfied with current version ${version}.`, ), ); // eslint-disable-next-line n/no-process-exit - process.exit(1); + exit(1); } } diff --git a/prepare.cjs b/prepare.cjs index 6160cf59..6038d204 100644 --- a/prepare.cjs +++ b/prepare.cjs @@ -1,5 +1,7 @@ -const isCIEnvironment = process.env.CI !== undefined; -const isCFEnvironment = process.env.VCAP_APPLICATION !== undefined; +const { env } = require('node:process'); + +const isCIEnvironment = env.CI !== undefined; +const isCFEnvironment = env.VCAP_APPLICATION !== undefined; if (isCFEnvironment === false && isCIEnvironment === false) { // eslint-disable-next-line n/no-unpublished-require require('husky').install(); diff --git a/rollup.config.mjs b/rollup.config.mjs index c28aaf04..40f1d679 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -1,5 +1,6 @@ /* eslint-disable n/no-unpublished-import */ import * as os from 'node:os'; +import { env } from 'node:process'; import json from '@rollup/plugin-json'; import terser from '@rollup/plugin-terser'; @@ -23,8 +24,8 @@ const availableParallelism = () => { return availableParallelism; }; -const isDevelopmentBuild = process.env.BUILD === 'development'; -const isAnalyzeBuild = process.env.ANALYZE; +const isDevelopmentBuild = env.BUILD === 'development'; +const isAnalyzeBuild = env.ANALYZE; const sourceMap = !!isDevelopmentBuild; export default defineConfig({ @@ -60,6 +61,7 @@ export default defineConfig({ 'node:http2', 'node:path', 'node:perf_hooks', + 'node:process', 'node:stream', 'node:url', 'node:util', diff --git a/skip-preinstall.cjs b/skip-preinstall.cjs index 8ec526bc..5be82290 100644 --- a/skip-preinstall.cjs +++ b/skip-preinstall.cjs @@ -1,8 +1,10 @@ -const skipPreinstall = process.env.SKIP_PREINSTALL || process.env.VCAP_APPLICATION !== undefined; +const { env, exit } = require('node:process'); + +const skipPreinstall = env.SKIP_PREINSTALL || env.VCAP_APPLICATION !== undefined; if (skipPreinstall) { // eslint-disable-next-line n/no-process-exit - process.exit(); + exit(); } else { // eslint-disable-next-line n/no-process-exit - process.exit(1); + exit(1); } diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index e53026ba..a6858afe 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -363,6 +363,9 @@ export class AutomaticTransactionGenerator extends AsyncResource { delete connectorStatus?.lastRunDate; delete connectorStatus?.stopDate; delete connectorStatus?.stoppedDate; + if (connectorStatus?.start === true) { + connectorStatus.start = false; + } return ( connectorStatus ?? { start: false, diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 4e8b43a9..0cd15055 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -2,6 +2,7 @@ import { EventEmitter } from 'node:events'; import { dirname, extname, join } from 'node:path'; +import { exit } from 'node:process'; import { fileURLToPath } from 'node:url'; import { isMainThread } from 'node:worker_threads'; @@ -354,13 +355,13 @@ export class Bootstrap extends EventEmitter { console.warn( chalk.yellow("'stationTemplateUrls' not defined or empty in configuration, exiting"), ); - process.exit(exitCodes.missingChargingStationsConfiguration); + exit(exitCodes.missingChargingStationsConfiguration); } if (this.numberOfChargingStations === 0) { console.warn( chalk.yellow('No charging station template enabled in configuration, exiting'), ); - process.exit(exitCodes.noChargingStationTemplates); + exit(exitCodes.noChargingStationTemplates); } this.initializedCounters = true; } @@ -391,11 +392,11 @@ export class Bootstrap extends EventEmitter { console.info(`${chalk.green('Graceful shutdown')}`); this.stop() .then(() => { - process.exit(exitCodes.succeeded); + exit(exitCodes.succeeded); }) .catch((error) => { console.error(chalk.red('Error while shutdowning charging stations simulator: '), error); - process.exit(exitCodes.gracefulShutdownError); + exit(exitCodes.gracefulShutdownError); }); }; diff --git a/src/charging-station/Helpers.ts b/src/charging-station/Helpers.ts index 90ecd0e8..99a8f82c 100644 --- a/src/charging-station/Helpers.ts +++ b/src/charging-station/Helpers.ts @@ -1,6 +1,7 @@ import { createHash, randomBytes } from 'node:crypto'; import type { EventEmitter } from 'node:events'; import { basename, dirname, join } from 'node:path'; +import { env } from 'node:process'; import { fileURLToPath } from 'node:url'; import chalk from 'chalk'; @@ -76,7 +77,7 @@ export const getChargingStationId = ( stationTemplate: ChargingStationTemplate, ): string => { // In case of multiple instances: add instance index to charging station id - const instanceIndex = process.env.CF_INSTANCE_INDEX ?? 0; + const instanceIndex = env.CF_INSTANCE_INDEX ?? 0; const idSuffix = stationTemplate?.nameSuffix ?? ''; const idStr = `000000000${index.toString()}`; return stationTemplate?.fixedName diff --git a/src/scripts/deleteChargingStations.cjs b/src/scripts/deleteChargingStations.cjs index 0e8333c3..beffb18c 100755 --- a/src/scripts/deleteChargingStations.cjs +++ b/src/scripts/deleteChargingStations.cjs @@ -1,6 +1,6 @@ #!/usr/bin/env node -const fs = require('fs'); +const fs = require('node:fs'); const { MongoClient } = require('mongodb'); diff --git a/src/scripts/setCSPublicFlag.cjs b/src/scripts/setCSPublicFlag.cjs index 1f412d1b..10dc9d11 100755 --- a/src/scripts/setCSPublicFlag.cjs +++ b/src/scripts/setCSPublicFlag.cjs @@ -1,6 +1,6 @@ #!/usr/bin/env node -const fs = require('fs'); +const fs = require('node:fs'); const { MongoClient } = require('mongodb'); diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index 0c5a356a..53eb720f 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -1,5 +1,6 @@ import { type FSWatcher, readFileSync, watch } from 'node:fs'; import { dirname, join, resolve } from 'node:path'; +import { env } from 'node:process'; import { fileURLToPath } from 'node:url'; import chalk from 'chalk'; @@ -171,7 +172,7 @@ export class Configuration { } if (isCFEnvironment() === true) { delete uiServerConfiguration.options?.host; - uiServerConfiguration.options!.port = parseInt(process.env.PORT!); + uiServerConfiguration.options!.port = parseInt(env.PORT!); } return uiServerConfiguration; } diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 808ef380..6a3d30fe 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -1,4 +1,5 @@ import { randomBytes, randomInt, randomUUID, webcrypto } from 'node:crypto'; +import { env } from 'node:process'; import { inspect } from 'node:util'; import { @@ -260,7 +261,7 @@ export const hasOwnProp = (object: unknown, property: PropertyKey): boolean => { }; export const isCFEnvironment = (): boolean => { - return !isNullOrUndefined(process.env.VCAP_APPLICATION); + return !isNullOrUndefined(env.VCAP_APPLICATION); }; export const isIterable = (obj: T): boolean => { diff --git a/ui/web/.eslintrc.js b/ui/web/.eslintrc.js index 18ddac2e..d82d3c5f 100644 --- a/ui/web/.eslintrc.js +++ b/ui/web/.eslintrc.js @@ -1,3 +1,4 @@ +const { env } = require('node:process'); const { defineConfig } = require('eslint-define-config'); module.exports = defineConfig({ @@ -30,8 +31,8 @@ module.exports = defineConfig({ }, rules: { - 'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off', - 'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off', + 'no-console': env.NODE_ENV === 'production' ? 'warn' : 'off', + 'no-debugger': env.NODE_ENV === 'production' ? 'warn' : 'off', 'vue/require-v-for-key': 'off', 'vue/multi-word-component-names': 'off', 'sort-imports': [ diff --git a/ui/web/start.js b/ui/web/start.js index 523b5267..9b18156c 100644 --- a/ui/web/start.js +++ b/ui/web/start.js @@ -1,10 +1,11 @@ -const http = require('http'), - path = require('path'), +const http = require('node:http'), + path = require('node:path'), + { env } = require('node:process'), finalhandler = require('finalhandler'), serveStatic = require('serve-static'); -const isCFEnvironment = process.env.VCAP_APPLICATION !== undefined, - PORT = isCFEnvironment ? parseInt(process.env.PORT) : 3030, +const isCFEnvironment = env.VCAP_APPLICATION !== undefined, + PORT = isCFEnvironment ? parseInt(env.PORT) : 3030, uiPath = path.join(__dirname, './dist'); const serve = serveStatic(uiPath); -- 2.34.1