@@ -4,6 +4,104 @@ import { configurationError } from './messages.js'
44
55const debugLog = createDebug ( 'lint-staged:getSpawnedTasks' )
66
7+ /**
8+ * Get the maximum length of a command-line argument string based on current platform
9+ *
10+ * https://serverfault.com/questions/69430/what-is-the-maximum-length-of-a-command-line-in-mac-os-x
11+ * https://support.microsoft.com/en-us/help/830473/command-prompt-cmd-exe-command-line-string-limitation
12+ * https://unix.stackexchange.com/a/120652
13+ */
14+ export const getMaxArgLength = ( platform = process . platform ) => {
15+ switch ( platform ) {
16+ case 'darwin' :
17+ return 262_144
18+ case 'win32' :
19+ return 8_191
20+ default :
21+ return 131_072
22+ }
23+ }
24+
25+ /**
26+ * Chunk `files` so that when joined with `command`, the resulting string fits into `maxArgLength`.
27+ * For function commands, they are evaluated with the list of files and measured afterwards.
28+ *
29+ * @param {string | Function } command
30+ * @param {string[] } files
31+ * @param {number } [maxArgLength]
32+ * @returns {Promise<Array<{ command: string, files: string[] }>> }
33+ */
34+ export const chunkFilesForCommand = async ( command , files , maxArgLength ) => {
35+ const isFn = typeof command === 'function'
36+ const resolved = isFn ? await command ( [ ...files ] ) : command
37+ const resolvedCommands = Array . isArray ( resolved ) ? resolved : [ resolved ]
38+
39+ if ( isFn && resolvedCommands . some ( ( command ) => typeof command !== 'string' ) ) {
40+ throw new Error (
41+ configurationError (
42+ '[Function]' ,
43+ 'Function task should return a string or an array of strings' ,
44+ resolved
45+ )
46+ )
47+ }
48+
49+ if ( ! maxArgLength || maxArgLength === Infinity || files . length <= 1 ) {
50+ debugLog (
51+ 'Skipping chunking because maxArgLength is %s and there are %s files' ,
52+ maxArgLength ,
53+ files . length
54+ )
55+
56+ return resolvedCommands . map ( ( resolvedCommand ) => ( {
57+ command : resolvedCommand ,
58+ files,
59+ } ) )
60+ }
61+
62+ const fits = resolvedCommands . every ( ( resolvedCommand ) => {
63+ /**
64+ * Really the command is spawned using `tinyexec` and the files passed as an array of
65+ * arguments; but this approximates the length without quoting filenames.
66+ */
67+ const finalCommandString = isFn ? resolvedCommand : resolvedCommand + ' ' + files . join ( ' ' )
68+
69+ debugLog (
70+ 'Resolved an argument string length of %d characters from %d files' ,
71+ finalCommandString . length ,
72+ files . length
73+ )
74+
75+ return finalCommandString . length <= maxArgLength
76+ } )
77+
78+ if ( fits ) {
79+ return resolvedCommands . map ( ( resolvedCommand ) => ( {
80+ command : resolvedCommand ,
81+ files,
82+ } ) )
83+ }
84+
85+ const middle = Math . ceil ( files . length / 2 )
86+ const left = files . slice ( 0 , middle )
87+ const right = files . slice ( middle )
88+
89+ debugLog (
90+ 'Splitting %d files into chunks of %d and %d for maxArgLength of %d' ,
91+ files . length ,
92+ left . length ,
93+ right . length ,
94+ maxArgLength
95+ )
96+
97+ const commands = await Promise . all ( [
98+ chunkFilesForCommand ( command , left , maxArgLength ) ,
99+ chunkFilesForCommand ( command , right , maxArgLength ) ,
100+ ] )
101+
102+ return commands . flat ( )
103+ }
104+
7105/**
8106 * Creates and returns an array of tasks which map to the given commands.
9107 *
@@ -13,6 +111,7 @@ const debugLog = createDebug('lint-staged:getSpawnedTasks')
13111 * @param {string } options.continueOnError
14112 * @param {string } options.cwd
15113 * @param {import('./getStagedFiles.js').StagedFile[] } options.files
114+ * @param {number } [options.maxArgLength]
16115 * @param {string } options.topLevelDir
17116 * @param {Boolean } verbose
18117 */
@@ -22,52 +121,41 @@ export const getSpawnedTasks = async ({
22121 continueOnError,
23122 cwd,
24123 files,
124+ maxArgLength,
25125 topLevelDir,
26126 verbose,
27127} ) => {
28128 debugLog ( 'Creating tasks for commands %o' , commands )
29- const cmdTasks = [ ]
30129
31130 const commandArray = Array . isArray ( commands ) ? commands : [ commands ]
32131
33132 const filepaths = files . map ( ( f ) => f . filepath )
34133
35- for ( const cmd of commandArray ) {
36- // command function may return array of commands that already include `stagedFiles`
37- const isFn = typeof cmd === 'function'
38-
39- /** Pass copy of file list to prevent mutation by function from config file. */
40- const resolved = isFn ? await cmd ( [ ...filepaths ] ) : cmd
41-
42- const resolvedArray = Array . isArray ( resolved ) ? resolved : [ resolved ] // Wrap non-array command as array
43-
44- for ( const command of resolvedArray ) {
45- // If the function linter didn't return string | string[] it won't work
46- // Do the validation here instead of `validateConfig` to skip evaluating the function multiple times
47- if ( isFn && typeof command !== 'string' ) {
48- throw new Error (
49- configurationError (
50- '[Function]' ,
51- 'Function task should return a string or an array of strings' ,
52- resolved
53- )
54- )
55- }
56-
57- const task = getSpawnedTask ( {
58- abortController,
59- command,
60- continueOnError,
61- cwd,
62- files : filepaths ,
63- topLevelDir,
64- isFn,
65- verbose,
66- } )
67-
68- cmdTasks . push ( { title : command , task } )
69- }
134+ if ( filepaths . length === 0 ) {
135+ debugLog ( 'Skipping task generation because no files matched' , { commands } )
136+ return [ ]
70137 }
71138
72- return cmdTasks
139+ const spawnedTasks = await Promise . all (
140+ commandArray . map ( async ( cmd ) => {
141+ const isFn = typeof cmd === 'function'
142+ const chunkedCommands = await chunkFilesForCommand ( cmd , filepaths , maxArgLength )
143+
144+ return chunkedCommands . map ( ( { command, files : chunkedFiles } ) => ( {
145+ title : command ,
146+ task : getSpawnedTask ( {
147+ abortController,
148+ command,
149+ continueOnError,
150+ cwd,
151+ files : chunkedFiles ,
152+ topLevelDir,
153+ isFn,
154+ verbose,
155+ } ) ,
156+ } ) )
157+ } )
158+ )
159+
160+ return spawnedTasks . flat ( )
73161}
0 commit comments