Skip to content

Commit

Permalink
Update --package option to accept file path
Browse files Browse the repository at this point in the history
Setting --package with no path will still default to `package.json`, for back compatibility
Fixes #106
  • Loading branch information
cookpete committed Apr 16, 2019
1 parent 9298a71 commit aafce3e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function getOptions (argv, pkg, dotOptions) {
.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}`)
.option('-r, --remote [remote]', `specify git remote to use for links, default: ${DEFAULT_OPTIONS.remote}`)
.option('-p, --package', 'use version from package.json as latest release')
.option('-p, --package [file]', 'use version from file as latest release, default: package.json')
.option('-v, --latest-version [version]', 'use specified version as latest release')
.option('-u, --unreleased', 'include section for unreleased changes')
.option('-l, --commit-limit [count]', `number of commits to display per release, default: ${DEFAULT_OPTIONS.commitLimit}`, parseLimit)
Expand Down Expand Up @@ -66,16 +66,21 @@ function getOptions (argv, pkg, dotOptions) {
}
}

function getLatestVersion (options, pkg, commits) {
async function getLatestVersion (options, commits) {
if (options.latestVersion) {
if (!semver.valid(options.latestVersion)) {
throw new Error('--latest-version must be a valid semver version')
}
return options.latestVersion
}
if (options.package) {
const file = options.package === true ? PACKAGE_FILE : options.package
if (await fileExists(file) === false) {
throw new Error(`File ${file} does not exist`)
}
const { version } = await readJson(file)
const prefix = commits.some(c => /^v/.test(c.tag)) ? 'v' : ''
return `${prefix}${pkg.version}`
return `${prefix}${version}`
}
return null
}
Expand Down Expand Up @@ -104,7 +109,7 @@ export default async function run (argv) {
const commitProgress = bytes => log(`Fetching commits… ${formatBytes(bytes)} loaded`)
const commits = await fetchCommits(remote, options, null, commitProgress)
log('Generating changelog…')
const latestVersion = getLatestVersion(options, pkg, commits)
const latestVersion = await getLatestVersion(options, commits)
const releases = await getReleases(commits, remote, latestVersion, options)
const changelog = await compileTemplate(options, { releases })
if (options.stdout) {
Expand Down
21 changes: 21 additions & 0 deletions test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ describe('run', () => {
return run(['', '', '--package'])
})

it('uses version from custom package file', async () => {
mock('fileExists', () => true)
mock('readJson', file => {
if (file === 'test.json') {
return { version: '2.0.0' }
}
return {}
})
mock('writeFile', (output, log) => {
expect(log).to.include('v2.0.0')
})

return run(['', '', '--package', 'test.json'])
})

it('uses version from package.json with no prefix', async () => {
mock('fileExists', () => true)
mock('readJson', () => ({
Expand Down Expand Up @@ -214,6 +229,12 @@ describe('run', () => {
.catch(() => done())
})

it('throws an error when no custom package found', done => {
run(['', '', '--package', 'does-not-exist.json'])
.then(() => done('Should throw an error'))
.catch(() => done())
})

it('throws an error when no template found', done => {
run(['', '', '--template', 'not-found'])
.then(() => done('Should throw an error'))
Expand Down

0 comments on commit aafce3e

Please sign in to comment.