-
-
Notifications
You must be signed in to change notification settings - Fork 933
/
runLifecycleHooksConcurrently.ts
103 lines (99 loc) · 3.73 KB
/
runLifecycleHooksConcurrently.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import fs from 'fs'
import path from 'path'
import { fetchFromDir } from '@pnpm/directory-fetcher'
import { type StoreController } from '@pnpm/store-controller-types'
import { type ProjectManifest } from '@pnpm/types'
import runGroups from 'run-groups'
import { runLifecycleHook, type RunLifecycleHookOptions } from './runLifecycleHook'
export type RunLifecycleHooksConcurrentlyOptions = Omit<RunLifecycleHookOptions,
| 'depPath'
| 'pkgRoot'
| 'rootModulesDir'
> & {
resolveSymlinksInInjectedDirs?: boolean
storeController: StoreController
}
export interface Importer {
buildIndex: number
manifest: ProjectManifest
rootDir: string
modulesDir: string
stages?: string[]
targetDirs?: string[]
}
export async function runLifecycleHooksConcurrently (
stages: string[],
importers: Importer[],
childConcurrency: number,
opts: RunLifecycleHooksConcurrentlyOptions
) {
const importersByBuildIndex = new Map<number, Importer[]>()
for (const importer of importers) {
if (!importersByBuildIndex.has(importer.buildIndex)) {
importersByBuildIndex.set(importer.buildIndex, [importer])
} else {
importersByBuildIndex.get(importer.buildIndex)!.push(importer)
}
}
const sortedBuildIndexes = Array.from(importersByBuildIndex.keys()).sort((a, b) => a - b)
const groups = sortedBuildIndexes.map((buildIndex) => {
const importers = importersByBuildIndex.get(buildIndex)!
return importers.map(({ manifest, modulesDir, rootDir, stages: importerStages, targetDirs }) =>
async () => {
const runLifecycleHookOpts = {
...opts,
depPath: rootDir,
pkgRoot: rootDir,
rootModulesDir: modulesDir,
}
let isBuilt = false
for (const stage of (importerStages ?? stages)) {
if ((manifest.scripts == null) || !manifest.scripts[stage]) continue
await runLifecycleHook(stage, manifest, runLifecycleHookOpts)
isBuilt = true
}
if (targetDirs == null || targetDirs.length === 0 || !isBuilt) return
const filesResponse = await fetchFromDir(rootDir, { resolveSymlinks: opts.resolveSymlinksInInjectedDirs })
await Promise.all(
targetDirs.map(async (targetDir) => {
const targetModulesDir = path.join(targetDir, 'node_modules')
const nodeModulesIndex = {}
if (fs.existsSync(targetModulesDir)) {
// If the target directory contains a node_modules directory
// (it may happen when the hoisted node linker is used)
// then we need to preserve this node_modules.
// So we scan this node_modules directory and pass it as part of the new package.
await scanDir('node_modules', targetModulesDir, targetModulesDir, nodeModulesIndex)
}
return opts.storeController.importPackage(targetDir, {
filesResponse: {
fromStore: false,
...filesResponse,
filesIndex: {
...filesResponse.filesIndex,
...nodeModulesIndex,
},
},
force: false,
})
})
)
}
)
})
await runGroups(childConcurrency, groups)
}
async function scanDir (prefix: string, rootDir: string, currentDir: string, index: Record<string, string>) {
const files = await fs.promises.readdir(currentDir)
await Promise.all(files.map(async (file) => {
const fullPath = path.join(currentDir, file)
const stat = await fs.promises.stat(fullPath)
if (stat.isDirectory()) {
return scanDir(prefix, rootDir, fullPath, index)
}
if (stat.isFile()) {
const relativePath = path.relative(rootDir, fullPath)
index[path.join(prefix, relativePath)] = fullPath
}
}))
}