Skip to content

Commit

Permalink
feat: multi improvement
Browse files Browse the repository at this point in the history
* make brickyard-cli more standalone, move the main boot logic to cli
* more clear config loading
* remove subcommand dep
* make sure the whole app use the same logger module
  • Loading branch information
e-cloud committed Sep 26, 2016
1 parent 3bf53d2 commit 3456172
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 56 deletions.
24 changes: 5 additions & 19 deletions base/brickyard-cli.js
Expand Up @@ -5,9 +5,6 @@
const Liftoff = require('liftoff')
const Command = require('commander').Command
const packageInfo = require('../package.json')
const butil = require('../lib/util')
const logger = require('../lib/logger')
const _ = require('lodash')

boot(require('minimist')(process.argv.slice(2)))

Expand All @@ -28,15 +25,15 @@ function boot(argv) {
v8flags: ['--harmony']
})

if (argv.backlog) {
logger.backlogFile(argv.backlog)
}

app.launch({
configPath: argv.config
}, (env) => {
let brickyard = !env.modulePath ? require('../') : require(env.modulePath)

if (argv.backlog) {
brickyard.logger.backlogFile(argv.backlog)
}

if (argv.verbose) {
brickyard.setLogLevel('debug')
} else {
Expand All @@ -45,18 +42,7 @@ function boot(argv) {

const rootCmd = initRootCmd(packageInfo)

brickyard.cli.load(rootCmd, env.configPath ? require(env.configPath).commands : null)
.spread((cmdName, options) => {
const cmdOptions = butil.assignWithValid({}, options, rootCmd.opts())
const targetCmd = brickyard.cli.commands[cmdName]

brickyard.load(env.configPath, targetCmd.config)

targetCmd.run(brickyard.hatchRuntime(cmdOptions))
})
.catch((e) => {
throw e
})
brickyard.cli.load(rootCmd, env)

rootCmd.parse(process.argv)
})
Expand Down
27 changes: 15 additions & 12 deletions lib/brickyard.js
Expand Up @@ -6,7 +6,6 @@
const EventEmitter = require('events').EventEmitter
const _ = require('lodash')

const butil = require('./util')
const logging = require('./logger')
const configLoader = require('./configLoader')
const programLoader = require('./programLoader')
Expand All @@ -19,9 +18,11 @@ class Brickyard extends EventEmitter {
constructor() {
super()

logging.setGlobalLogLevel('info')
this.config = {}

this.logger = logging

logger.trace('init brickyard instance')
logging.setGlobalLogLevel('info')
}

/**
Expand All @@ -30,8 +31,10 @@ class Brickyard extends EventEmitter {
* @param {String} configPath
* @param {Object} extraDefaultConfig
*/
load(configPath, extraDefaultConfig) {
this.config = configLoader.run(configPath, extraDefaultConfig)
load(extraDefaultConfig) {
this.defaultconfig = configLoader.loadDefaultConfig(extraDefaultConfig)

this.config = configLoader.mergeConfig(this.defaultconfig, this.userConfig)

this.programs = programLoader.getAllPrograms(this.config.programStore, this.config.allowNoPrograms)

Expand All @@ -52,23 +55,23 @@ class Brickyard extends EventEmitter {
* @param cmdOptions
* @returns Object
*/
hatchRuntime(cmdOptions) {
hatchRuntime() {
logger.trace('hatching the runtime object')

const mergePrograms = compoundPrograms(this.programs, cmdOptions.program)
const mergePrograms = compoundPrograms(this.programs, this.cmdOptions.program)

// 将所有 config 合并到 this.config, 有效值会按顺序 左->右 被覆盖
butil.mergeWithValid(this.config, mergePrograms.config, cmdOptions)
this.config = configLoader.mergeConfig(this.config, mergePrograms.config, this.cmdOptions)

// 动态构建输出目录名
if (!this.config.dest) {
this.config.dest = path.join('dist', constructOutputDir(this.config.destPrefix, this.config.program))
}

this.config.outputBase = path.resolve(process.cwd(), this.config.dest)
this.config.outputAssetsPath = path.join(this.config.outputBase, this.config.destPostfix)

const plugins = pluginLoader.getTargetPlugins(this.config.pluginStore, mergePrograms.plugins)

const plugins = _.pick(this.plugins, mergePrograms.plugins)

return {
config: this.config,
Expand All @@ -77,10 +80,10 @@ class Brickyard extends EventEmitter {
}
}

Brickyard.prototype.cli = require('./cli')

module.exports = new Brickyard()

Brickyard.prototype.cli = require('./cli')

// ==========================================================================

/**
Expand Down
34 changes: 30 additions & 4 deletions lib/cli.js
Expand Up @@ -5,11 +5,14 @@
'use strict'

const _ = require('lodash')
const packageLoader = require('./packageLoader')
const path = require('path')
const logger = require('log4js').getLogger('CLI')
const Promise = require('bluebird')

const brickyard = require('./brickyard')
const packageLoader = require('./packageLoader')
const butil = require('../lib/util')

const cli = {
commands: {},
load
Expand All @@ -24,10 +27,33 @@ module.exports = cli
* triggered, brickyard can do something to run the command.
*
* @param {Command} rootCmd
* @param {Array} userCommands
* @param {Object} env
* @returns {Promise}
*/
function load(rootCmd, userCommands) {
return new Promise(resolve => loadCommands(rootCmd, userCommands, resolve))
function load(rootCmd, env) {
brickyard.userConfig = loadUserConfig(env.configPath)

return new Promise(resolve => loadCommands(rootCmd, getUserSpecifiedCommands(brickyard.userConfig), resolve))
.spread((cmdName, options) => {
const targetCmd = brickyard.cli.commands[cmdName]

brickyard.cmdOptions = butil.assignWithValid({}, options, rootCmd.opts())

brickyard.load(targetCmd.config)

targetCmd.run(brickyard.hatchRuntime())
})
.catch((e) => {
throw e
})
}

function getUserSpecifiedCommands(userConfig) {
return userConfig ? userConfig.commands : null
}

function loadUserConfig(confPath) {
return confPath ? require(confPath) : null
}

function loadCommands(rootCmd, userCommands, resolve) {
Expand Down
38 changes: 18 additions & 20 deletions lib/configLoader.js
Expand Up @@ -7,36 +7,34 @@
const _ = require('lodash')
const path = require('path')
const logger = require('log4js').getLogger('ConfigLoader')
const butil = require('./util')

const frameworkDefaultConfigPath = path.resolve(__dirname, '../config/default.js')

module.exports = {
run: loadConfig
loadDefaultConfig,
mergeConfig
}

/**
* 获取默认配置文件与指定配置文件,并返回合并配置
* 合并配置
*
* @param {String} [configPath]
* @param {Object} [extraDefaultConfig]
* @param configs
* @returns {Object}
*/
function loadConfig(configPath, extraDefaultConfig) {
const configPathQueue = [configPath, extraDefaultConfig, frameworkDefaultConfigPath]
function mergeConfig(...configs) {
logger.trace('merge all config')

logger.debug('the config path queue is: ', configPathQueue)

return configPathQueue.reduceRight(function (configObject, pathOrConfig) {
if (_.isString(pathOrConfig)) {
const resolvedPath = path.resolve(process.cwd(), pathOrConfig)

_.assignIn(configObject, require(resolvedPath))
}

if (_.isPlainObject(pathOrConfig)) {
_.assignIn(configObject, pathOrConfig)
}
return butil.assignWithValid({}, ...configs)
}

return configObject
}, {})
/**
* load the default configuration
* @param defaultConfPath
* @param extraDefaultConfig
*/
function loadDefaultConfig(extraDefaultConfig, defaultConfPath = frameworkDefaultConfigPath) {
logger.trace('load the default configuration')
const defaultConf = require(defaultConfPath)
return _.assignIn({}, defaultConf, extraDefaultConfig)
}
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -25,7 +25,6 @@
"minimist": "^1.2.0"
},
"devDependencies": {
"brickyard-command-init": "*",
"chai": "^3.5.0",
"eslint": "^3.5.0",
"eslint-config-airbnb-base": "^7.0.0",
Expand Down Expand Up @@ -73,5 +72,8 @@
"post-rewrite": "npm install",
"post-merge": "npm install"
}
},
"engines": {
"node": ">=6.0"
}
}

0 comments on commit 3456172

Please sign in to comment.