Skip to content

Commit

Permalink
Add --stdout option
Browse files Browse the repository at this point in the history
  • Loading branch information
cookpete authored and mattlyons0 committed Feb 14, 2019
1 parent bd02a9d commit d39e434
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Options:
--starting-commit [hash] # starting commit to use for changelog generation
--include-branch [branch] # one or more branches to include commits from, comma separated
--release-summary # display tagged commit message body as release summary
--stdout # output changelog to stdout
-V, --version # output the version number
-h, --help # output usage information

Expand Down
10 changes: 8 additions & 2 deletions src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { fetchRemote } from './remote'
import { fetchCommits } from './commits'
import { parseReleases, sortReleases } from './releases'
import { compileTemplate } from './template'
import { parseLimit, readJson, writeFile, fileExists, log, formatBytes } from './utils'
import { parseLimit, readJson, writeFile, fileExists, updateLog, formatBytes } from './utils'

const DEFAULT_OPTIONS = {
output: 'CHANGELOG.md',
Expand Down Expand Up @@ -41,6 +41,7 @@ function getOptions (argv, pkg, dotOptions) {
.option('--include-branch [branch]', 'one or more branches to include commits from, comma separated', str => str.split(','))
.option('--release-summary', 'use tagged commit message body as release summary')
.option('--platform [platform]', 'set platform manually [bitbucket, gitlab, azure]')
.option('--stdout', 'output changelog to stdout')
.version(version)
.parse(argv)

Expand Down Expand Up @@ -94,6 +95,7 @@ export default async function run (argv) {
const pkg = await fileExists(PACKAGE_FILE) && await readJson(PACKAGE_FILE)
const dotOptions = await fileExists(OPTIONS_DOTFILE) && await readJson(OPTIONS_DOTFILE)
const options = getOptions(argv, pkg, dotOptions)
const log = string => options.stdout ? null : updateLog(string)
log('Fetching remote…')
const remote = await fetchRemote(options.remote)
const commitProgress = bytes => log(`Fetching commits… ${formatBytes(bytes)} loaded`)
Expand All @@ -102,7 +104,11 @@ export default async function run (argv) {
const latestVersion = getLatestVersion(options, pkg, commits)
const releases = await getReleases(commits, remote, latestVersion, options)
const changelog = await compileTemplate(options.template, { releases })
await writeFile(options.output, changelog)
if (options.stdout) {
process.stdout.write(changelog)
} else {
await writeFile(options.output, changelog)
}
const bytes = Buffer.byteLength(changelog, 'utf8')
log(`${formatBytes(bytes)} written to ${options.output}\n`)
}
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { spawn } from 'child_process'

const MONTH_NAMES = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

export function log (string, clearLine = true) {
export function updateLog (string, clearLine = true) {
if (clearLine) {
readline.clearLine(process.stdout)
readline.cursorTo(process.stdout, 0)
Expand Down
4 changes: 4 additions & 0 deletions test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ describe('run', () => {
return run(['', '', '--latest-version', 'v3.0.0'])
})

it('does not error when using stdout option', () => {
return run(['', '', '--stdout'])
})

it('throws an error when no package found', done => {
run(['', '', '--package'])
.then(() => done('Should throw an error'))
Expand Down
8 changes: 4 additions & 4 deletions test/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, it } from 'mocha'
import { expect } from 'chai'
import {
log,
updateLog,
cmd,
niceDate,
isLink,
Expand All @@ -14,10 +14,10 @@ import {
__ResetDependency__ as unmock
} from '../src/utils'

describe('log', () => {
describe('updateLog', () => {
it('doesn\'t error', async () => {
log('Test', false)
log('Test')
updateLog('Test', false)
updateLog('Test')
})
})

Expand Down

0 comments on commit d39e434

Please sign in to comment.