Skip to content

Commit

Permalink
Merge pull request #576 from iCodeOkay/460-Strict-Mode
Browse files Browse the repository at this point in the history
FIX #460 - Strict mode warning refer to appropriate env variable
  • Loading branch information
markstos committed Nov 20, 2020
2 parents 2f192f7 + d4e8ae4 commit b4ba63c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
29 changes: 22 additions & 7 deletions lib/config.js
Expand Up @@ -15,7 +15,7 @@ var deferConfig = require('../defer').deferConfig,
// Static members
var DEFAULT_CLONE_DEPTH = 20,
NODE_CONFIG, CONFIG_DIR, RUNTIME_JSON_FILENAME, NODE_ENV, APP_INSTANCE,
HOST, HOSTNAME, ALLOW_CONFIG_MUTATIONS, CONFIG_SKIP_GITCRYPT,
HOST, HOSTNAME, ALLOW_CONFIG_MUTATIONS, CONFIG_SKIP_GITCRYPT, NODE_ENV_VAR_NAME,
NODE_CONFIG_PARSER,
env = {},
privateUtil = {},
Expand Down Expand Up @@ -513,11 +513,26 @@ util.loadFileConfigs = function(configDir) {
var t = this,
config = {};

// Initialize parameters from command line, environment, or default
NODE_ENV = util.initParam('NODE_ENV', 'development');
// Specify variables that can be used to define the environment
var node_env_var_names = ['NODE_CONFIG_ENV', 'NODE_ENV'];

// Override, NODE_ENV if NODE_CONFIG_ENV is specified.
NODE_ENV = util.initParam('NODE_CONFIG_ENV', NODE_ENV);
// Loop through the variables to try and set environment
for (node_env_var_name of node_env_var_names) {
NODE_ENV = util.initParam(node_env_var_name);
if (!!NODE_ENV) {
NODE_ENV_VAR_NAME = node_env_var_name;
break;
}
}

// If we haven't successfully set the environment using the variables, we'll default it
if (!NODE_ENV) {
NODE_ENV = 'development';
}

node_env_var_names.forEach(node_env_var_name => {
env[node_env_var_name] = NODE_ENV;
});

// Split files name, for loading multiple files.
NODE_ENV = NODE_ENV.split(',');
Expand Down Expand Up @@ -1409,11 +1424,11 @@ util.runStrictnessChecks = function (config) {
});
// development is special-cased because it's the default value
if (env && (env !== 'development') && !anyFilesMatchEnv) {
_warnOrThrow("NODE_ENV value of '"+env+"' did not match any deployment config file names.");
_warnOrThrow(NODE_ENV_VAR_NAME+" value of '"+env+"' did not match any deployment config file names.");
}
// Throw if NODE_ENV matches' default' or 'local'
if ((env === 'default') || (env === 'local')) {
_warnOrThrow("NODE_ENV value of '"+env+"' is ambiguous.");
_warnOrThrow(NODE_ENV_VAR_NAME+" value of '"+env+"' is ambiguous.");
}
});

Expand Down
26 changes: 24 additions & 2 deletions test/6-strict-mode.js
Expand Up @@ -54,6 +54,20 @@ vows.describe('Tests for strict mode').addBatch({
exceptionMessage :"FATAL: NODE_ENV value of 'local' is ambiguous. "
+"See https://github.com/lorenwest/node-config/wiki/Strict-Mode",
}),

"Specifying reserved word for NODE_CONFIG_ENV throws reserved word exception with appropriate wording": _expectException({
NODE_CONFIG_ENV : 'local',
APP_INSTANCE : 'valid-instance',
exceptionMessage :"FATAL: NODE_CONFIG_ENV value of 'local' is ambiguous. "
+"See https://github.com/lorenwest/node-config/wiki/Strict-Mode",
}),

"Specifying NODE_CONFIG_ENV=production,cloud with no cloud file throws an exception with appropriate wording": _expectException({
NODE_CONFIG_ENV : 'cloud',
APP_INSTANCE : 'valid-instance',
exceptionMessage :"FATAL: NODE_CONFIG_ENV value of 'cloud' did not match any deployment config file names. "
+"See https://github.com/lorenwest/node-config/wiki/Strict-Mode",
}),
})
.export(module);

Expand All @@ -67,9 +81,17 @@ function _expectException (opts) {
process.env.NODE_CONFIG_DIR = __dirname + '/6-config';
process.env.NODE_CONFIG_STRICT_MODE = 1;
process.env.NODE_APP_INSTANCE = opts.APP_INSTANCE;
process.env.NODE_ENV = opts.NODE_ENV;

if (!!opts.NODE_ENV) {
process.env.NODE_ENV = opts.NODE_ENV;
}

if (!!opts.NODE_CONFIG_ENV) {
process.env.NODE_CONFIG_ENV = opts.NODE_CONFIG_ENV;
}

delete process.env.NODE_CONFIG;
try {
try {
var config = requireUncached(__dirname + '/../lib/config');
}
catch (e) {
Expand Down

0 comments on commit b4ba63c

Please sign in to comment.