1 | import path from 'path'
|
2 |
|
3 | import debug from 'debug'
|
4 | import normalize from 'normalize-path'
|
5 |
|
6 | const debugLog = debug('lint-staged:chunkFiles')
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | const chunkArray = (arr, chunkCount) => {
|
15 | if (chunkCount === 1) return [arr]
|
16 | const chunked = []
|
17 | let position = 0
|
18 | for (let i = 0; i < chunkCount; i++) {
|
19 | const chunkLength = Math.ceil((arr.length - position) / (chunkCount - i))
|
20 | chunked.push([])
|
21 | chunked[i] = arr.slice(position, chunkLength + position)
|
22 | position += chunkLength
|
23 | }
|
24 | return chunked
|
25 | }
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | export const chunkFiles = ({ files, baseDir, maxArgLength = null, relative = false }) => {
|
37 | const normalizedFiles = files.map((file) =>
|
38 | normalize(relative || !baseDir ? file : path.resolve(baseDir, file))
|
39 | )
|
40 |
|
41 | if (!maxArgLength) {
|
42 | debugLog('Skip chunking files because of undefined maxArgLength')
|
43 | return [normalizedFiles]
|
44 | }
|
45 |
|
46 | const fileListLength = normalizedFiles.join(' ').length
|
47 | debugLog(
|
48 | `Resolved an argument string length of ${fileListLength} characters from ${normalizedFiles.length} files`
|
49 | )
|
50 | const chunkCount = Math.min(Math.ceil(fileListLength / maxArgLength), normalizedFiles.length)
|
51 | debugLog(`Creating ${chunkCount} chunks for maxArgLength of ${maxArgLength}`)
|
52 | return chunkArray(normalizedFiles, chunkCount)
|
53 | }
|