Skip to content

Commit

Permalink
perf: don't use await inside loops
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed Jun 2, 2023
1 parent 6cb5433 commit fa5cd65
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
6 changes: 3 additions & 3 deletions releasing/plugin-commands-publishing/src/pack.ts
Expand Up @@ -153,17 +153,17 @@ async function packPkg (opts: {
]
const mtime = new Date('1985-10-26T08:15:00.000Z')
const pack = tar.pack()
for (const [name, source] of Object.entries(filesMap)) {
await Promise.all(Object.entries(filesMap).map(async ([name, source]) => {
const isExecutable = bins.some((bin) => path.relative(bin, source) === '')
const mode = isExecutable ? 0o755 : 0o644
if (/^package\/package\.(json|json5|yaml)/.test(name)) {
const readmeFile = embedReadme ? await readReadmeFile(filesMap) : undefined
const publishManifest = await createExportableManifest(projectDir, manifest, { readmeFile, modulesDir: opts.modulesDir })
pack.entry({ mode, mtime, name: 'package/package.json' }, JSON.stringify(publishManifest, null, 2))
continue
return
}
pack.entry({ mode, mtime, name }, fs.readFileSync(source))
}
}))
const tarball = fs.createWriteStream(destFile)
pack.pipe(createGzip({ level: opts.packGzipLevel })).pipe(tarball)
pack.finalize()
Expand Down
2 changes: 1 addition & 1 deletion releasing/plugin-commands-publishing/src/publish.ts
Expand Up @@ -276,6 +276,6 @@ export async function runScriptsIfPresent (
) {
for (const scriptName of scriptNames) {
if (!manifest.scripts?.[scriptName]) continue
await runLifecycleHook(scriptName, manifest, opts)
await runLifecycleHook(scriptName, manifest, opts) // eslint-disable-line no-await-in-loop
}
}
16 changes: 10 additions & 6 deletions releasing/plugin-commands-publishing/src/recursivePublish.ts
Expand Up @@ -81,14 +81,14 @@ export async function recursivePublish (
}, pkg.manifest.name, pkg.manifest.version))
})
const publishedPkgDirs = new Set(pkgsToPublish.map(({ dir }) => dir))
const publishedPackages = []
const publishedPackages: Array<{ name?: string, version?: string }> = []
if (publishedPkgDirs.size === 0) {
logger.info({
message: 'There are no new packages that should be published',
prefix: opts.dir,
})
} else {
const appendedArgs = []
const appendedArgs: string[] = []
if (opts.cliOptions['access']) {
appendedArgs.push(`--access=${opts.cliOptions['access'] as string}`)
}
Expand All @@ -101,8 +101,9 @@ export async function recursivePublish (
const chunks = sortPackages(opts.selectedProjectsGraph)
const tag = opts.tag ?? 'latest'
for (const chunk of chunks) {
for (const pkgDir of chunk) {
if (!publishedPkgDirs.has(pkgDir)) continue
// eslint-disable-next-line no-await-in-loop
const publishResults = await Promise.all(chunk.map(async (pkgDir) => {
if (!publishedPkgDirs.has(pkgDir)) return null
const pkg = opts.selectedProjectsGraph[pkgDir].package
const publishResult = await publish({
...opts,
Expand All @@ -122,9 +123,12 @@ export async function recursivePublish (
}, [pkg.dir])
if (publishResult?.manifest != null) {
publishedPackages.push(pick(['name', 'version'], publishResult.manifest))
} else if (publishResult?.exitCode) {
return { exitCode: publishResult.exitCode }
}
return publishResult
}))
const failedPublish = publishResults.find((result) => result?.exitCode)
if (failedPublish) {
return { exitCode: failedPublish.exitCode! }
}
}
}
Expand Down

0 comments on commit fa5cd65

Please sign in to comment.