From a60e7fee3043cef97ad97d088609dc3da59df623 Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Mon, 23 Oct 2023 19:16:35 +0300 Subject: [PATCH] refactor(hoist): don't use async functions when they are not needed --- pkg-manager/hoist/src/index.ts | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/pkg-manager/hoist/src/index.ts b/pkg-manager/hoist/src/index.ts index 59c4fa89d3..1a810960ed 100644 --- a/pkg-manager/hoist/src/index.ts +++ b/pkg-manager/hoist/src/index.ts @@ -51,12 +51,12 @@ export async function hoist ( depPath: '', depth: -1, }, - ...await getDependencies(step, 0), + ...getDependencies(0, step), ] const getAliasHoistType = createGetAliasHoistType(opts.publicHoistPattern, opts.privateHoistPattern) - const { hoistedDependencies, hoistedAliasesWithBins } = await hoistGraph(deps, opts.lockfile.importers['.']?.specifiers ?? {}, { + const { hoistedDependencies, hoistedAliasesWithBins } = hoistGraph(deps, opts.lockfile.importers['.']?.specifiers ?? {}, { getAliasHoistType, lockfile: opts.lockfile, }) @@ -125,10 +125,10 @@ async function linkAllBins (modulesDir: string, opts: LinkAllBinsOptions) { } } -async function getDependencies ( - step: LockfileWalkerStep, - depth: number -): Promise { +function getDependencies ( + depth: number, + step: LockfileWalkerStep +): Dependency[] { const deps: Dependency[] = [] const nextSteps: LockfileWalkerStep[] = [] for (const { pkgSnapshot, depPath, next } of step.dependencies) { @@ -151,11 +151,10 @@ async function getDependencies ( logger.debug({ message: `No entry for "${depPath}" in ${WANTED_LOCKFILE}` }) } - return ( - await Promise.all( - nextSteps.map(async (nextStep) => getDependencies(nextStep, depth + 1)) - ) - ).reduce((acc, deps) => [...acc, ...deps], deps) + return [ + ...deps, + ...nextSteps.flatMap(getDependencies.bind(null, depth + 1)), + ] } export interface Dependency { @@ -169,14 +168,14 @@ interface HoistGraphResult { hoistedAliasesWithBins: string[] } -async function hoistGraph ( +function hoistGraph ( depNodes: Dependency[], currentSpecifiers: Record, opts: { getAliasHoistType: GetAliasHoistType lockfile: Lockfile } -): Promise { +): HoistGraphResult { const hoistedAliases = new Set(Object.keys(currentSpecifiers)) const hoistedDependencies: HoistedDependencies = {} const hoistedAliasesWithBins = new Set()