(thread: number)
| 84 | const threads = new Array<boolean>(AVAILABLE_THREADS).fill(false); |
| 85 | |
| 86 | // Recursively run the command on the next available file from the list using the provided |
| 87 | // thread. |
| 88 | function runCommandInThread(thread: number) { |
| 89 | const nextCommand = pendingCommands.pop(); |
| 90 | // If no file was pulled from the array, return as there are no more files to run against. |
| 91 | if (nextCommand === undefined) { |
| 92 | threads[thread] = false; |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | // Get the file and formatter for the next command. |
| 97 | const {file, formatter} = nextCommand; |
| 98 | |
| 99 | if (lstatSync(file).isSymbolicLink()) { |
| 100 | throw new Error(`Security violation: symlink detected for file ${file}`); |
| 101 | } |
| 102 | |
| 103 | const [spawnCmd, ...spawnArgs] = [...formatter.commandFor(action), '--', file]; |
| 104 | |
| 105 | ChildProcess.spawn(spawnCmd, spawnArgs, { |
| 106 | suppressErrorOnFailingExitCode: true, |
| 107 | mode: 'silent', |
| 108 | }).then(async ({stdout, stderr, status}: SpawnResult) => { |
| 109 | // Run the provided callback function. |
| 110 | const failed = formatter.callbackFor(action)(file, status, stdout, stderr); |
| 111 | if (failed) { |
| 112 | const line = await formatter.getFailureLine(file, action, status, stdout, stderr); |
| 113 | failures.push({filePath: file, message: stderr, line}); |
| 114 | } |
| 115 | // Note in the progress bar another file being completed. |
| 116 | progressBar.increment(1); |
| 117 | // If more files exist in the list, run again to work on the next file, |
| 118 | // using the same slot. |
| 119 | if (pendingCommands.length) { |
| 120 | return runCommandInThread(thread); |
| 121 | } |
| 122 | // If not more files are available, mark the thread as unused. |
| 123 | threads[thread] = false; |
| 124 | // If all of the threads are false, as they are unused, mark the progress bar |
| 125 | // completed and resolve the promise. |
| 126 | if (threads.every((active) => !active)) { |
| 127 | progressBar.stop(); |
| 128 | resolve(failures); |
| 129 | } |
| 130 | }); |
| 131 | // Mark the thread as in use as the command execution has been started. |
| 132 | threads[thread] = true; |
| 133 | } |
no test coverage detected