diff --git a/lib/load-config.js b/lib/load-config.js new file mode 100644 index 0000000..19674f3 --- /dev/null +++ b/lib/load-config.js @@ -0,0 +1,8 @@ +var loadConfig = require('load-config-file'); + +// loadConfig.register('.json', JSON.parse); +loadConfig.register(['.json', '.js'], function(ctx, opt, file){ + return require(file); +}); + +module.exports = loadConfig; diff --git a/lib/plugin-base.js b/lib/plugin-base.js index d4d4af0..595b369 100644 --- a/lib/plugin-base.js +++ b/lib/plugin-base.js @@ -12,6 +12,7 @@ var assert = require('assert'); var _ = require('lodash'); var util = require('./utils'); var g = require('./globalize'); +var loadConfig = require('./load-config'); module.exports = PluginBase; @@ -92,17 +93,23 @@ PluginBase.prototype.loadNamed = function(rootDir, env, name) { * @returns {Array.} Array of absolute file paths. */ PluginBase.prototype.findConfigFiles = function(rootDir, env, name, exts) { - var master = ifExists(name + '.json'); - if (!master && (ifExistsWithAnyExt(name + '.local') || - ifExistsWithAnyExt(name + '.' + env))) { + var master; + var vExts = Object.keys(loadConfig.prototype.configurators); + for (var ext of vExts) { + master = ifExists(name + ext); + if (master) break; + } + + if (!master && (ifExistsWithAnyExt(name + '.local', vExts) || + ifExistsWithAnyExt(name + '.' + env, vExts))) { g.warn('WARNING: Main config file "%s{{.json}}" is missing', name); } if (!master) return []; var candidates = [ master, - ifExistsWithAnyExt(name + '.local'), - ifExistsWithAnyExt(name + '.' + env), + ifExistsWithAnyExt(name + '.local', vExts), + ifExistsWithAnyExt(name + '.' + env, vExts), ]; return candidates.filter(function(c) { @@ -114,11 +121,11 @@ PluginBase.prototype.findConfigFiles = function(rootDir, env, name, exts) { return util.fileExistsSync(filePath) ? filePath : undefined; } - function ifExistsWithAnyExt(fileName) { - var extensions = exts || ['js', 'json']; + function ifExistsWithAnyExt(fileName, extensions) { + extensions = extensions || ['.js', '.json']; var file; for (var i = 0, n = extensions.length; i < n; i++) { - file = ifExists(fileName + '.' + extensions[i]); + file = ifExists(fileName + extensions[i]); if (file) { return file; } @@ -134,7 +141,7 @@ PluginBase.prototype.findConfigFiles = function(rootDir, env, name, exts) { */ PluginBase.prototype._loadConfigFiles = function(files) { return files.map(function(f) { - var config = require(f); + var config = loadConfig(f); config = _.cloneDeep(config); Object.defineProperty(config, '_filename', { enumerable: false, diff --git a/lib/plugins/model.js b/lib/plugins/model.js index 27f81f1..cb6dca1 100644 --- a/lib/plugins/model.js +++ b/lib/plugins/model.js @@ -13,7 +13,7 @@ var debug = require('debug')('loopback:boot:model'); var _ = require('lodash'); var toposort = require('toposort'); var utils = require('../utils'); -var loadConfig = require('load-config-file'); +var loadConfig = require('../load-config'); var tryReadDir = utils.tryReadDir; var assertIsValidConfig = utils.assertIsValidConfig; @@ -21,8 +21,6 @@ var tryResolveAppPath = utils.tryResolveAppPath; var fixFileExtension = utils.fixFileExtension; var g = require('../globalize'); -loadConfig.register('.json', JSON.parse); - module.exports = function(options) { return new Model(options); };