From bedefd3efef65b82ec150bb9b47d314e804d2c45 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Mon, 18 Nov 2024 19:19:30 +0100 Subject: [PATCH] fix(core): Improve handling of env.json and env.yml file checks --- changelog.md | 49 ++++++++++++++++++++++++++++++++++++++++ ts/00_commitinfo_data.ts | 6 ++--- ts/qenv.classes.qenv.ts | 22 ++++++++++++++---- 3 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 changelog.md diff --git a/changelog.md b/changelog.md new file mode 100644 index 0000000..e51bee2 --- /dev/null +++ b/changelog.md @@ -0,0 +1,49 @@ +# Changelog + +## 2024-11-18 - 6.0.6 - fix(core) +Improve handling of env.json and env.yml file checks + +- Check for existence of both env.json and env.yml files and prioritize env.json. +- Consolidate getFromEnvJsonFile and getFromEnvYamlFile methods into getFromEnvYamlOrJsonFile. + +## 2024-05-29 to 2024-02-09 - 6.0.5 - update +Updates related to configuration files and data handling. + +- Updated description +- Updated tsconfig +- Updated npmextra.json for githost + +## 2023-08-09 - 6.0.0 to 6.0.4 - core +Various fixes within the core functionality. + +- Fixes and improvements across multiple minor versions + +## 2023-08-09 - 5.0.5 - core +Breaking change that impacts core functionality. + +- Significant updates leading to breaking changes + +## 2023-07-11 to 2022-07-28 - 5.0.2 - organization +Transition to a new organization scheme. + +- Switched to new organizational scheme + +## 2022-07-28 - 4.0.11 - core +Breaking change introducing ESM modules. + +- Switch to ECMAScript modules + +## 2019-01-15 - 3.1.1 - environment +Breaking change in environment handling. + +- Treat environment variables as immutable + +## 2019-01-14 - 3.0.7 - docker +New feature for Docker secret management. + +- Allow Docker secret.json to be named flexibly + +## 2018-08-13 - 1.1.7 - scope +Scope update for package management. + +- Change scope to @pushrocks/ diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index a722239..7680486 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -1,8 +1,8 @@ /** - * autocreated commitinfo by @pushrocks/commitinfo + * autocreated commitinfo by @push.rocks/commitinfo */ export const commitinfo = { name: '@push.rocks/qenv', - version: '6.0.5', - description: 'easy promised environments' + version: '6.0.6', + description: 'A module for easily handling environment variables in Node.js projects with support for .yml and .json configuration.' } diff --git a/ts/qenv.classes.qenv.ts b/ts/qenv.classes.qenv.ts index 55943cd..9ada42f 100644 --- a/ts/qenv.classes.qenv.ts +++ b/ts/qenv.classes.qenv.ts @@ -33,10 +33,24 @@ export class Qenv { 'qenv.yml' ); if (envFileBasePathArg) { - this.envFilePathAbsolute = plugins.path.join( + const envFileJsonPath = this.envFilePathAbsolute = plugins.path.join( plugins.path.resolve(envFileBasePathArg), 'env.json' ); + const envFileYamlPath = this.envFilePathAbsolute = plugins.path.join( + plugins.path.resolve(envFileBasePathArg), + 'env.yml' + ); + const envFileJsonExists = plugins.smartfile.fs.fileExistsSync(envFileJsonPath); + const envFileYamlExists = plugins.smartfile.fs.fileExistsSync(envFileYamlPath); + + if (envFileJsonExists && envFileYamlExists) { + this.logger.log('warn', 'Both env.json and env.yml files exist! Using env.json'); + } else if (envFileJsonExists) { + this.envFilePathAbsolute = envFileJsonPath; + } else if (envFileYamlExists) { + this.envFilePathAbsolute = envFileYamlPath; + } } } @@ -126,7 +140,7 @@ export class Qenv { return ( this.getFromEnvironmentVariable(envVarRefArg) || - this.getFromEnvJsonFile(envVarRefArg) || + this.getFromEnvYamlOrJsonFile(envVarRefArg) || this.getFromDockerSecret(envVarRefArg) || this.getFromDockerSecretJson(envVarRefArg) ); @@ -135,7 +149,7 @@ export class Qenv { private tryGetEnvVarSync(envVarName: string): string | undefined { return ( this.getFromEnvironmentVariable(envVarName) || - this.getFromEnvJsonFile(envVarName) || + this.getFromEnvYamlOrJsonFile(envVarName) || this.getFromDockerSecret(envVarName) || this.getFromDockerSecretJson(envVarName) ); @@ -145,7 +159,7 @@ export class Qenv { return process.env[envVarName]; } - private getFromEnvJsonFile(envVarName: string): string | undefined { + private getFromEnvYamlOrJsonFile(envVarName: string): string | undefined { if (!plugins.smartfile.fs.fileExistsSync(this.envFilePathAbsolute)) { return undefined; }