diff --git a/base/brickyard-cli.js b/base/brickyard-cli.js index b1e3871..e864565 100755 --- a/base/brickyard-cli.js +++ b/base/brickyard-cli.js @@ -42,7 +42,7 @@ function boot(argv) { const cmdOptions = butil.assignWithValid({}, options, rootCmd.opts()) const targetCmd = brickyard.cli.commands[cmdName] - brickyard.load(env.configPath) + brickyard.load(env.configPath, targetCmd.config) targetCmd.run(brickyard.hatchRuntime(cmdOptions)) }) diff --git a/config/default.js b/config/default.js index 600d9ac..09bab9b 100644 --- a/config/default.js +++ b/config/default.js @@ -6,26 +6,5 @@ module.exports = { // path related dest: null, - program: null, - - // server related - port: 8080, - host: 'localhost', - bsProxy: { - port: 3000, - host: 'localhost' - }, - apiProxy: null, - watch: false, - livereload: false, - https: false, - - // others - hashbit: 7, - lint: false, - offline: false, - registry: null, - showConfig: false, - debuggable: false, - compress: true + program: null } diff --git a/lib/brickyard.js b/lib/brickyard.js index d5eb151..f942726 100644 --- a/lib/brickyard.js +++ b/lib/brickyard.js @@ -29,8 +29,8 @@ class Brickyard extends EventEmitter { * * @param {String} configPath */ - load(configPath) { - this.config = configLoader.run(configPath) + load(configPath, extraDefaultConfig) { + this.config = configLoader.run(configPath, extraDefaultConfig) this.programs = programLoader.getAllPrograms(this.config.programStore) @@ -63,7 +63,7 @@ class Brickyard extends EventEmitter { this.config.dest = path.join('dist', constructOutputDir(this.config.destPrefix, this.config.program)) } - transformConfig(this.config) + this.config.outputBase = path.resolve(process.cwd(), this.config.dest) const plugins = pluginLoader.getTargetPlugins(this.config.pluginStore, mergePrograms.plugins) @@ -124,9 +124,3 @@ function compoundPrograms(programs, target) { function constructOutputDir(prefix, programs) { return [prefix].concat(programs).join('-') } - -function transformConfig(config) { - // 获取输出目录根路径 - config.outputBase = path.resolve(process.cwd(), config.dest) - config.protocol = config.https ? 'https' : 'http' -} diff --git a/lib/configLoader.js b/lib/configLoader.js index 4cccf26..3aed41c 100644 --- a/lib/configLoader.js +++ b/lib/configLoader.js @@ -6,7 +6,6 @@ const _ = require('lodash') const path = require('path') -const fs = require('fs') const logger = require('log4js').getLogger('ConfigLoader') const frameworkDefaultConfigPath = path.resolve(__dirname, '../config/default.js') @@ -19,19 +18,23 @@ module.exports = Object.seal({ * 获取默认配置文件与指定配置文件,并返回合并配置 * * @param {String} [configPath] + * @param {Object} [extraDefaultConfig] * @returns {Object} */ -function loadConfig(configPath) { - const configPathQueue = [configPath, frameworkDefaultConfigPath] +function loadConfig(configPath, extraDefaultConfig) { + const configPathQueue = [configPath, extraDefaultConfig, frameworkDefaultConfigPath] logger.debug('the config path queue is: ', configPathQueue) - return configPathQueue.reduceRight(function (configObject, _path) { - if (_path) { - const resolvedPath = path.resolve(process.cwd(), _path) + return configPathQueue.reduceRight(function (configObject, pathOrConfig) { + if (_.isString(pathOrConfig)) { + const resolvedPath = path.resolve(process.cwd(), pathOrConfig) - let targetConfig = require(resolvedPath) - _.assignIn(configObject, targetConfig) + _.assignIn(configObject, require(resolvedPath)) + } + + if (_.isPlainObject(pathOrConfig)) { + _.assignIn(configObject, pathOrConfig) } return configObject diff --git a/lib/pluginLoader.js b/lib/pluginLoader.js index 248bbc9..d887dc5 100644 --- a/lib/pluginLoader.js +++ b/lib/pluginLoader.js @@ -70,9 +70,7 @@ function constructPluginConfig(rawPlugin) { logger.trace(`extract the valid main file path of plugin - ${plugin.name}`) // `main` may has many entries, just take the first - let jsEntries = _.filter(rawPlugin.main, function (entry) { - return entry.match(/\.js$/) - }) + let jsEntries = _.filter(rawPlugin.main, entry => entry.match(/\.js$/)) rawPlugin.main = jsEntries[0] }