Repository: okonet/lint-staged (js)

Issue:
Better error handling If config is missing
Right now, if `lint-staged` is run on a project with no config file, this happens:

```
Could not parse lint-staged config.
Make sure you have created it. See https://github.com/okonet/lint-staged#readme.

TypeError: Cannot read property 'config' of null
```

Would be great if lint-staged could say for sure:

```
Config could not be found. 

Please make sure you have created it. See https://github.com/okonet/lint-staged#configuration.
```

Pull request:
feat: Better error handling if config is missing
Closes #280.

Patch:
diff --git a/src/index.js b/src/index.js
index 26067c9..10d3f98 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
     })


Test patch:
diff --git a/test/__snapshots__/index.spec.js.snap b/test/__snapshots__/index.spec.js.snap
index a217686..aa1e5b6 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 291f580..0f6771c 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()
   })