Skip to content

Commit

Permalink
feat(config): Add helper to reset all config
Browse files Browse the repository at this point in the history
  • Loading branch information
timkinnane committed Aug 12, 2018
1 parent 450a3e9 commit 82befde
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 13 deletions.
55 changes: 53 additions & 2 deletions src/lib/config.spec.ts
Expand Up @@ -29,8 +29,56 @@ describe('[config]', () => {
settings.reloadConfig()
expect(typeof settings.config.isFoo).to.equal('boolean')
})
it('retains manually assigned configs as default', () => {
const settings = new config.Settings()
settings.set('name', 'foo')
settings.reloadConfig()
expect(settings.config.name).to.equal('foo')
})
it('retains added options and their config', () => {
const settings = new config.Settings()
settings.options['is-foo'] = {
type: 'boolean',
description: 'testing a new option',
default: false
}
settings.config = settings.loadConfig()
settings.set('is-foo', true)
settings.set('name', 'foo')
settings.reloadConfig()
expect(settings.config.isFoo).to.equal(true)
expect(settings.config.name).to.equal('foo')
})
})
describe('.resetConfig', () => {
it('overwrites assigned configs with option defaults', () => {
const settings = new config.Settings()
settings.options['is-foo'] = {
type: 'boolean',
description: 'testing a new option',
default: false
}
settings.config = settings.loadConfig()
settings.set('is-foo', true)
settings.resetConfig()
expect(settings.config.isFoo).to.equal(false)
})
it('returns assigned config to its original default', () => {
const settings = new config.Settings()
settings.set('name', 'foo')
settings.resetConfig()
expect(settings.config.name).to.equal('bot')
})
it('nothing inherited after reload', () => {
const settings = new config.Settings()
settings.set('name', 'foo')
settings.reloadConfig()
settings.resetConfig()
expect(settings.config.name).to.equal('bot')
})
})
describe('.extend', () => {
beforeEach(() => config.settings.resetConfig())
it('allows defining new options after load', () => {
process.env.BOT_FOO = 'bar'
const settings = new config.Settings()
Expand All @@ -40,6 +88,7 @@ describe('[config]', () => {
})
})
describe('.name', () => {
beforeEach(() => config.settings.resetConfig())
it('provides shortcut to name from config', () => {
config.settings.name = 'foo1'
expect(config.settings.config.name).to.equal('foo1')
Expand All @@ -50,6 +99,7 @@ describe('[config]', () => {
})
})
describe('.alias', () => {
beforeEach(() => config.settings.resetConfig())
it('provides shortcut to alias from config', () => {
config.settings.alias = 'foo1'
expect(config.settings.config.alias).to.equal('foo1')
Expand All @@ -60,6 +110,7 @@ describe('[config]', () => {
})
})
describe('.set', () => {
beforeEach(() => config.settings.resetConfig())
it('assigns the given setting and all alias settings', () => {
config.settings.extend({
'foo-bar': {
Expand All @@ -78,13 +129,13 @@ describe('[config]', () => {
})
})
describe('.config', () => {
beforeEach(() => config.settings.reloadConfig())
beforeEach(() => config.settings.resetConfig())
it('contains arguments collection, with defaults', () => {
expect(config.settings).to.have.property('name', 'bot')
})
})
describe('.getConfig', () => {
beforeEach(() => config.settings.reloadConfig())
beforeEach(() => config.settings.resetConfig())
it('loads config from process.config', () => {
yargs.parse(['--name', 'hao']) // overwrite config
expect(config.getConfig()).to.have.property('name', 'hao')
Expand Down
33 changes: 22 additions & 11 deletions src/lib/config.ts
Expand Up @@ -80,18 +80,24 @@ export class Settings {
}

/** Access all settings from argv, env, package.json and custom config file */
config: yargs.Arguments = this.loadConfig()
config: yargs.Arguments = this.loadConfig(true)

/**
* Combine and load config from command line, environment and JSON if provided.
* The returned argv object will copy any options given using param alias into
* the main attribute, or use defaults if none assigned. The option values are
* then assigned to the config object (some are nullable).
*/
loadConfig () {
loadConfig (reset = false) {
for (let key in this.options) {
this.options[key].global = false
yargs.option(key, this.options[key])
const opt = Object.assign({}, this.options[key])
if (this.config) {
if (typeof opt.global === 'undefined') opt.global = false
if (!reset && typeof this.config[key] !== 'undefined') {
opt.default = this.config[key]
}
}
yargs.option(key, opt)
}
const config = yargs
.usage('\nUsage: $0 [args]')
Expand All @@ -113,29 +119,34 @@ export class Settings {
return config
}

/** Force reloading config after options update */
/** Allow reloading config after options update */
reloadConfig () {
this.config = this.loadConfig()
this.config = this.loadConfig(false)
}

/** Reload config without taking on existing */
resetConfig () {
this.config = this.loadConfig(true)
}

/** Validate name, stripping special characters */
safeName (name: string) { return name.replace(/[^a-z0-9_-]/ig, '') }

/** Shortcut to loaded bot name config */
get name () { return this.config.name }
get name () { return this.get('name') }

/** Shortcut to setting name with validation */
set name (name: string) { this.config.name = this.safeName(name) }
set name (name: string) { this.set('name', this.safeName(name)) }

/** Shortcut to loaded bot alias config */
get alias () { return this.config.alias }
get alias () { return this.get('alias') }

/** Shortcut to setting alias with validation */
set alias (name: string) { this.config.alias = this.safeName(name) }
set alias (name: string) { this.set('alias', this.safeName(name)) }

/** Generic config getter */
get (key: string) {
return this.config[key]
return (this.config) ? this.config[key] : undefined
}

/** Generic config setter (@todo this is kinda whack) */
Expand Down

0 comments on commit 82befde

Please sign in to comment.