Skip to content

Commit

Permalink
Refactor run logic
Browse files Browse the repository at this point in the history
  • Loading branch information
cookpete committed Nov 7, 2017
1 parent b2dc7bb commit 3892183
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,12 @@
import 'babel-polyfill'
import run from './run'

run().catch(e => console.error(e))
run(process.argv)
.then(message => {
console.log(message)
process.exit(0)
})
.catch(error => {
console.error(error)
process.exit(1)
})
11 changes: 5 additions & 6 deletions src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ const DEFAULT_REMOTE = 'origin'
const NPM_VERSION_TAG_PREFIX = 'v'
const PACKAGE_OPTIONS_KEY = 'auto-changelog'

function getOptions (pkg) {
function getOptions (argv, pkg) {
const options = commander
.option('-o, --output [file]', `output file, default: ${DEFAULT_OUTPUT}`, DEFAULT_OUTPUT)
.option('-t, --template [template]', `specify template to use [compact, keepachangelog, json], default: ${DEFAULT_TEMPLATE}`, DEFAULT_TEMPLATE)
.option('-r, --remote [remote]', `specify git remote to use for links, default: ${DEFAULT_REMOTE}`, DEFAULT_REMOTE)
.option('-p, --package', 'use version from package.json as latest release')
.option('-u, --unreleased', 'include section for unreleased changes')
.version(version)
.parse(process.argv)
.parse(argv)

if (!pkg) {
if (options.package) {
Expand All @@ -35,15 +35,14 @@ function getOptions (pkg) {
}
}

export default async function run () {
export default async function run (argv) {
const pkg = await pathExists('package.json') && await readJson('package.json')
const options = getOptions(pkg)
const options = getOptions(argv, pkg)
const origin = await fetchOrigin(options.remote)
const commits = await fetchCommits(origin)
const packageVersion = options.package ? NPM_VERSION_TAG_PREFIX + pkg.version : null
const releases = parseReleases(commits, origin, packageVersion, options.unreleased)
const log = await compileTemplate(options.template, { releases })
await writeFile(options.output, log)
console.log(`${Buffer.byteLength(log, 'utf8')} bytes written to ${options.output}`)
process.exit(0)
return `${Buffer.byteLength(log, 'utf8')} bytes written to ${options.output}`
}

0 comments on commit 3892183

Please sign in to comment.