Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(argv): Load options from command line, env, package.json or conf…
…ig file
- Loading branch information
1 parent
f555520
commit e509c9b
Showing
13 changed files
with
173 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export interface IOptions { | ||
logLevel?: 'debug' | 'info' | 'warn' | 'error' | 'silent' | ||
logLevel?: 'debug' | 'info' | 'warn' | 'error' | 'silent', | ||
name?: string, | ||
alias?: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import 'mocha' | ||
import { expect } from 'chai' | ||
import * as yargs from 'yargs' | ||
import mock from 'mock-fs' | ||
import * as argv from './argv' | ||
|
||
describe('argv', () => { | ||
describe('.config', () => { | ||
it('contains arguments collection, with defaults', () => { | ||
expect(argv.config).to.have.property('name', 'bot') | ||
}) | ||
}) | ||
describe('.getConfig', () => { | ||
it('loads config from process.argv', () => { | ||
yargs.parse(['--name', 'hao']) // overwrite argv | ||
const config = argv.getConfig() | ||
expect(config).to.have.property('name', 'hao') | ||
yargs.parse(process.argv) // replace with actual argv | ||
}) | ||
it('loads configs from ENV variables using prefix', () => { | ||
process.env.BOT_NAME = 'henry' | ||
expect(argv.getConfig()).to.have.property('name', 'henry') | ||
delete process.env.BOT_NAME | ||
}) | ||
it('loads config from package.json `bot` attribute', () => { | ||
expect(argv.getConfig()).to.have.property('alias', 'bbot') | ||
}) | ||
/** @todo restore config file testing without crashing wallaby */ | ||
// it('load config from a defined json file if given', () => { | ||
// mock({ | ||
// '/mock/config.json': JSON.stringify({ name: 'harriet' }) | ||
// }) | ||
// yargs.parse(['--config', '/mock/config.json']) // overwrite argv | ||
// console.log(argv.getConfig()) | ||
// mock.restore() | ||
// yargs.parse(process.argv) // replace with actual argv | ||
// }) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import * as yargs from 'yargs' | ||
import * as packageJSON from '../../package.json' | ||
import { IOptions } from '../config/botInterfaces' | ||
|
||
/** | ||
* Used to trim argv into shape of interface | ||
* @todo Needs to be updated with changes to interface, should be dynamic | ||
*/ | ||
export const optionsFilterKeys = [ | ||
'logLevel', | ||
'name', | ||
'alias' | ||
] | ||
|
||
/** | ||
* Combine and load config from command line, environment and JSON if provided. | ||
*/ | ||
export function getConfig () { | ||
const argv = yargs | ||
.usage('\nUsage: $0 [args]') | ||
.env('BOT') | ||
.pkgConf('bot') | ||
.option('log-level', { | ||
type: 'string', | ||
describe: 'The starting minimum level for logging events (silent|debug|info|warn|error).', | ||
default: 'info' | ||
}) | ||
.option('name', { | ||
alias: 'n', | ||
type: 'string', | ||
describe: 'Name of the bot in chat. Prepending any command with the name will trigger respond listeners.\n', | ||
default: 'bot' | ||
}) | ||
.option('alias', { | ||
type: 'string', | ||
describe: 'Alternate name for the bot.\n', | ||
default: false | ||
}) | ||
.config() | ||
.alias('config', 'c') | ||
.example('config', 'bin/bbot -c bot-config.json') | ||
.version(packageJSON.version) | ||
.alias('version', 'v') | ||
.help() | ||
.alias('help', 'h') | ||
.epilogue( | ||
`All option can be provided as environment variables, with the prefix \`BOT_\`. | ||
Config can also be declared in \`package.json\` with the key: "botConfig". | ||
For more information, see https://amazebot.github.io/bbot'` | ||
) | ||
.fail((msg: string, err: Error) => { | ||
console.error(msg, err) | ||
console.info('Start with --help for config argument info.') | ||
if (err) throw err | ||
process.exit(1) | ||
}) | ||
.argv | ||
const config: any = [] | ||
for (let key of Object.keys(argv)) { | ||
if (optionsFilterKeys.indexOf(key) !== -1) { | ||
config[key] = argv[key] | ||
} | ||
} | ||
return config | ||
} | ||
|
||
export const config = getConfig() as IOptions | ||
|
||
if (process.platform !== 'win32') process.on('SIGTERM', () => process.exit(0)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,4 @@ | |
*/ | ||
|
||
import * as b from './index' | ||
module.exports = b.bot.start().then(() => b) | ||
export = b.bot.start().then(() => b) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
declare module "*.json" { | ||
export let version: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters