Skip to content

Commit

Permalink
Add better progress logging
Browse files Browse the repository at this point in the history
  • Loading branch information
cookpete committed Nov 10, 2018
1 parent 5240b1e commit 64ebb30
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/commits.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ const MERGE_PATTERNS = [
/Merge branch .+ into .+\n\n(.+)[\S\s]+See merge request [^!]*!(\d+)/ // GitLab merge
]

export async function fetchCommits (remote, options, branch = null) {
export async function fetchCommits (remote, options, branch = null, onProgress) {
const command = branch ? `git log ${branch}` : 'git log'
const format = await getLogFormat()
const log = await cmd(`${command} --shortstat --pretty=format:${format}`)
const log = await cmd(`${command} --shortstat --pretty=format:${format}`, onProgress)
return parseCommits(log, remote, options)
}

Expand Down
4 changes: 0 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import 'babel-polyfill'
import run from './run'

run(process.argv)
.then(message => {
console.log(message)
process.exit(0)
})
.catch(error => {
console.error(error)
process.exit(1)
Expand Down
15 changes: 10 additions & 5 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 } from './utils'
import { parseLimit, readJson, writeFile, fileExists, log, formatBytes } from './utils'

const DEFAULT_OPTIONS = {
output: 'CHANGELOG.md',
Expand Down Expand Up @@ -90,14 +90,19 @@ async function getReleases (commits, remote, latestVersion, options) {
}

export default async function run (argv) {
log('Fetching config…')
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)
log('Fetching remote…')
const remote = await fetchRemote(options.remote)
const commits = await fetchCommits(remote, options)
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 releases = await getReleases(commits, remote, latestVersion, options)
const log = await compileTemplate(options.template, { releases })
await writeFile(options.output, log)
return `${Buffer.byteLength(log, 'utf8')} bytes written to ${options.output}`
const changelog = await compileTemplate(options.template, { releases })
await writeFile(options.output, changelog)
const bytes = Buffer.byteLength(changelog, 'utf8')
log(`${formatBytes(bytes)} written to ${options.output}\n`)
}
21 changes: 19 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,31 @@ 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) {
if (clearLine) {
process.stdout.clearLine()
process.stdout.cursorTo(0)
}
process.stdout.write(`auto-changelog: ${string}`)
}

export function formatBytes (bytes) {
return `${Math.max(1, Math.round(bytes / 1024))} kB`
}

// Simple util for calling a child process
export function cmd (string) {
export function cmd (string, onProgress) {
const [ cmd, ...args ] = string.split(' ')
return new Promise((resolve, reject) => {
const child = spawn(cmd, args)
let data = ''

child.stdout.on('data', buffer => { data += buffer.toString() })
child.stdout.on('data', buffer => {
data += buffer.toString()
if (onProgress) {
onProgress(data.length)
}
})
child.stdout.on('end', () => resolve(data))
child.on('error', reject)
})
Expand Down
7 changes: 3 additions & 4 deletions test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('run', () => {
mock('fetchRemote', () => remotes.github)
mock('fetchCommits', () => commits)
mock('writeFile', () => {})
mock('log', () => {})
})

afterEach(() => {
Expand All @@ -40,6 +41,7 @@ describe('run', () => {
unmock('fetchRemote')
unmock('fetchCommits')
unmock('writeFile')
unmock('log')
})

it('generates a changelog', async () => {
Expand All @@ -50,10 +52,7 @@ describe('run', () => {
expect(log).to.equal(expected)
})

return run(['', '']).then(message => {
expect(message).to.be.a('string')
expect(message).to.have.string('bytes written to')
})
return run(['', ''])
})

it('generates a changelog with no remote', async () => {
Expand Down

0 comments on commit 64ebb30

Please sign in to comment.