Skip to content

Commit

Permalink
feat: support command-specified default config
Browse files Browse the repository at this point in the history
split default config mangement per command for self management.
  • Loading branch information
e-cloud committed Sep 19, 2016
1 parent 8c5b503 commit 98f0bf1
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 43 deletions.
2 changes: 1 addition & 1 deletion base/brickyard-cli.js
Expand Up @@ -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))
})
Expand Down
23 changes: 1 addition & 22 deletions config/default.js
Expand Up @@ -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
}
12 changes: 3 additions & 9 deletions lib/brickyard.js
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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'
}
19 changes: 11 additions & 8 deletions lib/configLoader.js
Expand Up @@ -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')
Expand All @@ -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
Expand Down
4 changes: 1 addition & 3 deletions lib/pluginLoader.js
Expand Up @@ -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]
}

Expand Down

0 comments on commit 98f0bf1

Please sign in to comment.