From 06269dd3ea067985b8f36af1a231e6bc7bd983ea Mon Sep 17 00:00:00 2001 From: Cameron Hunter Date: Thu, 4 Jul 2019 22:46:13 -0700 Subject: [PATCH] refactor: return a Promise from the main entrypoint --- bin/lint-staged | 14 ++++++-------- src/index.js | 8 ++++---- test/__snapshots__/index.spec.js.snap | 6 +++--- test/index.spec.js | 21 ++++++++++++++------- 4 files changed, 27 insertions(+), 22 deletions(-) diff --git a/bin/lint-staged b/bin/lint-staged index 9ba9551df..412799312 100755 --- a/bin/lint-staged +++ b/bin/lint-staged @@ -43,12 +43,10 @@ lintStaged({ shell: !!cmdline.shell, quiet: !!cmdline.quiet, debug: !!cmdline.debug -}).then( - exitCode => { - process.exitCode = exitCode - }, - err => { - console.error(err) +}) + .then(passed => { + process.exitCode = passed ? 0 : 1 + }) + .catch(() => { process.exitCode = 1 - } -) + }) diff --git a/src/index.js b/src/index.js index 2673da278..f8afd8a6b 100644 --- a/src/index.js +++ b/src/index.js @@ -48,7 +48,7 @@ function loadConfig(configPath) { * @param {boolean} [options.debug] - Enable debug mode * @param {Logger} [logger] * - * @returns {Promise} Promise containing the exit code to use + * @returns {Promise} Promise of whether the linting passed or failed */ module.exports = function lintStaged( { configPath, shell = false, quiet = false, debug = false } = {}, @@ -77,11 +77,11 @@ module.exports = function lintStaged( return runAll(config, shell, quiet, debug, logger) .then(() => { debugLog('linters were executed successfully!') - return Promise.resolve(0) + return Promise.resolve(true) }) .catch(error => { printErrors(error, logger) - return Promise.resolve(1) + return Promise.resolve(false) }) }) .catch(err => { @@ -102,6 +102,6 @@ module.exports = function lintStaged( See https://github.com/okonet/lint-staged#configuration. `) - return Promise.resolve(1) + return Promise.reject(err) }) } diff --git a/test/__snapshots__/index.spec.js.snap b/test/__snapshots__/index.spec.js.snap index 552008507..61101f37e 100644 --- a/test/__snapshots__/index.spec.js.snap +++ b/test/__snapshots__/index.spec.js.snap @@ -50,7 +50,7 @@ LOG { ERROR Unable to get staged files!" `; -exports[`lintStaged should print helpful error message when config file is not found 1`] = ` +exports[`lintStaged should print helpful error message when config file is not found 2`] = ` " ERROR Config could not be found. ERROR @@ -58,7 +58,7 @@ ERROR Please make sure you have created it correctly. See https://github.com/okonet/lint-staged#configuration." `; -exports[`lintStaged should print helpful error message when explicit config file is not found 1`] = ` +exports[`lintStaged should print helpful error message when explicit config file is not found 2`] = ` ERROR Could not parse lint-staged config. @@ -68,7 +68,7 @@ ERROR Please make sure you have created it correctly. See https://github.com/okonet/lint-staged#configuration. `; -exports[`lintStaged should throw when invalid config is provided 1`] = ` +exports[`lintStaged should throw when invalid config is provided 2`] = ` " ERROR Could not parse lint-staged config. diff --git a/test/index.spec.js b/test/index.spec.js index 05c9f8bf7..5f0c55a11 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -56,7 +56,9 @@ describe('lintStaged', () => { it('should throw when invalid config is provided', async () => { const config = {} mockCosmiconfigWith({ config }) - await lintStaged({ quiet: true }, logger) + await expect(lintStaged({ quiet: true }, logger)).rejects.toMatchInlineSnapshot( + `[Error: Configuration should not be empty!]` + ) expect(logger.printHistory()).toMatchSnapshot() }) @@ -96,9 +98,10 @@ describe('lintStaged', () => { it('should print helpful error message when config file is not found', async () => { expect.assertions(2) mockCosmiconfigWith(null) - const exitCode = await lintStaged({ quiet: true }, logger) + await expect(lintStaged({ quiet: true }, logger)).rejects.toMatchInlineSnapshot( + `[Error: Config could not be found]` + ) expect(logger.printHistory()).toMatchSnapshot() - expect(exitCode).toEqual(1) }) it('should print helpful error message when explicit config file is not found', async () => { @@ -113,9 +116,13 @@ describe('lintStaged', () => { ) ) - const exitCode = await lintStaged({ configPath: nonExistentConfig, quiet: true }, logger) + await expect( + lintStaged({ configPath: nonExistentConfig, quiet: true }, logger) + ).rejects.toMatchInlineSnapshot( + `[Error: ENOENT: no such file or directory, open '/Users/chunter/workspace/github/cameronhunter/lint-staged/fake-config-file.yml']` + ) + expect(logger.printHistory()).toMatchSnapshot() - expect(exitCode).toEqual(1) }) it('should exit with code 1 on linter errors', async () => { @@ -124,8 +131,8 @@ describe('lintStaged', () => { } mockCosmiconfigWith({ config }) getStagedFiles.mockImplementationOnce(async () => ['sample.java']) - const exitCode = await lintStaged({ quiet: true }, logger) + const passed = await lintStaged({ quiet: true }, logger) expect(logger.printHistory()).toMatchSnapshot() - expect(exitCode).toEqual(1) + expect(passed).toBe(false) }) })