import commander from 'commander'
import { readJson, writeFile, pathExists } from 'fs-extra'

import { version } from '../package.json'
import { fetchOrigin } from './origin'
import { fetchCommits } from './commits'
import { parseReleases } from './releases'
import { compileTemplate } from './template'

const DEFAULT_OUTPUT = 'CHANGELOG.md'
const DEFAULT_TEMPLATE = 'compact'
const DEFAULT_REMOTE = 'origin'
const NPM_VERSION_TAG_PREFIX = 'v'

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)

export default async function run () {
  const pkg = await pathExists('package.json') && await readJson('package.json')
  let options = { ...commander }
  if (pkg) {
    options = { ...options, ...pkg['auto-changelog'] }
  } else if (commander.package) {
    throw Error('package.json could not be found')
  }
  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)
}

@@ -438,17 +438,108 @@
 X = 'v'%0A
-%0A
+const PACKAGE_OPTIONS_KEY = 'auto-changelog'%0A%0Afunction getOptions (pkg) %7B%0A  const options = 
 commande
@@ -542,16 +542,18 @@
 ander%0A  
+ 
+ 
 .option(
@@ -634,16 +634,18 @@
 OUTPUT)%0A
+  
   .optio
@@ -780,24 +780,26 @@
 TEMPLATE)%0A  
+ 
+ 
 .option('-r,
@@ -902,16 +902,18 @@
 REMOTE)%0A
+  
   .optio
@@ -982,16 +982,18 @@
 lease')%0A
+  
   .optio
@@ -1056,16 +1056,18 @@
 anges')%0A
+  
   .versi
@@ -1078,16 +1078,18 @@
 ersion)%0A
+  
   .parse
@@ -1108,189 +1108,145 @@
 v)%0A%0A
-export default async function run () %7B%0A  const pkg = await pathExists('package.json') && await readJson('package.json')%0A  let options = %7B ...commander %7D%0A  if (pkg) %7B%0A    options = %7B
+  if (!pkg) %7B%0A    if (options.package) %7B%0A      throw Error('package.json could not be found')%0A    %7D%0A    return options%0A  %7D%0A  return %7B%0A   
  ...
@@ -1257,16 +1257,20 @@
 ons,
+%0A   
  ...pkg%5B
 'aut
@@ -1269,116 +1269,189 @@
 pkg%5B
-'auto-changelog'%5D %7D%0A  %7D else if (commander.package) %7B%0A    throw Error('package.json could not be found')%0A  %7D
+PACKAGE_OPTIONS_KEY%5D%0A  %7D%0A%7D%0A%0Aexport default async function run () %7B%0A  const pkg = await pathExists('package.json') && await readJson('package.json')%0A  const options = getOptions(pkg)
 %0A  c

Tidy options logic