Skip to content

Commit

Permalink
refactor: replace "for...of" with "await Promise.all()"
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Jan 18, 2022
1 parent 1c94c27 commit 3731f10
Showing 1 changed file with 43 additions and 44 deletions.
87 changes: 43 additions & 44 deletions lib/runAll.js
Expand Up @@ -142,52 +142,51 @@ export const runAll = async (
}

for (const [index, files] of stagedFileChunks.entries()) {
const chunkTasks = generateTasks({ config, cwd, files, relative })
const chunkListrTasks = []

for (const task of chunkTasks) {
const subTasks = await makeCmdTasks({
commands: task.commands,
cwd,
files: task.fileList,
gitDir,
renderer: listrOptions.renderer,
shell,
verbose,
})

// Add files from task to match set
task.fileList.forEach((file) => {
matchedFiles.add(file)
})

hasDeprecatedGitAdd =
hasDeprecatedGitAdd || subTasks.some((subTask) => subTask.command === 'git add')

const fileCount = task.fileList.length

chunkListrTasks.push({
title: `${task.pattern}${dim(` β€” ${fileCount} ${fileCount > 1 ? 'files' : 'file'}`)}`,
task: async () =>
new Listr(subTasks, {
// In sub-tasks we don't want to run concurrently
// and we want to abort on errors
...listrOptions,
concurrent: false,
exitOnError: true,
}),
skip: () => {
// Skip task when no files matched
if (fileCount === 0) {
return `${task.pattern}${dim(' β€” no files')}`
}
return false
},
})
}

const relativeConfig = normalize(path.relative(cwd, configPath))

const chunkListrTasks = await Promise.all(
generateTasks({ config, cwd, files, relative }).map((task) =>
makeCmdTasks({
commands: task.commands,
cwd,
files: task.fileList,
gitDir,
renderer: listrOptions.renderer,
shell,
verbose,
}).then((subTasks) => {
// Add files from task to match set
task.fileList.forEach((file) => {
matchedFiles.add(file)
})

hasDeprecatedGitAdd =
hasDeprecatedGitAdd || subTasks.some((subTask) => subTask.command === 'git add')

const fileCount = task.fileList.length

return {
title: `${task.pattern}${dim(` β€” ${fileCount} ${fileCount > 1 ? 'files' : 'file'}`)}`,
task: async () =>
new Listr(subTasks, {
// In sub-tasks we don't want to run concurrently
// and we want to abort on errors
...listrOptions,
concurrent: false,
exitOnError: true,
}),
skip: () => {
// Skip task when no files matched
if (fileCount === 0) {
return `${task.pattern}${dim(' β€” no files')}`
}
return false
},
}
})
)
)

listrTasks.push({
title:
`${relativeConfig}${dim(` β€” ${files.length} ${files.length > 1 ? 'files' : 'file'}`)}` +
Expand Down

0 comments on commit 3731f10

Please sign in to comment.