-
-
Notifications
You must be signed in to change notification settings - Fork 933
/
recursive.ts
executable file
·163 lines (148 loc) · 4.56 KB
/
recursive.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import {
RecursiveSummary,
throwOnCommandFail,
} from '@pnpm/cli-utils'
import {
Config,
readLocalConfig,
} from '@pnpm/config'
import { arrayOfWorkspacePackagesToMap } from '@pnpm/find-workspace-packages'
import { logger } from '@pnpm/logger'
import { sortPackages } from '@pnpm/sort-packages'
import { createOrConnectStoreController, CreateStoreControllerOptions } from '@pnpm/store-connection-manager'
import { Project, ProjectManifest } from '@pnpm/types'
import mem from 'mem'
import pLimit from 'p-limit'
import { rebuildProjects as rebuildAll, RebuildOptions, rebuildSelectedPkgs } from './implementation'
type RecursiveRebuildOpts = CreateStoreControllerOptions & Pick<Config,
| 'hoistPattern'
| 'hooks'
| 'ignorePnpmfile'
| 'ignoreScripts'
| 'lockfileDir'
| 'lockfileOnly'
| 'rawLocalConfig'
| 'registries'
| 'sharedWorkspaceLockfile'
> & {
pending?: boolean
} & Partial<Pick<Config, 'bail' | 'sort' | 'workspaceConcurrency'>>
export async function recursiveRebuild (
allProjects: Project[],
params: string[],
opts: RecursiveRebuildOpts & {
ignoredPackages?: Set<string>
} & Required<Pick<Config, 'selectedProjectsGraph' | 'workspaceDir'>>
) {
if (allProjects.length === 0) {
// It might make sense to throw an exception in this case
return
}
const pkgs = Object.values(opts.selectedProjectsGraph).map((wsPkg) => wsPkg.package)
if (pkgs.length === 0) {
return
}
const manifestsByPath: { [dir: string]: Omit<Project, 'dir'> } = {}
for (const { dir, manifest, writeProjectManifest } of pkgs) {
manifestsByPath[dir] = { manifest, writeProjectManifest }
}
const throwOnFail = throwOnCommandFail.bind(null, 'pnpm recursive rebuild')
const chunks = opts.sort !== false
? sortPackages(opts.selectedProjectsGraph)
: [Object.keys(opts.selectedProjectsGraph).sort()]
const store = await createOrConnectStoreController(opts)
const workspacePackages = arrayOfWorkspacePackagesToMap(allProjects)
const rebuildOpts = Object.assign(opts, {
ownLifecycleHooksStdio: 'pipe',
pruneLockfileImporters: ((opts.ignoredPackages == null) || opts.ignoredPackages.size === 0) &&
pkgs.length === allProjects.length,
storeController: store.ctrl,
storeDir: store.dir,
workspacePackages,
}) as RebuildOptions
const result = {
fails: [],
passes: 0,
} as RecursiveSummary
const memReadLocalConfig = mem(readLocalConfig)
async function getImporters () {
const importers = [] as Array<{ buildIndex: number, manifest: ProjectManifest, rootDir: string }>
await Promise.all(chunks.map(async (prefixes: string[], buildIndex) => {
if (opts.ignoredPackages != null) {
prefixes = prefixes.filter((prefix) => !opts.ignoredPackages!.has(prefix))
}
return Promise.all(
prefixes.map(async (prefix) => {
importers.push({
buildIndex,
manifest: manifestsByPath[prefix].manifest,
rootDir: prefix,
})
})
)
}))
return importers
}
const rebuild = (
params.length === 0
? rebuildAll
: (importers: any, opts: any) => rebuildSelectedPkgs(importers, params, opts) // eslint-disable-line
)
if (opts.lockfileDir) {
const importers = await getImporters()
await rebuild(
importers,
{
...rebuildOpts,
pending: opts.pending === true,
}
)
return
}
const limitRebuild = pLimit(opts.workspaceConcurrency ?? 4)
for (const chunk of chunks) {
await Promise.all(chunk.map(async (rootDir: string) =>
limitRebuild(async () => {
try {
if (opts.ignoredPackages?.has(rootDir)) {
return
}
const localConfig = await memReadLocalConfig(rootDir)
await rebuild(
[
{
buildIndex: 0,
manifest: manifestsByPath[rootDir].manifest,
rootDir,
},
],
{
...rebuildOpts,
...localConfig,
dir: rootDir,
pending: opts.pending === true,
rawConfig: {
...rebuildOpts.rawConfig,
...localConfig,
},
}
)
result.passes++
} catch (err: any) { // eslint-disable-line
logger.info(err)
if (!opts.bail) {
result.fails.push({
error: err,
message: err.message,
prefix: rootDir,
})
return
}
err['prefix'] = rootDir
throw err
}
})
))
}
throwOnFail(result)
}