Skip to content

Commit

Permalink
Use async fs utils instead of fs-extra
Browse files Browse the repository at this point in the history
Closes #42
Also, tidies up imports
  • Loading branch information
cookpete committed May 31, 2018
1 parent 3c5ae3b commit 02e50c5
Show file tree
Hide file tree
Showing 16 changed files with 64 additions and 69 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"dependencies": {
"babel-polyfill": "^6.26.0",
"commander": "^2.9.0",
"fs-extra": "^5.0.0",
"handlebars": "^4.0.11",
"lodash.uniqby": "^4.7.0",
"parse-github-url": "^1.0.1",
Expand Down
5 changes: 2 additions & 3 deletions scripts/generate-test-data.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { readFile, writeFile } from 'fs-extra'
import { join } from 'path'

import { readFile, writeFile } from '../src/utils'
import { __get__ } from '../src/commits'
import { parseReleases } from '../src/releases'
import { compileTemplate } from '../src/template'
Expand All @@ -21,7 +20,7 @@ const options = {
}

async function run () {
const gitLog = await readFile(join(DATA_DIR, 'git-log.txt'), 'utf-8')
const gitLog = await readFile(join(DATA_DIR, 'git-log.txt'))
const commits = parseCommits(gitLog, remote, options)
const releases = parseReleases(commits, remote, null, options)
await writeFile(join(DATA_DIR, 'commits.js'), 'export default ' + JSON.stringify(commits, null, 2))
Expand Down
1 change: 0 additions & 1 deletion src/commits.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import semver from 'semver'

import { cmd, isLink, replaceText } from './utils'

const COMMIT_SEPARATOR = '__AUTO_CHANGELOG_COMMIT_SEPARATOR__'
Expand Down
1 change: 0 additions & 1 deletion src/releases.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import semver from 'semver'

import { niceDate } from './utils'

const MERGE_COMMIT_PATTERN = /^Merge (remote-tracking )?branch '.+'/
Expand Down
1 change: 0 additions & 1 deletion src/remote.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import parseRepoURL from 'parse-github-url'

import { cmd } from './utils'

export async function fetchRemote (name) {
Expand Down
6 changes: 2 additions & 4 deletions src/run.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { Command } from 'commander'
import { readJson, writeFile, pathExists } from 'fs-extra'
import semver from 'semver'
import uniqBy from 'lodash.uniqby'

import { version } from '../package.json'
import { fetchRemote } from './remote'
import { fetchCommits } from './commits'
import { parseReleases, sortReleases } from './releases'
import { compileTemplate } from './template'
import { parseLimit } from './utils'
import { parseLimit, readJson, writeFile, fileExists } from './utils'

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

export default async function run (argv) {
const pkg = await pathExists('package.json') && await readJson('package.json')
const pkg = await fileExists('package.json') && await readJson('package.json')
const options = getOptions(argv, pkg)
const remote = await fetchRemote(options.remote)
const commits = await fetchCommits(remote, options)
Expand Down
12 changes: 5 additions & 7 deletions src/template.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { join } from 'path'
import { readFile, pathExists } from 'fs-extra'
import Handlebars from 'handlebars'

import { removeIndentation } from './utils'
import { removeIndentation, readFile, fileExists } from './utils'

const TEMPLATES_DIR = join(__dirname, '..', 'templates')

Expand Down Expand Up @@ -49,14 +47,14 @@ Handlebars.registerHelper('matches', function (val, pattern, options) {
})

async function getTemplate (template) {
if (await pathExists(template)) {
return readFile(template, 'utf-8')
if (await fileExists(template)) {
return readFile(template)
}
const path = join(TEMPLATES_DIR, template + '.hbs')
if (await pathExists(path) === false) {
if (await fileExists(path) === false) {
throw new Error(`Template '${template}' was not found`)
}
return readFile(path, 'utf-8')
return readFile(path)
}

export async function compileTemplate (template, data) {
Expand Down
29 changes: 29 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'fs'
import { spawn } from 'child_process'

const MONTH_NAMES = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
Expand Down Expand Up @@ -45,3 +46,31 @@ export function replaceText (string, options) {
return string.replace(new RegExp(pattern, 'g'), options.replaceText[pattern])
}, string)
}

const createCallback = (resolve, reject) => (err, data) => {
if (err) reject(err)
else resolve(data)
}

export function readFile (path) {
return new Promise((resolve, reject) => {
fs.readFile(path, 'utf-8', createCallback(resolve, reject))
})
}

export function writeFile (path, data) {
return new Promise((resolve, reject) => {
fs.writeFile(path, data, createCallback(resolve, reject))
})
}

export function fileExists (path) {
return new Promise(resolve => {
fs.access(path, err => resolve(!err))
})
}

export async function readJson (path) {
const json = await readFile(path)
return JSON.parse(json)
}
21 changes: 10 additions & 11 deletions test/commits.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { describe, it } from 'mocha'
import { expect } from 'chai'
import { readFile } from 'fs-extra'
import { join } from 'path'

import { readFile } from '../src/utils'
import remotes from './data/remotes'
import commits from './data/commits'
import commitsNoRemote from './data/commits-no-remote'
Expand All @@ -24,7 +23,7 @@ const options = {

describe('fetchCommits', () => {
it('fetches commits', async () => {
const gitLog = await readFile(join(__dirname, 'data', 'git-log.txt'), 'utf-8')
const gitLog = await readFile(join(__dirname, 'data', 'git-log.txt'))
mock('cmd', () => gitLog)
expect(await fetchCommits(remotes.github, options)).to.deep.equal(commits)
unmock('cmd')
Expand All @@ -33,44 +32,44 @@ describe('fetchCommits', () => {

describe('parseCommits', () => {
it('parses commits', async () => {
const gitLog = await readFile(join(__dirname, 'data', 'git-log.txt'), 'utf-8')
const gitLog = await readFile(join(__dirname, 'data', 'git-log.txt'))
expect(parseCommits(gitLog, remotes.github, options)).to.deep.equal(commits)
})

it('parses commits without remote', async () => {
const gitLog = await readFile(join(__dirname, 'data', 'git-log.txt'), 'utf-8')
const gitLog = await readFile(join(__dirname, 'data', 'git-log.txt'))
expect(parseCommits(gitLog, null, options)).to.deep.equal(commitsNoRemote)
})

it('parses bitbucket commits', async () => {
const gitLog = await readFile(join(__dirname, 'data', 'git-log.txt'), 'utf-8')
const gitLog = await readFile(join(__dirname, 'data', 'git-log.txt'))
const commits = parseCommits(gitLog, remotes.bitbucket)
expect(commits[0].href).to.equal('https://bitbucket.org/user/repo/commits/2401ee4706e94629f48830bab9ed5812c032734a')
})

it('supports startingCommit option', async () => {
const gitLog = await readFile(join(__dirname, 'data', 'git-log.txt'), 'utf-8')
const gitLog = await readFile(join(__dirname, 'data', 'git-log.txt'))
const options = { startingCommit: '17fbef87e82889f01d8257900f7edc55b05918a2' }
expect(parseCommits(gitLog, remotes.github, options)).to.have.length(10)
})

it('supports ignoreCommitPattern option', async () => {
const gitLog = await readFile(join(__dirname, 'data', 'git-log.txt'), 'utf-8')
const gitLog = await readFile(join(__dirname, 'data', 'git-log.txt'))
const options = { ignoreCommitPattern: 'Second commit' }
const result = parseCommits(gitLog, remotes.github, options)
expect(result).to.have.length(commits.length - 1)
expect(JSON.stringify(result)).to.not.contain('Second commit')
})

it('supports breakingPattern option', async () => {
const gitLog = await readFile(join(__dirname, 'data', 'git-log.txt'), 'utf-8')
const gitLog = await readFile(join(__dirname, 'data', 'git-log.txt'))
const options = { breakingPattern: 'Some breaking change' }
const result = parseCommits(gitLog, remotes.github, options)
expect(result.filter(c => c.breaking)).to.have.length(1)
})

it('supports replaceText option', async () => {
const gitLog = await readFile(join(__dirname, 'data', 'git-log.txt'), 'utf-8')
const gitLog = await readFile(join(__dirname, 'data', 'git-log.txt'))
const options = {
replaceText: {
'breaking': '**BREAKING**'
Expand All @@ -82,7 +81,7 @@ describe('parseCommits', () => {

it('invalid startingCommit throws an error', done => {
const options = { startingCommit: 'not-a-hash' }
readFile(join(__dirname, 'data', 'git-log.txt'), 'utf-8')
readFile(join(__dirname, 'data', 'git-log.txt'))
.then(gitLog => parseCommits(gitLog, remotes.github, options))
.then(() => done('Should throw an error'))
.catch(() => done())
Expand Down
1 change: 0 additions & 1 deletion test/matches-helper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { describe, it } from 'mocha'
import { expect } from 'chai'
import Handlebars from 'handlebars'

import releases from './data/releases'

describe('matches helper', () => {
Expand Down
1 change: 0 additions & 1 deletion test/releases.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, it } from 'mocha'
import { expect } from 'chai'

import remotes from './data/remotes'
import commits from './data/commits'
import releases from './data/releases'
Expand Down
1 change: 0 additions & 1 deletion test/remote.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, it } from 'mocha'
import { expect } from 'chai'

import remotes from './data/remotes'
import {
fetchRemote,
Expand Down
21 changes: 10 additions & 11 deletions test/run.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { describe, it, beforeEach, afterEach } from 'mocha'
import { expect } from 'chai'
import { readFile } from 'fs-extra'
import { join } from 'path'

import { readFile } from '../src/utils'
import remotes from './data/remotes'
import commits from './data/commits'
import commitsNoRemote from './data/commits-no-remote'
Expand All @@ -28,23 +27,23 @@ describe('getOptions', () => {

describe('run', () => {
beforeEach(() => {
mock('pathExists', () => false)
mock('fileExists', () => false)
mock('readJson', () => null)
mock('fetchRemote', () => remotes.github)
mock('fetchCommits', () => commits)
mock('writeFile', () => {})
})

afterEach(() => {
unmock('pathExists')
unmock('fileExists')
unmock('readJson')
unmock('fetchRemote')
unmock('fetchCommits')
unmock('writeFile')
})

it('generates a changelog', async () => {
const expected = await readFile(join(__dirname, 'data', 'template-compact.md'), 'utf-8')
const expected = await readFile(join(__dirname, 'data', 'template-compact.md'))

mock('writeFile', (output, log) => {
expect(output).to.equal('CHANGELOG.md')
Expand All @@ -58,7 +57,7 @@ describe('run', () => {
})

it('generates a changelog with no remote', async () => {
const expected = await readFile(join(__dirname, 'data', 'template-compact-no-remote.md'), 'utf-8')
const expected = await readFile(join(__dirname, 'data', 'template-compact-no-remote.md'))

mock('fetchRemote', () => null)
mock('fetchCommits', () => commitsNoRemote)
Expand All @@ -71,9 +70,9 @@ describe('run', () => {
})

it('uses options from package.json', async () => {
const expected = await readFile(join(__dirname, 'data', 'template-keepachangelog.md'), 'utf-8')
const expected = await readFile(join(__dirname, 'data', 'template-keepachangelog.md'))

mock('pathExists', () => true)
mock('fileExists', () => true)
mock('readJson', () => ({
'auto-changelog': {
template: 'keepachangelog'
Expand All @@ -88,7 +87,7 @@ describe('run', () => {
})

it('uses version from package.json', async () => {
mock('pathExists', () => true)
mock('fileExists', () => true)
mock('readJson', () => ({
version: '2.0.0'
}))
Expand All @@ -100,7 +99,7 @@ describe('run', () => {
})

it('uses version from package.json with no prefix', async () => {
mock('pathExists', () => true)
mock('fileExists', () => true)
mock('readJson', () => ({
version: '2.0.0'
}))
Expand All @@ -119,7 +118,7 @@ describe('run', () => {
})

it('command line options override options from package.json', async () => {
mock('pathExists', () => true)
mock('fileExists', () => true)
mock('readJson', () => ({
'auto-changelog': {
output: 'should-not-be-this.md'
Expand Down
11 changes: 5 additions & 6 deletions test/template.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
import { describe, it } from 'mocha'
import { expect } from 'chai'
import { readFile } from 'fs-extra'
import { join } from 'path'

import { readFile } from '../src/utils'
import releases from './data/releases'
import { compileTemplate } from '../src/template'

describe('compileTemplate', () => {
it('compiles using compact template', async () => {
const expected = await readFile(join(__dirname, 'data', 'template-compact.md'), 'utf-8')
const expected = await readFile(join(__dirname, 'data', 'template-compact.md'))
expect(await compileTemplate('compact', { releases })).to.equal(expected)
})

it('compiles using keepachangelog template', async () => {
const expected = await readFile(join(__dirname, 'data', 'template-keepachangelog.md'), 'utf-8')
const expected = await readFile(join(__dirname, 'data', 'template-keepachangelog.md'))
expect(await compileTemplate('keepachangelog', { releases })).to.equal(expected)
})

it('compiles using json template', async () => {
const expected = await readFile(join(__dirname, 'data', 'template-json.json'), 'utf-8')
const expected = await readFile(join(__dirname, 'data', 'template-json.json'))
expect(await compileTemplate('json', { releases })).to.equal(expected)
})

it('compiles using path to template file', async () => {
const path = join(__dirname, 'data', 'template-compact.md')
const expected = await readFile(path, 'utf-8')
const expected = await readFile(path)
expect(await compileTemplate(path, { releases })).to.equal(expected)
})

Expand Down
1 change: 0 additions & 1 deletion test/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, it } from 'mocha'
import { expect } from 'chai'

import { cmd, niceDate, removeIndentation, isLink } from '../src/utils'

describe('cmd', () => {
Expand Down
Loading

0 comments on commit 02e50c5

Please sign in to comment.