forked from lint-staged/lint-staged
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runScript.js
68 lines (54 loc) Β· 2.4 KB
/
runScript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'use strict'
const chunk = require('lodash.chunk')
const execa = require('execa')
const pMap = require('p-map')
const calcChunkSize = require('./calcChunkSize')
const findBin = require('./findBin')
const readConfigOption = require('./readConfigOption')
module.exports = function runScript(commands, pathsToLint, packageJson, options) {
const config = readConfigOption(options, 'config', {})
const concurrency = readConfigOption(config, 'subTaskConcurrency', 2)
const chunkSize = calcChunkSize(
pathsToLint,
readConfigOption(config, 'chunkSize', Number.MAX_SAFE_INTEGER)
)
const filePathChunks = chunk(pathsToLint, chunkSize)
const lintersArray = Array.isArray(commands) ? commands : [commands]
return lintersArray.map(linter => ({
title: linter,
task: () => {
try {
const res = findBin(linter, packageJson, options)
const execaOptions =
res.bin !== 'npm' && options && options.gitDir
? { cwd: options.gitDir } : {}
const errors = []
const mapper = (pathsChunk) => {
const args = res.args.concat(['--'], pathsChunk)
return execa(res.bin, args, Object.assign({}, execaOptions))
/* If we don't catch, pMap will terminate on first rejection */
/* We want error information of all chunks */
.catch((err) => {
errors.push(err)
})
}
return pMap(filePathChunks, mapper, { concurrency })
.catch((err) => {
/* This will probably never be called. But just in case.. */
throw new Error(`π« ${ linter } got an unexpected error.
${ err.message }`)
})
.then(() => {
if (errors.length === 0) return `β
${ linter } passed!`
const errStdout = errors.map(err => err.stdout).join('')
const errStderr = errors.map(err => err.stderr).join('')
throw new Error(`π« ${ linter } found some errors. Please fix them and try committing again.
${ errStdout }
${ errStderr }`)
})
} catch (err) {
throw err
}
}
}))
}