Skip to content

Commit

Permalink
feat(config): add config check and alert user if not ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBrisorgueil committed Jun 8, 2019
1 parent 4c798b0 commit 0aeddbc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 34 deletions.
1 change: 1 addition & 0 deletions config/defaults/production.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = _.merge(defaultConfig, {
title: 'WeAreOpenSource Node - Production Environment',
},
host: '0.0.0.0',
port: 4200,
db: {
uri: 'mongodb://localhost/WaosNode',
debug: false,
Expand Down
42 changes: 8 additions & 34 deletions config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,6 @@ const getGlobbedPaths = (globPatterns, excludes) => {
return output;
};

// /**
// * Validate NODE_ENV existence
// */
// const validateEnvironmentVariable = function () {
// const environmentFiles = glob.sync('./config/env/' + process.env.NODE_ENV + '.js');
// console.log();
// if (!environmentFiles.length) {
// if (process.env.NODE_ENV) {
// console.error(chalk.red('+ Error: No configuration file found for "' + process.env.NODE_ENV + '" environment using development instead'));
// } else {
// console.error(chalk.red('+ Error: NODE_ENV is not defined! Using default development environment'));
// }
// process.env.NODE_ENV = 'development';
// }
// // Reset console color
// console.log(chalk.white(''));
// };

/** Validate config.domain is set
*/
const validateDomainIsSet = (config) => {
Expand Down Expand Up @@ -106,39 +88,36 @@ const initSecureMode = (config) => {
const initGlobalConfigFiles = (config, assets) => {
// Appending files
config.files = {};

// Setting Globbed mongoose model files
config.files.mongooseModels = getGlobbedPaths(assets.mongooseModels);

// Setting Globbed sequelize model files
config.files.sequelizeModels = getGlobbedPaths(assets.sequelizeModels);

// Setting Globbed route files
config.files.routes = getGlobbedPaths(assets.routes);

// Setting Globbed config files
config.files.configs = getGlobbedPaths(assets.config);

// Setting Globbed socket files
// config.files.sockets = getGlobbedPaths(assets.sockets);

// Setting Globbed policies files
config.files.policies = getGlobbedPaths(assets.policies);
};

/**
* Initialize global configuration
*/
const initGlobalConfig = () => {
// Validate NODE_ENV existence
// // validateEnvironmentVariable();

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

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

// Get the config from process.env.WAOS_NODE_*
const environmentVars = _.mapKeys(
Expand All @@ -147,10 +126,8 @@ const initGlobalConfig = () => {
);
const environmentConfigVars = {};
_.forEach(environmentVars, (v, k) => objectPath.set(environmentConfigVars, k, v));

// Merge config files
const config = _.merge(defaultConfig, environmentConfigVars);

// read package.json for MEAN.JS project information
const pkg = require(path.resolve('./package.json'));
config.meanjs = pkg;
Expand All @@ -160,13 +137,10 @@ const initGlobalConfig = () => {

// Initialize global globbed files
initGlobalConfigFiles(config, assets);

// Init Secure SSL if can be used
initSecureMode(config);

// Print a warning if config.domain is not set
validateDomainIsSet(config);

// Expose configuration utilities
config.utils = {
getGlobbedPaths,
Expand Down

0 comments on commit 0aeddbc

Please sign in to comment.