(allFiles: string[], action: FormatterAction)
| 42 | * |
| 43 | * A promise is returned, completed when the command has completed running for each file. |
| 44 | * The promise resolves with a list of failures, or `false` if no formatters have matched. |
| 45 | */ |
| 46 | export function runFormatterInParallel(allFiles: string[], action: FormatterAction) { |
| 47 | return new Promise<false | FormatFailure[]>(async (resolve) => { |
| 48 | const formatters = await getActiveFormatters(); |
| 49 | const failures: FormatFailure[] = []; |
| 50 | const pendingCommands: {formatter: Formatter; file: string}[] = []; |
| 51 | |
| 52 | for (const formatter of formatters) { |
| 53 | pendingCommands.push( |
| 54 | ...multimatch |
| 55 | .call(undefined, allFiles, formatter.getFileMatcher(), {dot: true}) |
| 56 | .map((file) => ({formatter, file})), |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | // If no commands are generated, resolve the promise as `false` as no files |
| 61 | // were run against the any formatters. |
| 62 | if (pendingCommands.length === 0) { |
| 63 | return resolve(false); |
| 64 | } |
| 65 | |
| 66 | switch (action) { |
| 67 | case 'format': |
| 68 | Log.info(`Formatting ${pendingCommands.length} file(s)`); |
| 69 | break; |
| 70 | case 'check': |
| 71 | Log.info(`Checking format of ${pendingCommands.length} file(s)`); |
| 72 | break; |
| 73 | default: |
| 74 | throw Error(`Invalid format action "${action}": allowed actions are "format" and "check"`); |
| 75 | } |
| 76 | |
| 77 | // The progress bar instance to use for progress tracking. |
| 78 | const progressBar = new Bar({ |
| 79 | format: `[{bar}] ETA: {eta}s | {value}/{total} files`, |
| 80 | clearOnComplete: true, |
| 81 | }); |
| 82 | // A local copy of the files to run the command on. |
| 83 | // An array to represent the current usage state of each of the threads for parallelization. |
| 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 | } |
no test coverage detected