Skip to content

Commit 0aeddbc

Browse files
feat(config): add config check and alert user if not ✨
1 parent 4c798b0 commit 0aeddbc

File tree

2 files changed

+9
-34
lines changed

2 files changed

+9
-34
lines changed

config/defaults/production.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module.exports = _.merge(defaultConfig, {
66
title: 'WeAreOpenSource Node - Production Environment',
77
},
88
host: '0.0.0.0',
9+
port: 4200,
910
db: {
1011
uri: 'mongodb://localhost/WaosNode',
1112
debug: false,

config/index.js

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,6 @@ const getGlobbedPaths = (globPatterns, excludes) => {
4949
return output;
5050
};
5151

52-
// /**
53-
// * Validate NODE_ENV existence
54-
// */
55-
// const validateEnvironmentVariable = function () {
56-
// const environmentFiles = glob.sync('./config/env/' + process.env.NODE_ENV + '.js');
57-
// console.log();
58-
// if (!environmentFiles.length) {
59-
// if (process.env.NODE_ENV) {
60-
// console.error(chalk.red('+ Error: No configuration file found for "' + process.env.NODE_ENV + '" environment using development instead'));
61-
// } else {
62-
// console.error(chalk.red('+ Error: NODE_ENV is not defined! Using default development environment'));
63-
// }
64-
// process.env.NODE_ENV = 'development';
65-
// }
66-
// // Reset console color
67-
// console.log(chalk.white(''));
68-
// };
69-
7052
/** Validate config.domain is set
7153
*/
7254
const validateDomainIsSet = (config) => {
@@ -106,39 +88,36 @@ const initSecureMode = (config) => {
10688
const initGlobalConfigFiles = (config, assets) => {
10789
// Appending files
10890
config.files = {};
109-
11091
// Setting Globbed mongoose model files
11192
config.files.mongooseModels = getGlobbedPaths(assets.mongooseModels);
112-
11393
// Setting Globbed sequelize model files
11494
config.files.sequelizeModels = getGlobbedPaths(assets.sequelizeModels);
115-
11695
// Setting Globbed route files
11796
config.files.routes = getGlobbedPaths(assets.routes);
118-
11997
// Setting Globbed config files
12098
config.files.configs = getGlobbedPaths(assets.config);
121-
12299
// Setting Globbed socket files
123100
// config.files.sockets = getGlobbedPaths(assets.sockets);
124-
125101
// Setting Globbed policies files
126102
config.files.policies = getGlobbedPaths(assets.policies);
127103
};
128104

129105
/**
130106
* Initialize global configuration
131107
*/
132-
const initGlobalConfig = () => {
133-
// Validate NODE_ENV existence
134-
// // validateEnvironmentVariable();
135108

109+
const initGlobalConfig = () => {
136110
// Get the default assets
137111
const assets = require(path.join(process.cwd(), './config/assets'));
138112

139113
// Get the current config
140-
const currentEnv = process.env.NODE_ENV || 'development';
141-
const defaultConfig = require(path.join(process.cwd(), './config', 'defaults', currentEnv)) || {};
114+
const _path = path.join(process.cwd(), './config', 'defaults', process.env.NODE_ENV || 'development');
115+
let defaultConfig;
116+
if (fs.existsSync(`${_path}.js`)) defaultConfig = require(_path);
117+
else {
118+
console.error(chalk.red(`+ Error: No configuration file found for "${process.env.NODE_ENV}" environment using development instead`));
119+
defaultConfig = require(path.join(process.cwd(), './config', 'defaults', 'development'));
120+
}
142121

143122
// Get the config from process.env.WAOS_NODE_*
144123
const environmentVars = _.mapKeys(
@@ -147,10 +126,8 @@ const initGlobalConfig = () => {
147126
);
148127
const environmentConfigVars = {};
149128
_.forEach(environmentVars, (v, k) => objectPath.set(environmentConfigVars, k, v));
150-
151129
// Merge config files
152130
const config = _.merge(defaultConfig, environmentConfigVars);
153-
154131
// read package.json for MEAN.JS project information
155132
const pkg = require(path.resolve('./package.json'));
156133
config.meanjs = pkg;
@@ -160,13 +137,10 @@ const initGlobalConfig = () => {
160137

161138
// Initialize global globbed files
162139
initGlobalConfigFiles(config, assets);
163-
164140
// Init Secure SSL if can be used
165141
initSecureMode(config);
166-
167142
// Print a warning if config.domain is not set
168143
validateDomainIsSet(config);
169-
170144
// Expose configuration utilities
171145
config.utils = {
172146
getGlobbedPaths,

0 commit comments

Comments
 (0)