Skip to content

Commit

Permalink
feat: Print friendlier error if config is missing (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
sudo-suhas authored and okonet committed Sep 15, 2017
1 parent 0077644 commit 30fa594
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
22 changes: 17 additions & 5 deletions src/index.js
Expand Up @@ -21,15 +21,19 @@ if (process.stdout.isTTY) {
process.env.FORCE_COLOR = true
}

const errConfigNotFound = new Error('Config could not be found')

/**
* Root lint-staged function that is called from .bin
*/
module.exports = function lintStaged() {
cosmiconfig('lint-staged', {
return cosmiconfig('lint-staged', {
rc: '.lintstagedrc',
rcExtensions: true
})
.then(result => {
if (result == null) throw errConfigNotFound

// result.config is the parsed configuration object
// result.filepath is the path to the config file that was found
const config = validateConfig(getConfig(result.config))
Expand All @@ -52,11 +56,19 @@ ${stringifyObject(config)}
process.exitCode = 1
})
})
.catch(parsingError => {
console.error(`Could not parse lint-staged config.
Make sure you have created it. See https://github.com/okonet/lint-staged#readme.
.catch(err => {
if (err === errConfigNotFound) {
console.error(`${err.message}.`)
} else {
// It was probably a parsing error
console.error(`Could not parse lint-staged config.
${parsingError}
${err}`)
}
// Print helpful message for all errors
console.error(`
Please make sure you have created it correctly.
See https://github.com/okonet/lint-staged#configuration.
`)
process.exitCode = 1
})
Expand Down
9 changes: 9 additions & 0 deletions test/__snapshots__/index.spec.js.snap
Expand Up @@ -23,3 +23,12 @@ Running lint-staged with the following config:
}
"
`;

exports[`lintStaged should print helpful error message when config file is not found 1`] = `
"
ERROR Config could not be found.
ERROR
Please make sure you have created it correctly.
See https://github.com/okonet/lint-staged#configuration.
"
`;
10 changes: 8 additions & 2 deletions test/index.spec.js
Expand Up @@ -20,7 +20,7 @@ describe('lintStaged', () => {
'*': 'mytask'
}
}
cosmiconfig.mockImplementation(() => Promise.resolve({ config }))
cosmiconfig.mockImplementationOnce(() => Promise.resolve({ config }))
await lintStaged()
expect(console.printHistory()).toMatchSnapshot()
})
Expand All @@ -29,7 +29,13 @@ describe('lintStaged', () => {
const config = {
'*': 'mytask'
}
cosmiconfig.mockImplementation(() => Promise.resolve({ config }))
cosmiconfig.mockImplementationOnce(() => Promise.resolve({ config }))
await lintStaged()
expect(console.printHistory()).toMatchSnapshot()
})

it('should print helpful error message when config file is not found', async () => {
cosmiconfig.mockImplementationOnce(() => Promise.resolve(null))
await lintStaged()
expect(console.printHistory()).toMatchSnapshot()
})
Expand Down

0 comments on commit 30fa594

Please sign in to comment.