Skip to content

Commit

Permalink
feat: support functions as linter commands
Browse files Browse the repository at this point in the history
Linter functions receive an array of the staged filenames (matching the glob), and return a complete command as a string. This allows customising, or omitting completely, the passing of file arguments to the linter binary
  • Loading branch information
iiroj authored and okonet committed Jul 1, 2019
1 parent 279863d commit f76c0d1
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/resolveTaskFn.js
Expand Up @@ -13,23 +13,20 @@ const findBin = require('./findBin')
const debug = require('debug')('lint-staged:task')

/**
* Execute the given linter binary with arguments and file paths using execa and
* Execute the given linter binary with arguments using execa and
* return the promise.
*
* @param {string} bin
* @param {Array<string>} args
* @param {Object} execaOptions
* @param {Array<string>} pathsToLint
* @return {Promise} child_process
*/
function execLinter(bin, args, execaOptions, pathsToLint) {
const binArgs = args.concat(pathsToLint)

function execLinter(bin, args, execaOptions) {
debug('bin:', bin)
debug('args: %O', binArgs)
debug('args: %O', args)
debug('opts: %o', execaOptions)

return execa(bin, binArgs, { ...execaOptions })
return execa(bin, args, { ...execaOptions })
}

const successMsg = linter => `${symbols.success} ${linter} passed!`
Expand Down Expand Up @@ -93,7 +90,16 @@ function makeErr(linter, result, context = {}) {
*/
module.exports = function resolveTaskFn(options) {
const { linter, gitDir, pathsToLint } = options
const { bin, args } = findBin(linter)

// If cmd` is a function, it should return a string when evaluated with `pathsToLint`.
// Else, it's a already a string
const cmdIsFn = typeof cmd === 'function'
const linterString = cmdIsFn ? linter(pathsToLint) : linter

const { bin, args } = findBin(linterString)

// If `cmd` is a function, args already include `pathsToLint`.
const argsWithPaths = cmdIsFn ? args : args.concat(pathsToLint)

const execaOptions = { reject: false }
// Only use gitDir as CWD if we are using the git binary
Expand All @@ -105,7 +111,7 @@ module.exports = function resolveTaskFn(options) {
if (!isWindows()) {
debug('%s OS: %s; File path chunking unnecessary', symbols.success, process.platform)
return ctx =>
execLinter(bin, args, execaOptions, pathsToLint).then(result => {
execLinter(bin, argsWithPaths, execaOptions).then(result => {
if (result.failed || result.killed || result.signal != null) {
throw makeErr(linter, result, ctx)
}
Expand All @@ -117,7 +123,7 @@ module.exports = function resolveTaskFn(options) {
const { chunkSize, subTaskConcurrency: concurrency } = options

const filePathChunks = chunk(pathsToLint, calcChunkSize(pathsToLint, chunkSize))
const mapper = execLinter.bind(null, bin, args, execaOptions)
const mapper = execLinter.bind(null, bin, argsWithPaths, execaOptions)

debug(
'OS: %s; Creating linter task with %d chunked file paths',
Expand Down

0 comments on commit f76c0d1

Please sign in to comment.