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