|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const chunk = require('lodash/chunk') |
| 4 | +const dedent = require('dedent') |
| 5 | +const isWindows = require('is-windows') |
| 6 | +const execa = require('execa') |
| 7 | +const symbols = require('log-symbols') |
| 8 | +const pMap = require('p-map') |
| 9 | +const calcChunkSize = require('./calcChunkSize') |
| 10 | +const findBin = require('./findBin') |
| 11 | + |
| 12 | +const debug = require('debug')('lint-staged:task') |
| 13 | + |
| 14 | +/** |
| 15 | + * Execute the given linter binary with arguments and file paths using execa and |
| 16 | + * return the promise. |
| 17 | + * |
| 18 | + * @param {string} bin |
| 19 | + * @param {Array<string>} args |
| 20 | + * @param {Object} execaOptions |
| 21 | + * @param {Array<string>} pathsToLint |
| 22 | + * @return {Promise} |
| 23 | + */ |
| 24 | +function execLinter(bin, args, execaOptions, pathsToLint) { |
| 25 | + const binArgs = args.concat(pathsToLint) |
| 26 | + |
| 27 | + debug('bin:', bin) |
| 28 | + debug('args: %O', binArgs) |
| 29 | + debug('opts: %o', execaOptions) |
| 30 | + |
| 31 | + return execa(bin, binArgs, Object.assign({}, execaOptions)) |
| 32 | +} |
| 33 | + |
| 34 | +const successMsg = linter => `${symbols.success} ${linter} passed!` |
| 35 | + |
| 36 | +/** |
| 37 | + * Create and returns an error instance with given stdout and stderr. If we set |
| 38 | + * the message on the error instance, it gets logged multiple times(see #142). |
| 39 | + * So we set the actual error message in a private field and extract it later, |
| 40 | + * log only once. |
| 41 | + * |
| 42 | + * @param {string} linter |
| 43 | + * @param {string} errStdout |
| 44 | + * @param {string} errStderr |
| 45 | + * @returns {Error} |
| 46 | + */ |
| 47 | +function makeErr(linter, errStdout, errStderr) { |
| 48 | + const err = new Error() |
| 49 | + err.privateMsg = dedent` |
| 50 | + ${symbols.error} "${linter}" found some errors. Please fix them and try committing again. |
| 51 | + ${errStdout} |
| 52 | + ${errStderr} |
| 53 | + ` |
| 54 | + return err |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Returns the task function for the linter. It handles chunking for file paths |
| 59 | + * if the OS is Windows. |
| 60 | + * |
| 61 | + * @param {Object} options |
| 62 | + * @param {string} options.linter |
| 63 | + * @param {string} options.gitDir |
| 64 | + * @param {Array<string>} options.pathsToLint |
| 65 | + * @param {number} options.chunkSize |
| 66 | + * @param {number} options.subTaskConcurrency |
| 67 | + * @returns {function(): Promise<string>} |
| 68 | + */ |
| 69 | +module.exports = function resolveTaskFn(options) { |
| 70 | + const { linter, gitDir, pathsToLint } = options |
| 71 | + const { bin, args } = findBin(linter) |
| 72 | + |
| 73 | + const execaOptions = { reject: false } |
| 74 | + // Only use gitDir as CWD if we are using the git binary |
| 75 | + // e.g `npm` should run tasks in the actual CWD |
| 76 | + if (/git(\.exe)?$/i.test(bin) && gitDir !== process.cwd()) { |
| 77 | + execaOptions.cwd = gitDir |
| 78 | + } |
| 79 | + |
| 80 | + if (!isWindows()) { |
| 81 | + debug('%s OS: %s; File path chunking unnecessary', symbols.success, process.platform) |
| 82 | + return () => |
| 83 | + execLinter(bin, args, execaOptions, pathsToLint).then(result => { |
| 84 | + if (!result.failed) return successMsg(linter) |
| 85 | + |
| 86 | + throw makeErr(linter, result.stdout, result.stderr) |
| 87 | + }) |
| 88 | + } |
| 89 | + |
| 90 | + const { chunkSize, subTaskConcurrency: concurrency } = options |
| 91 | + |
| 92 | + const filePathChunks = chunk(pathsToLint, calcChunkSize(pathsToLint, chunkSize)) |
| 93 | + const mapper = execLinter.bind(null, bin, args, execaOptions) |
| 94 | + |
| 95 | + debug( |
| 96 | + 'OS: %s; Creating linter task with %d chunked file paths', |
| 97 | + process.platform, |
| 98 | + filePathChunks.length |
| 99 | + ) |
| 100 | + return () => |
| 101 | + pMap(filePathChunks, mapper, { concurrency }) |
| 102 | + .catch(err => { |
| 103 | + /* This will probably never be called. But just in case.. */ |
| 104 | + throw new Error(dedent` |
| 105 | + ${symbols.error} ${linter} got an unexpected error. |
| 106 | + ${err.message} |
| 107 | + `) |
| 108 | + }) |
| 109 | + .then(results => { |
| 110 | + const errors = results.filter(res => res.failed) |
| 111 | + if (errors.length === 0) return successMsg(linter) |
| 112 | + |
| 113 | + const errStdout = errors.map(err => err.stdout).join('') |
| 114 | + const errStderr = errors.map(err => err.stderr).join('') |
| 115 | + |
| 116 | + throw makeErr(linter, errStdout, errStderr) |
| 117 | + }) |
| 118 | +} |
0 commit comments