Skip to content

Commit 02e50c5

Browse files
committed
Use async fs utils instead of fs-extra
Closes #42 Also, tidies up imports
1 parent 3c5ae3b commit 02e50c5

16 files changed

+64
-69
lines changed

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
"dependencies": {
4343
"babel-polyfill": "^6.26.0",
4444
"commander": "^2.9.0",
45-
"fs-extra": "^5.0.0",
4645
"handlebars": "^4.0.11",
4746
"lodash.uniqby": "^4.7.0",
4847
"parse-github-url": "^1.0.1",

scripts/generate-test-data.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { readFile, writeFile } from 'fs-extra'
21
import { join } from 'path'
3-
2+
import { readFile, writeFile } from '../src/utils'
43
import { __get__ } from '../src/commits'
54
import { parseReleases } from '../src/releases'
65
import { compileTemplate } from '../src/template'
@@ -21,7 +20,7 @@ const options = {
2120
}
2221

2322
async function run () {
24-
const gitLog = await readFile(join(DATA_DIR, 'git-log.txt'), 'utf-8')
23+
const gitLog = await readFile(join(DATA_DIR, 'git-log.txt'))
2524
const commits = parseCommits(gitLog, remote, options)
2625
const releases = parseReleases(commits, remote, null, options)
2726
await writeFile(join(DATA_DIR, 'commits.js'), 'export default ' + JSON.stringify(commits, null, 2))

src/commits.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import semver from 'semver'
2-
32
import { cmd, isLink, replaceText } from './utils'
43

54
const COMMIT_SEPARATOR = '__AUTO_CHANGELOG_COMMIT_SEPARATOR__'

src/releases.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import semver from 'semver'
2-
32
import { niceDate } from './utils'
43

54
const MERGE_COMMIT_PATTERN = /^Merge (remote-tracking )?branch '.+'/

src/remote.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import parseRepoURL from 'parse-github-url'
2-
32
import { cmd } from './utils'
43

54
export async function fetchRemote (name) {

src/run.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import { Command } from 'commander'
2-
import { readJson, writeFile, pathExists } from 'fs-extra'
32
import semver from 'semver'
43
import uniqBy from 'lodash.uniqby'
5-
64
import { version } from '../package.json'
75
import { fetchRemote } from './remote'
86
import { fetchCommits } from './commits'
97
import { parseReleases, sortReleases } from './releases'
108
import { compileTemplate } from './template'
11-
import { parseLimit } from './utils'
9+
import { parseLimit, readJson, writeFile, fileExists } from './utils'
1210

1311
const DEFAULT_OPTIONS = {
1412
output: 'CHANGELOG.md',
@@ -84,7 +82,7 @@ async function getReleases (commits, remote, latestVersion, options) {
8482
}
8583

8684
export default async function run (argv) {
87-
const pkg = await pathExists('package.json') && await readJson('package.json')
85+
const pkg = await fileExists('package.json') && await readJson('package.json')
8886
const options = getOptions(argv, pkg)
8987
const remote = await fetchRemote(options.remote)
9088
const commits = await fetchCommits(remote, options)

src/template.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { join } from 'path'
2-
import { readFile, pathExists } from 'fs-extra'
32
import Handlebars from 'handlebars'
4-
5-
import { removeIndentation } from './utils'
3+
import { removeIndentation, readFile, fileExists } from './utils'
64

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

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

5149
async function getTemplate (template) {
52-
if (await pathExists(template)) {
53-
return readFile(template, 'utf-8')
50+
if (await fileExists(template)) {
51+
return readFile(template)
5452
}
5553
const path = join(TEMPLATES_DIR, template + '.hbs')
56-
if (await pathExists(path) === false) {
54+
if (await fileExists(path) === false) {
5755
throw new Error(`Template '${template}' was not found`)
5856
}
59-
return readFile(path, 'utf-8')
57+
return readFile(path)
6058
}
6159

6260
export async function compileTemplate (template, data) {

src/utils.js

+29
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import fs from 'fs'
12
import { spawn } from 'child_process'
23

34
const MONTH_NAMES = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
@@ -45,3 +46,31 @@ export function replaceText (string, options) {
4546
return string.replace(new RegExp(pattern, 'g'), options.replaceText[pattern])
4647
}, string)
4748
}
49+
50+
const createCallback = (resolve, reject) => (err, data) => {
51+
if (err) reject(err)
52+
else resolve(data)
53+
}
54+
55+
export function readFile (path) {
56+
return new Promise((resolve, reject) => {
57+
fs.readFile(path, 'utf-8', createCallback(resolve, reject))
58+
})
59+
}
60+
61+
export function writeFile (path, data) {
62+
return new Promise((resolve, reject) => {
63+
fs.writeFile(path, data, createCallback(resolve, reject))
64+
})
65+
}
66+
67+
export function fileExists (path) {
68+
return new Promise(resolve => {
69+
fs.access(path, err => resolve(!err))
70+
})
71+
}
72+
73+
export async function readJson (path) {
74+
const json = await readFile(path)
75+
return JSON.parse(json)
76+
}

test/commits.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { describe, it } from 'mocha'
22
import { expect } from 'chai'
3-
import { readFile } from 'fs-extra'
43
import { join } from 'path'
5-
4+
import { readFile } from '../src/utils'
65
import remotes from './data/remotes'
76
import commits from './data/commits'
87
import commitsNoRemote from './data/commits-no-remote'
@@ -24,7 +23,7 @@ const options = {
2423

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

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

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

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

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

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

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

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

8382
it('invalid startingCommit throws an error', done => {
8483
const options = { startingCommit: 'not-a-hash' }
85-
readFile(join(__dirname, 'data', 'git-log.txt'), 'utf-8')
84+
readFile(join(__dirname, 'data', 'git-log.txt'))
8685
.then(gitLog => parseCommits(gitLog, remotes.github, options))
8786
.then(() => done('Should throw an error'))
8887
.catch(() => done())

test/matches-helper.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { describe, it } from 'mocha'
22
import { expect } from 'chai'
33
import Handlebars from 'handlebars'
4-
54
import releases from './data/releases'
65

76
describe('matches helper', () => {

test/releases.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { describe, it } from 'mocha'
22
import { expect } from 'chai'
3-
43
import remotes from './data/remotes'
54
import commits from './data/commits'
65
import releases from './data/releases'

test/remote.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { describe, it } from 'mocha'
22
import { expect } from 'chai'
3-
43
import remotes from './data/remotes'
54
import {
65
fetchRemote,

test/run.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { describe, it, beforeEach, afterEach } from 'mocha'
22
import { expect } from 'chai'
3-
import { readFile } from 'fs-extra'
43
import { join } from 'path'
5-
4+
import { readFile } from '../src/utils'
65
import remotes from './data/remotes'
76
import commits from './data/commits'
87
import commitsNoRemote from './data/commits-no-remote'
@@ -28,23 +27,23 @@ describe('getOptions', () => {
2827

2928
describe('run', () => {
3029
beforeEach(() => {
31-
mock('pathExists', () => false)
30+
mock('fileExists', () => false)
3231
mock('readJson', () => null)
3332
mock('fetchRemote', () => remotes.github)
3433
mock('fetchCommits', () => commits)
3534
mock('writeFile', () => {})
3635
})
3736

3837
afterEach(() => {
39-
unmock('pathExists')
38+
unmock('fileExists')
4039
unmock('readJson')
4140
unmock('fetchRemote')
4241
unmock('fetchCommits')
4342
unmock('writeFile')
4443
})
4544

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

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

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

6362
mock('fetchRemote', () => null)
6463
mock('fetchCommits', () => commitsNoRemote)
@@ -71,9 +70,9 @@ describe('run', () => {
7170
})
7271

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

76-
mock('pathExists', () => true)
75+
mock('fileExists', () => true)
7776
mock('readJson', () => ({
7877
'auto-changelog': {
7978
template: 'keepachangelog'
@@ -88,7 +87,7 @@ describe('run', () => {
8887
})
8988

9089
it('uses version from package.json', async () => {
91-
mock('pathExists', () => true)
90+
mock('fileExists', () => true)
9291
mock('readJson', () => ({
9392
version: '2.0.0'
9493
}))
@@ -100,7 +99,7 @@ describe('run', () => {
10099
})
101100

102101
it('uses version from package.json with no prefix', async () => {
103-
mock('pathExists', () => true)
102+
mock('fileExists', () => true)
104103
mock('readJson', () => ({
105104
version: '2.0.0'
106105
}))
@@ -119,7 +118,7 @@ describe('run', () => {
119118
})
120119

121120
it('command line options override options from package.json', async () => {
122-
mock('pathExists', () => true)
121+
mock('fileExists', () => true)
123122
mock('readJson', () => ({
124123
'auto-changelog': {
125124
output: 'should-not-be-this.md'

test/template.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
import { describe, it } from 'mocha'
22
import { expect } from 'chai'
3-
import { readFile } from 'fs-extra'
43
import { join } from 'path'
5-
4+
import { readFile } from '../src/utils'
65
import releases from './data/releases'
76
import { compileTemplate } from '../src/template'
87

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

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

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

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

test/utils.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { describe, it } from 'mocha'
22
import { expect } from 'chai'
3-
43
import { cmd, niceDate, removeIndentation, isLink } from '../src/utils'
54

65
describe('cmd', () => {

0 commit comments

Comments
 (0)