From 30fa5940ae4be9bfd7a8cf6a6012b674e6230ce2 Mon Sep 17 00:00:00 2001 From: Suhas Karanth Date: Fri, 15 Sep 2017 18:14:58 +0530 Subject: [PATCH] feat: Print friendlier error if config is missing (#281) --- src/index.js | 22 +++++++++++++++++----- test/__snapshots__/index.spec.js.snap | 9 +++++++++ test/index.spec.js | 10 ++++++++-- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/index.js b/src/index.js index 26067c91b..10d3f9837 100644 --- a/src/index.js +++ b/src/index.js @@ -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)) @@ -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 }) diff --git a/test/__snapshots__/index.spec.js.snap b/test/__snapshots__/index.spec.js.snap index a21768625..aa1e5b68f 100644 --- a/test/__snapshots__/index.spec.js.snap +++ b/test/__snapshots__/index.spec.js.snap @@ -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. +" +`; diff --git a/test/index.spec.js b/test/index.spec.js index 291f580d5..0f6771c0e 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -20,7 +20,7 @@ describe('lintStaged', () => { '*': 'mytask' } } - cosmiconfig.mockImplementation(() => Promise.resolve({ config })) + cosmiconfig.mockImplementationOnce(() => Promise.resolve({ config })) await lintStaged() expect(console.printHistory()).toMatchSnapshot() }) @@ -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() })