Skip to content

Commit

Permalink
feat: make node-api accessible
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronhunter authored and okonet committed Jul 6, 2019
1 parent 28da59a commit ca37906
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 25 deletions.
20 changes: 18 additions & 2 deletions index.js → bin/lint-staged
Expand Up @@ -2,7 +2,14 @@

'use strict'

const pkg = require('./package.json')
// Force colors for packages that depend on https://www.npmjs.com/package/supports-color
// but do this only in TTY mode
if (process.stdout.isTTY) {
// istanbul ignore next
process.env.FORCE_COLOR = '1'
}

const pkg = require('../package.json')
require('please-upgrade-node')(
Object.assign({}, pkg, {
engines: {
Expand All @@ -13,6 +20,7 @@ require('please-upgrade-node')(

const cmdline = require('commander')
const debugLib = require('debug')
const lintStaged = require('../src')

const debug = debugLib('lint-staged:bin')

Expand All @@ -30,4 +38,12 @@ if (cmdline.debug) {

debug('Running `lint-staged@%s`', pkg.version)

require('./src')(console, cmdline.config, !!cmdline.shell, !!cmdline.quiet, !!cmdline.debug)
lintStaged(console, cmdline.config, !!cmdline.shell, !!cmdline.quiet, !!cmdline.debug).then(
exitCode => {
process.exitCode = exitCode
},
err => {
console.error(err)
process.exitCode = 1
}
)
9 changes: 6 additions & 3 deletions package.json
Expand Up @@ -10,8 +10,11 @@
"Suhas Karanth <sudo.suhas@gmail.com>",
"Iiro Jäppinen <iiro@jappinen.fi> (https://iiro.fi)"
],
"bin": "index.js",
"files": ["index.js", "src"],
"main": "./src/index.js",
"bin": {
"lint-staged": "./bin/lint-staged"
},
"files": ["src", "bin"],
"scripts": {
"cz": "git-cz",
"lint:base": "eslint --rule \"prettier/prettier: 2\"",
Expand All @@ -23,7 +26,7 @@
},
"husky": {
"hooks": {
"pre-commit": "node index.js"
"pre-commit": "./bin/lint-staged"
}
},
"dependencies": {
Expand Down
26 changes: 12 additions & 14 deletions src/index.js
Expand Up @@ -9,13 +9,6 @@ const validateConfig = require('./validateConfig')

const debug = require('debug')('lint-staged')

// Force colors for packages that depend on https://www.npmjs.com/package/supports-color
// but do this only in TTY mode
if (process.stdout.isTTY) {
// istanbul ignore next
process.env.FORCE_COLOR = '1'
}

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

function resolveConfig(configPath) {
Expand Down Expand Up @@ -43,12 +36,17 @@ function loadConfig(configPath) {
}

/**
* Root lint-staged function that is called from .bin
* @param {Function} logger
* @typedef {(...any) => void} LogFunction
* @typedef {{ error: LogFunction, log: LogFunction }} Logger
*
* Root lint-staged function that is called from `bin/lint-staged`.
*
* @param {Logger} logger
* @param {String} configPath
* @param {Boolean} shellMode Use execa’s shell mode to execute linter commands
* @param {Boolean} quietMode Use Listr’s silent renderer
* @param {Boolean} debugMode Enable debug mode
* @returns {Promise<number>} Promise containing the exit code to use
*/
module.exports = function lintStaged(
logger = console,
Expand Down Expand Up @@ -80,16 +78,14 @@ module.exports = function lintStaged(
return runAll(config, shellMode, quietMode, debugMode)
.then(() => {
debug('linters were executed successfully!')
// No errors, exiting with 0
return Promise.resolve(0)
})
.catch(error => {
// Errors detected, printing and exiting with non-zero
process.exitCode = 1
printErrors(error)
printErrors(error, logger)
return Promise.resolve(1)
})
})
.catch(err => {
process.exitCode = 1
if (err === errConfigNotFound) {
logger.error(`${err.message}.`)
} else {
Expand All @@ -106,5 +102,7 @@ module.exports = function lintStaged(
Please make sure you have created it correctly.
See https://github.com/okonet/lint-staged#configuration.
`)

return Promise.resolve(1)
})
}
12 changes: 6 additions & 6 deletions test/index.spec.js
Expand Up @@ -98,9 +98,9 @@ describe('lintStaged', () => {
it('should print helpful error message when config file is not found', async () => {
expect.assertions(2)
mockCosmiconfigWith(null)
await lintStaged(logger)
const exitCode = await lintStaged(logger)
expect(logger.printHistory()).toMatchSnapshot()
expect(process.exitCode).toEqual(1)
expect(exitCode).toEqual(1)
})

it('should print helpful error message when explicit config file is not found', async () => {
Expand All @@ -115,9 +115,9 @@ describe('lintStaged', () => {
)
)

await lintStaged(logger, nonExistentConfig)
const exitCode = await lintStaged(logger, nonExistentConfig)
expect(logger.printHistory()).toMatchSnapshot()
expect(process.exitCode).toEqual(1)
expect(exitCode).toEqual(1)
})

it('should exit with code 1 on linter errors', async () => {
Expand All @@ -126,8 +126,8 @@ describe('lintStaged', () => {
}
mockCosmiconfigWith({ config })
getStagedFiles.mockImplementationOnce(async () => ['sample.java'])
await lintStaged(logger, undefined)
const exitCode = await lintStaged(logger, undefined)
expect(console.error).toHaveBeenCalledWith(expect.stringContaining('node -e "process.exit(1)"'))
expect(process.exitCode).toEqual(1)
expect(exitCode).toEqual(1)
})
})

0 comments on commit ca37906

Please sign in to comment.