From d39e4349e566c1370eced061d008a3bda8f56ebd Mon Sep 17 00:00:00 2001 From: Pete Cook Date: Mon, 14 Jan 2019 15:37:20 +0000 Subject: [PATCH] Add --stdout option Fixes https://github.com/CookPete/auto-changelog/issues/83 --- README.md | 1 + src/run.js | 10 ++++++++-- src/utils.js | 2 +- test/run.js | 4 ++++ test/utils.js | 8 ++++---- 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f0737b6e..a73cc453 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/run.js b/src/run.js index 926be56c..5fd3b130 100644 --- a/src/run.js +++ b/src/run.js @@ -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', @@ -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) @@ -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`) @@ -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`) } diff --git a/src/utils.js b/src/utils.js index fef66c54..cefc3e75 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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) diff --git a/test/run.js b/test/run.js index b923f982..665688d6 100644 --- a/test/run.js +++ b/test/run.js @@ -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')) diff --git a/test/utils.js b/test/utils.js index af5da640..97c1568a 100644 --- a/test/utils.js +++ b/test/utils.js @@ -1,7 +1,7 @@ import { describe, it } from 'mocha' import { expect } from 'chai' import { - log, + updateLog, cmd, niceDate, isLink, @@ -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') }) })