Skip to content

Commit

Permalink
refactor: return a Promise<Boolean> from the main entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronhunter authored and okonet committed Jul 6, 2019
1 parent 386ff4a commit 06269dd
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
14 changes: 6 additions & 8 deletions bin/lint-staged
Expand Up @@ -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
}
)
})
8 changes: 4 additions & 4 deletions src/index.js
Expand Up @@ -48,7 +48,7 @@ function loadConfig(configPath) {
* @param {boolean} [options.debug] - Enable debug mode
* @param {Logger} [logger]
*
* @returns {Promise<number>} Promise containing the exit code to use
* @returns {Promise<boolean>} Promise of whether the linting passed or failed
*/
module.exports = function lintStaged(
{ configPath, shell = false, quiet = false, debug = false } = {},
Expand Down Expand Up @@ -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 => {
Expand All @@ -102,6 +102,6 @@ module.exports = function lintStaged(
See https://github.com/okonet/lint-staged#configuration.
`)

return Promise.resolve(1)
return Promise.reject(err)
})
}
6 changes: 3 additions & 3 deletions test/__snapshots__/index.spec.js.snap
Expand Up @@ -50,15 +50,15 @@ 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
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.
Expand All @@ -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.
Expand Down
21 changes: 14 additions & 7 deletions test/index.spec.js
Expand Up @@ -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()
})

Expand Down Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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)
})
})

0 comments on commit 06269dd

Please sign in to comment.