Skip to content

Commit

Permalink
Tidy up getOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
cookpete committed Apr 16, 2019
1 parent 2014fc2 commit 0168216
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
22 changes: 7 additions & 15 deletions src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const PACKAGE_FILE = 'package.json'
const PACKAGE_OPTIONS_KEY = 'auto-changelog'
const OPTIONS_DOTFILE = '.auto-changelog'

function getOptions (argv, pkg, dotOptions) {
async function getOptions (argv) {
const options = new Command()
.option('-o, --output [file]', `output file, default: ${DEFAULT_OPTIONS.output}`)
.option('-t, --template [template]', `specify template to use [compact, keepachangelog, json], default: ${DEFAULT_OPTIONS.template}`)
Expand All @@ -48,20 +48,14 @@ function getOptions (argv, pkg, dotOptions) {
.version(version)
.parse(argv)

if (!pkg) {
if (options.package) {
throw new Error('package.json could not be found')
}
return {
...DEFAULT_OPTIONS,
...dotOptions,
...options
}
}
const pkg = await readJson(PACKAGE_FILE)
const packageOptions = pkg ? pkg[PACKAGE_OPTIONS_KEY] : null
const dotOptions = await readJson(OPTIONS_DOTFILE)

return {
...DEFAULT_OPTIONS,
...dotOptions,
...pkg[PACKAGE_OPTIONS_KEY],
...packageOptions,
...options
}
}
Expand Down Expand Up @@ -100,9 +94,7 @@ async function getReleases (commits, remote, latestVersion, options) {
}

export default async function run (argv) {
const pkg = await readJson(PACKAGE_FILE)
const dotOptions = await readJson(OPTIONS_DOTFILE)
const options = getOptions(argv, pkg, dotOptions)
const options = await getOptions(argv)
const log = string => options.stdout ? null : updateLog(string)
log('Fetching remote…')
const remote = await fetchRemote(options.remote)
Expand Down
8 changes: 4 additions & 4 deletions test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import run, {
const getOptions = __get__('getOptions')

describe('getOptions', () => {
it('parses commit limit correctly', () => {
const options = getOptions(['', '', '--commit-limit', '10'])
it('parses commit limit correctly', async () => {
const options = await getOptions(['', '', '--commit-limit', '10'])
expect(options.commitLimit).to.equal(10)
})

it('parses false commit limit correctly', () => {
const options = getOptions(['', '', '--commit-limit', 'false'])
it('parses false commit limit correctly', async () => {
const options = await getOptions(['', '', '--commit-limit', 'false'])
expect(options.commitLimit).to.equal(false)
})
})
Expand Down

0 comments on commit 0168216

Please sign in to comment.