fix(core): Improve handling of env.json and env.yml file checks

This commit is contained in:
2024-11-18 19:19:30 +01:00
parent fa00f33c13
commit bedefd3efe
3 changed files with 70 additions and 7 deletions

View File

@@ -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.'
}

View File

@@ -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;
}