Skip to content

Commit

Permalink
Tidy up readJson util
Browse files Browse the repository at this point in the history
  • Loading branch information
cookpete committed Apr 16, 2019
1 parent aafce3e commit 36bfd9e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ async function getReleases (commits, remote, latestVersion, options) {
}

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 pkg = await readJson(PACKAGE_FILE)
const dotOptions = await readJson(OPTIONS_DOTFILE)
const options = getOptions(argv, pkg, dotOptions)
const log = string => options.stdout ? null : updateLog(string)
log('Fetching remote…')
Expand Down
6 changes: 4 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ export function fileExists (path) {
}

export async function readJson (path) {
const json = await readFile(path)
return JSON.parse(json)
if (await fileExists(path) === false) {
return null
}
return JSON.parse(await readFile(path))
}
5 changes: 4 additions & 1 deletion test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ describe('fileExists', () => {

describe('readJson', () => {
it('reads file', async () => {
mock('fs', { readFile: (path, type, cb) => cb(null, '{"abc":123}') })
mock('fs', {
readFile: (path, type, cb) => cb(null, '{"abc":123}'),
access: (path, cb) => cb(null)
})
expect(await readJson()).to.deep.equal({ abc: 123 })
unmock('cmd')
})
Expand Down

0 comments on commit 36bfd9e

Please sign in to comment.