Skip to content

Commit

Permalink
fix: let cliFlags take precedence over nerfDarted rcFlags
Browse files Browse the repository at this point in the history
This also contains a refactor that properly introduces
defaults and nerfDartsFlags to remove edge case handling,
making this automatic and future proof.

BREAKING CHANGE: Before the nerfDarted admin and token took precedence over everything.
  • Loading branch information
boennemann committed Jun 5, 2016
1 parent 2e8d27e commit ffc1df2
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var nopt = require('nopt')

var rc = require('@greenkeeper/rc')

// Available flags and their type definitions
var types = {
api: String,
help: Boolean,
Expand All @@ -26,8 +27,18 @@ var types = {
]
}

// Flag objects from different sources
// 1. defaults flags
// 2. rcfile flags
// 3. nerfDarted rcfile flags (i.e. scoped by api endpoint)
// 4. cli flags

var defaults = {
api: 'https://api.greenkeeper.io/',
postpublish: true
}

var rcFlags = rc.get()
nopt.clean(rcFlags, types)

var cliFlags = nopt(types, {
h: '--help',
Expand All @@ -42,15 +53,20 @@ var cliFlags = nopt(types, {
quiet: ['--loglevel', 'warn']
})

var flags = module.exports = _.assign({}, rcFlags, cliFlags)
var api = url.parse(cliFlags.api || rcFlags.api || defaults.api).format()
var prefix = nerfDart(api)

var nerfDartFlags = _(rcFlags)
.pickBy(function (flag, flagName) {
return flagName.slice(0, prefix.length) === prefix
})
.mapKeys(function (flag, flagName) {
return flagName.replace(prefix, '')
})
.value()

flags.api = url.parse(flags.api || 'https://api.greenkeeper.io/').format()
flags.token = rc.get()[nerfDart(flags.api) + 'token'] || flags.token
var admin = rc.get()[nerfDart(flags.api) + 'admin']
flags.admin = admin === false || admin === true
? admin : flags.admin
var flags = module.exports = _.assign({}, defaults, rcFlags, nerfDartFlags, cliFlags)
nopt.clean(flags, types)

flags.postpublish = flags.hasOwnProperty('postpublish')
? flags.postpublish
: true
flags.api = api
flags._rc = rc

0 comments on commit ffc1df2

Please sign in to comment.