From ca379068de27fb838270e7a4772f83258d613acc Mon Sep 17 00:00:00 2001 From: Cameron Hunter Date: Thu, 4 Jul 2019 15:32:57 -0700 Subject: [PATCH] feat: make node-api accessible --- index.js => bin/lint-staged | 20 ++++++++++++++++++-- package.json | 9 ++++++--- src/index.js | 26 ++++++++++++-------------- test/index.spec.js | 12 ++++++------ 4 files changed, 42 insertions(+), 25 deletions(-) rename index.js => bin/lint-staged (57%) diff --git a/index.js b/bin/lint-staged similarity index 57% rename from index.js rename to bin/lint-staged index 69d8c1224..8a5f9c751 100755 --- a/index.js +++ b/bin/lint-staged @@ -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: { @@ -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') @@ -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 + } +) diff --git a/package.json b/package.json index b534fd083..01509840c 100644 --- a/package.json +++ b/package.json @@ -10,8 +10,11 @@ "Suhas Karanth ", "Iiro Jäppinen (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\"", @@ -23,7 +26,7 @@ }, "husky": { "hooks": { - "pre-commit": "node index.js" + "pre-commit": "./bin/lint-staged" } }, "dependencies": { diff --git a/src/index.js b/src/index.js index d4d7aa16e..4d01fa1b5 100644 --- a/src/index.js +++ b/src/index.js @@ -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) { @@ -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} Promise containing the exit code to use */ module.exports = function lintStaged( logger = console, @@ -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 { @@ -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) }) } diff --git a/test/index.spec.js b/test/index.spec.js index 0cebe25f0..be0163208 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -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 () => { @@ -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 () => { @@ -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) }) })