Skip to content

Commit 36bfd9e

Browse files
committed
Tidy up readJson util
1 parent aafce3e commit 36bfd9e

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

src/run.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ async function getReleases (commits, remote, latestVersion, options) {
100100
}
101101

102102
export default async function run (argv) {
103-
const pkg = await fileExists(PACKAGE_FILE) && await readJson(PACKAGE_FILE)
104-
const dotOptions = await fileExists(OPTIONS_DOTFILE) && await readJson(OPTIONS_DOTFILE)
103+
const pkg = await readJson(PACKAGE_FILE)
104+
const dotOptions = await readJson(OPTIONS_DOTFILE)
105105
const options = getOptions(argv, pkg, dotOptions)
106106
const log = string => options.stdout ? null : updateLog(string)
107107
log('Fetching remote…')

src/utils.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ export function fileExists (path) {
8989
}
9090

9191
export async function readJson (path) {
92-
const json = await readFile(path)
93-
return JSON.parse(json)
92+
if (await fileExists(path) === false) {
93+
return null
94+
}
95+
return JSON.parse(await readFile(path))
9496
}

test/utils.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ describe('fileExists', () => {
9595

9696
describe('readJson', () => {
9797
it('reads file', async () => {
98-
mock('fs', { readFile: (path, type, cb) => cb(null, '{"abc":123}') })
98+
mock('fs', {
99+
readFile: (path, type, cb) => cb(null, '{"abc":123}'),
100+
access: (path, cb) => cb(null)
101+
})
99102
expect(await readJson()).to.deep.equal({ abc: 123 })
100103
unmock('cmd')
101104
})

0 commit comments

Comments
 (0)