@teambit/install 1.0.108 → 1.0.109

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1113 +0,0 @@
1
- import fs, { pathExists } from 'fs-extra';
2
- import path from 'path';
3
- import { getRootComponentDir, getBitRootsDir, linkPkgsToBitRoots } from '@teambit/bit-roots';
4
- import { CompilerMain, CompilerAspect, CompilationInitiator } from '@teambit/compiler';
5
- import { CLIMain, CommandList, CLIAspect, MainRuntime } from '@teambit/cli';
6
- import chalk from 'chalk';
7
- import { WorkspaceAspect, Workspace, ComponentConfigFile } from '@teambit/workspace';
8
- import { compact, mapValues, omit, uniq, intersection } from 'lodash';
9
- import { ProjectManifest } from '@pnpm/types';
10
- import { GenerateResult, GeneratorAspect, GeneratorMain } from '@teambit/generator';
11
- import componentIdToPackageName from '@teambit/legacy/dist/utils/bit/component-id-to-package-name';
12
- import { ApplicationMain, ApplicationAspect } from '@teambit/application';
13
- import { VariantsMain, Patterns, VariantsAspect } from '@teambit/variants';
14
- import { Component, ComponentID, ComponentMap } from '@teambit/component';
15
- import { createLinks } from '@teambit/dependencies.fs.linked-dependencies';
16
- import pMapSeries from 'p-map-series';
17
- import { Slot, SlotRegistry } from '@teambit/harmony';
18
- import { linkToNodeModulesWithCodemod, NodeModulesLinksResult } from '@teambit/workspace.modules.node-modules-linker';
19
- import { EnvsMain, EnvsAspect } from '@teambit/envs';
20
- import IpcEventsAspect, { IpcEventsMain } from '@teambit/ipc-events';
21
- import { IssuesClasses } from '@teambit/component-issues';
22
- import {
23
- GetComponentManifestsOptions,
24
- WorkspaceDependencyLifecycleType,
25
- DependencyResolverMain,
26
- DependencyInstaller,
27
- DependencyResolverAspect,
28
- PackageManagerInstallOptions,
29
- ComponentDependency,
30
- VariantPolicyConfigObject,
31
- WorkspacePolicyEntry,
32
- LinkingOptions,
33
- LinkResults,
34
- DependencyList,
35
- MergedOutdatedPkg,
36
- WorkspacePolicy,
37
- UpdatedComponent,
38
- } from '@teambit/dependency-resolver';
39
- import WorkspaceConfigFilesAspect, { WorkspaceConfigFilesMain } from '@teambit/workspace-config-files';
40
- import { Logger, LoggerAspect, LoggerMain } from '@teambit/logger';
41
- import { IssuesAspect, IssuesMain } from '@teambit/issues';
42
- import { CodemodResult } from '@teambit/legacy/dist/consumer/component-ops/codemod-components';
43
- import { snapToSemver } from '@teambit/component-package-version';
44
- import hash from 'object-hash';
45
- import { DependencyTypeNotSupportedInPolicy } from './exceptions';
46
- import { InstallAspect } from './install.aspect';
47
- import { pickOutdatedPkgs } from './pick-outdated-pkgs';
48
- import { LinkCommand } from './link';
49
- import InstallCmd from './install.cmd';
50
- import UninstallCmd from './uninstall.cmd';
51
- import UpdateCmd from './update.cmd';
52
-
53
- export type WorkspaceLinkOptions = LinkingOptions & {
54
- rootPolicy?: WorkspacePolicy;
55
- linkToBitRoots?: boolean;
56
- includePeers?: boolean;
57
- };
58
-
59
- export type WorkspaceLinkResults = {
60
- legacyLinkResults?: NodeModulesLinksResult[];
61
- legacyLinkCodemodResults?: CodemodResult[];
62
- } & LinkResults;
63
-
64
- export type WorkspaceInstallOptions = {
65
- addMissingDeps?: boolean;
66
- addMissingPeers?: boolean;
67
- lifecycleType?: WorkspaceDependencyLifecycleType;
68
- dedupe?: boolean;
69
- import?: boolean;
70
- copyPeerToRuntimeOnRoot?: boolean;
71
- copyPeerToRuntimeOnComponents?: boolean;
72
- updateExisting?: boolean;
73
- skipIfExisting?: boolean;
74
- savePrefix?: string;
75
- compile?: boolean;
76
- includeOptionalDeps?: boolean;
77
- updateAll?: boolean;
78
- recurringInstall?: boolean;
79
- optimizeReportForNonTerminal?: boolean;
80
- lockfileOnly?: boolean;
81
- writeConfigFiles?: boolean;
82
- };
83
-
84
- export type ModulesInstallOptions = Omit<WorkspaceInstallOptions, 'updateExisting' | 'lifecycleType' | 'import'>;
85
-
86
- type PreLink = (linkOpts?: WorkspaceLinkOptions) => Promise<void>;
87
- type PreInstall = (installOpts?: WorkspaceInstallOptions) => Promise<void>;
88
- type PostInstall = () => Promise<void>;
89
-
90
- type PreLinkSlot = SlotRegistry<PreLink>;
91
- type PreInstallSlot = SlotRegistry<PreInstall>;
92
- type PostInstallSlot = SlotRegistry<PostInstall>;
93
-
94
- type GetComponentsAndManifestsOptions = Omit<
95
- GetComponentManifestsOptions,
96
- 'componentDirectoryMap' | 'rootPolicy' | 'rootDir'
97
- > &
98
- Pick<PackageManagerInstallOptions, 'nodeLinker'>;
99
-
100
- export class InstallMain {
101
- private visitedAspects: Set<string> = new Set();
102
-
103
- constructor(
104
- private dependencyResolver: DependencyResolverMain,
105
-
106
- private logger: Logger,
107
-
108
- private workspace: Workspace,
109
-
110
- private variants: VariantsMain,
111
-
112
- private compiler: CompilerMain,
113
-
114
- private envs: EnvsMain,
115
-
116
- private wsConfigFiles: WorkspaceConfigFilesMain,
117
-
118
- private app: ApplicationMain,
119
-
120
- private generator: GeneratorMain,
121
-
122
- private preLinkSlot: PreLinkSlot,
123
-
124
- private preInstallSlot: PreInstallSlot,
125
-
126
- private postInstallSlot: PostInstallSlot,
127
-
128
- private ipcEvents: IpcEventsMain
129
- ) {}
130
- /**
131
- * Install dependencies for all components in the workspace
132
- *
133
- * @returns
134
- * @memberof Workspace
135
- */
136
- async install(packages?: string[], options?: WorkspaceInstallOptions): Promise<ComponentMap<string>> {
137
- // set workspace in install context
138
- this.workspace.inInstallContext = true;
139
- if (packages && packages.length) {
140
- await this._addPackages(packages, options);
141
- }
142
- if (options?.addMissingPeers) {
143
- const compDirMap = await this.getComponentsDirectory([]);
144
- const mergedRootPolicy = this.dependencyResolver.getWorkspacePolicy();
145
- const depsFilterFn = await this.generateFilterFnForDepsFromLocalRemote();
146
- const pmInstallOptions: PackageManagerInstallOptions = {
147
- dedupe: options?.dedupe,
148
- copyPeerToRuntimeOnRoot: options?.copyPeerToRuntimeOnRoot ?? true,
149
- copyPeerToRuntimeOnComponents: options?.copyPeerToRuntimeOnComponents ?? false,
150
- dependencyFilterFn: depsFilterFn,
151
- overrides: this.dependencyResolver.config.overrides,
152
- packageImportMethod: this.dependencyResolver.config.packageImportMethod,
153
- };
154
- const missingPeers = await this.dependencyResolver.getMissingPeerDependencies(
155
- this.workspace.path,
156
- mergedRootPolicy,
157
- compDirMap,
158
- pmInstallOptions
159
- );
160
- if (missingPeers) {
161
- const missingPeerPackages = Object.entries(missingPeers).map(([peerName, range]) => `${peerName}@${range}`);
162
- await this._addPackages(missingPeerPackages, options);
163
- } else {
164
- this.logger.console('No missing peer dependencies found.');
165
- }
166
- }
167
- await pMapSeries(this.preInstallSlot.values(), (fn) => fn(options)); // import objects if not disabled in options
168
- const res = await this._installModules(options);
169
- this.workspace.inInstallContext = false;
170
-
171
- await this.ipcEvents.publishIpcEvent('onPostInstall');
172
-
173
- return res;
174
- }
175
-
176
- registerPreLink(fn: PreLink) {
177
- this.preLinkSlot.register(fn);
178
- }
179
-
180
- registerPreInstall(fn: PreInstall) {
181
- this.preInstallSlot.register(fn);
182
- }
183
-
184
- registerPostInstall(fn: PostInstall) {
185
- this.postInstallSlot.register(fn);
186
- }
187
-
188
- async onComponentCreate(generateResults: GenerateResult[]) {
189
- this.workspace.inInstallContext = true;
190
- let runInstall = false;
191
- let packages: string[] = [];
192
- let installMissing = false;
193
-
194
- const ids = generateResults.map((generateResult) => {
195
- if (generateResult.dependencies && generateResult.dependencies.length) {
196
- packages = packages.concat(generateResult.dependencies);
197
- runInstall = true;
198
- }
199
- if (generateResult.installMissingDependencies) {
200
- installMissing = true;
201
- runInstall = true;
202
- }
203
- if (generateResult.isApp || generateResult.isEnv) {
204
- runInstall = true;
205
- }
206
- return generateResult.id;
207
- });
208
- const nonLoadedEnvs: string[] = [];
209
-
210
- ids.map((id) => this.workspace.clearComponentCache(id));
211
- await pMapSeries(ids, async (id) => {
212
- const component = await this.workspace.get(id);
213
- // const envId = await this.envs.getEnvId(component);
214
- const envId = (await this.envs.calculateEnvId(component)).toString();
215
- const isLoaded = this.envs.isEnvRegistered(envId);
216
- if (!isLoaded) {
217
- nonLoadedEnvs.push(envId);
218
- }
219
- return component;
220
- });
221
- if (nonLoadedEnvs.length) {
222
- runInstall = true;
223
- }
224
- if (!runInstall) {
225
- this.workspace.inInstallContext = false;
226
- return;
227
- }
228
- // this.logger.console(
229
- // `the following environments are not installed yet: ${nonLoadedEnvs.join(', ')}. installing them now...`
230
- // );
231
- await this.install(packages, { addMissingDeps: installMissing, skipIfExisting: true });
232
- }
233
-
234
- private async _addPackages(packages: string[], options?: WorkspaceInstallOptions) {
235
- if ((options?.lifecycleType as string) === 'dev') {
236
- throw new DependencyTypeNotSupportedInPolicy(options?.lifecycleType as string);
237
- }
238
- this.logger.debug(`installing the following packages: ${packages.join()}`);
239
- const resolver = await this.dependencyResolver.getVersionResolver();
240
- const resolvedPackagesP = packages.map((packageName) =>
241
- resolver.resolveRemoteVersion(packageName, {
242
- rootDir: this.workspace.path,
243
- })
244
- );
245
- const resolvedPackages = await Promise.all(resolvedPackagesP);
246
- const newWorkspacePolicyEntries: WorkspacePolicyEntry[] = [];
247
- resolvedPackages.forEach((resolvedPackage) => {
248
- if (resolvedPackage.version) {
249
- const versionWithPrefix = this.dependencyResolver.getVersionWithSavePrefix({
250
- version: resolvedPackage.version,
251
- overridePrefix: options?.savePrefix,
252
- wantedRange: resolvedPackage.wantedRange,
253
- });
254
- newWorkspacePolicyEntries.push({
255
- dependencyId: resolvedPackage.packageName,
256
- value: {
257
- version: versionWithPrefix,
258
- },
259
- lifecycleType: options?.lifecycleType || 'runtime',
260
- });
261
- }
262
- });
263
- this.dependencyResolver.addToRootPolicy(newWorkspacePolicyEntries, {
264
- updateExisting: options?.updateExisting ?? false,
265
- skipIfExisting: options?.skipIfExisting ?? false,
266
- });
267
- await this.dependencyResolver.persistConfig(this.workspace.path);
268
- }
269
-
270
- private async _installModules(options?: ModulesInstallOptions): Promise<ComponentMap<string>> {
271
- const pm = this.dependencyResolver.getPackageManager();
272
- this.logger.console(
273
- `installing dependencies in workspace using ${pm?.name} (${chalk.cyan(
274
- this.dependencyResolver.getPackageManagerName()
275
- )})`
276
- );
277
- this.logger.debug(`installing dependencies in workspace with options`, options);
278
- const depsFilterFn = await this.generateFilterFnForDepsFromLocalRemote();
279
- const hasRootComponents = this.dependencyResolver.hasRootComponents();
280
- // TODO: pass get install options
281
- const installer = this.dependencyResolver.getInstaller({});
282
- const calcManifestsOpts: GetComponentsAndManifestsOptions = {
283
- copyPeerToRuntimeOnComponents: options?.copyPeerToRuntimeOnComponents ?? false,
284
- copyPeerToRuntimeOnRoot: options?.copyPeerToRuntimeOnRoot ?? true,
285
- dedupe: !hasRootComponents && options?.dedupe,
286
- dependencyFilterFn: depsFilterFn,
287
- nodeLinker: this.dependencyResolver.nodeLinker(),
288
- };
289
- const linkOpts = {
290
- linkTeambitBit: true,
291
- linkCoreAspects: this.dependencyResolver.linkCoreAspects(),
292
- linkDepsResolvedFromEnv: !hasRootComponents,
293
- linkNestedDepsInNM: !this.workspace.isLegacy && !hasRootComponents,
294
- };
295
- const { linkedRootDeps } = await this.calculateLinks(linkOpts);
296
- // eslint-disable-next-line prefer-const
297
- let { mergedRootPolicy, componentsAndManifests: current } = await this._getComponentsManifestsAndRootPolicy(
298
- installer,
299
- {
300
- ...calcManifestsOpts,
301
- addMissingDeps: options?.addMissingDeps,
302
- linkedRootDeps,
303
- }
304
- );
305
-
306
- const pmInstallOptions: PackageManagerInstallOptions = {
307
- ...calcManifestsOpts,
308
- includeOptionalDeps: options?.includeOptionalDeps,
309
- neverBuiltDependencies: this.dependencyResolver.config.neverBuiltDependencies,
310
- overrides: this.dependencyResolver.config.overrides,
311
- packageImportMethod: this.dependencyResolver.config.packageImportMethod,
312
- rootComponents: hasRootComponents,
313
- updateAll: options?.updateAll,
314
- optimizeReportForNonTerminal: options?.optimizeReportForNonTerminal,
315
- lockfileOnly: options?.lockfileOnly,
316
- };
317
- const prevManifests = new Set<string>();
318
- // TODO: this make duplicate
319
- // this.logger.consoleSuccess();
320
- const linkedDependencies = {
321
- [this.workspace.path]: linkedRootDeps,
322
- };
323
- const compDirMap = await this.getComponentsDirectory([]);
324
- let installCycle = 0;
325
- let hasMissingLocalComponents = true;
326
- const forceTeambitHarmonyLink = !this.dependencyResolver.hasHarmonyInRootPolicy();
327
- /* eslint-disable no-await-in-loop */
328
- do {
329
- // In case there are missing local components,
330
- // we'll need to make another round of installation as on the first round the missing local components
331
- // are not added to the manifests.
332
- // This is an issue when installation is done using root components.
333
- hasMissingLocalComponents = hasRootComponents && hasComponentsFromWorkspaceInMissingDeps(current);
334
- const { dependenciesChanged } = await installer.installComponents(
335
- this.workspace.path,
336
- current.manifests,
337
- mergedRootPolicy,
338
- current.componentDirectoryMap,
339
- {
340
- linkedDependencies,
341
- installTeambitBit: false,
342
- forceTeambitHarmonyLink,
343
- },
344
- pmInstallOptions
345
- );
346
- let cacheCleared = false;
347
- await this.linkCodemods(compDirMap);
348
- if (options?.compile ?? true) {
349
- const compileStartTime = process.hrtime();
350
- const compileOutputMessage = `compiling components`;
351
- this.logger.setStatusLine(compileOutputMessage);
352
- // We need to clear cache before compiling the components or it might compile them with the default env
353
- // incorrectly in case the env was not loaded correctly before the install
354
- this.workspace.consumer.componentLoader.clearComponentsCache();
355
- // We don't want to clear the failed to load envs because we want to show the warning at the end
356
- await this.workspace.clearCache({ skipClearFailedToLoadEnvs: true });
357
- cacheCleared = true;
358
- await this.compiler.compileOnWorkspace([], { initiator: CompilationInitiator.Install });
359
- this.logger.consoleSuccess(compileOutputMessage, compileStartTime);
360
- }
361
- if (options?.writeConfigFiles ?? true) {
362
- await this.tryWriteConfigFiles(!cacheCleared);
363
- }
364
- if (!dependenciesChanged) break;
365
- if (!options?.recurringInstall) break;
366
- const oldNonLoadedEnvs = this.getOldNonLoadedEnvs();
367
- if (!oldNonLoadedEnvs.length) break;
368
- prevManifests.add(manifestsHash(current.manifests));
369
- // If we run compile we do the clear cache before the compilation so no need to clean it again (it's an expensive
370
- // operation)
371
- if (!cacheCleared) {
372
- // We need to clear cache before creating the new component manifests.
373
- this.workspace.consumer.componentLoader.clearComponentsCache();
374
- // We don't want to clear the failed to load envs because we want to show the warning at the end
375
- await this.workspace.clearCache({ skipClearFailedToLoadEnvs: true });
376
- }
377
- current = await this._getComponentsManifests(installer, mergedRootPolicy, calcManifestsOpts);
378
- installCycle += 1;
379
- } while ((!prevManifests.has(manifestsHash(current.manifests)) || hasMissingLocalComponents) && installCycle < 5);
380
- if (!options?.lockfileOnly) {
381
- // We clean node_modules only after the last install.
382
- // Otherwise, we might load an env from a location that we later remove.
383
- await installer.pruneModules(this.workspace.path);
384
- }
385
- await this.workspace.consumer.componentFsCache.deleteAllDependenciesDataCache();
386
- /* eslint-enable no-await-in-loop */
387
- return current.componentDirectoryMap;
388
- }
389
-
390
- private async _getComponentsManifestsAndRootPolicy(
391
- installer: DependencyInstaller,
392
- options: GetComponentsAndManifestsOptions & {
393
- addMissingDeps?: boolean;
394
- linkedRootDeps: Record<string, string>;
395
- }
396
- ): Promise<{ componentsAndManifests: ComponentsAndManifests; mergedRootPolicy: WorkspacePolicy }> {
397
- const mergedRootPolicy = await this.addConfiguredAspectsToWorkspacePolicy();
398
- await this.addConfiguredGeneratorEnvsToWorkspacePolicy(mergedRootPolicy);
399
- const componentsAndManifests = await this._getComponentsManifests(installer, mergedRootPolicy, options);
400
- if (!options?.addMissingDeps) {
401
- return { componentsAndManifests, mergedRootPolicy };
402
- }
403
- const rootDeps = new Set(
404
- Object.keys({
405
- ...componentsAndManifests.manifests[this.workspace.path].devDependencies,
406
- ...componentsAndManifests.manifests[this.workspace.path].dependencies,
407
- ...options.linkedRootDeps,
408
- })
409
- );
410
- Object.values(omit(componentsAndManifests.manifests, [this.workspace.path])).forEach((manifest) => {
411
- if ((manifest as ProjectManifest).name) {
412
- rootDeps.add((manifest as ProjectManifest).name!); // eslint-disable-line @typescript-eslint/no-non-null-assertion
413
- }
414
- });
415
- const addedNewPkgs = await this._addMissingPackagesToRootPolicy(rootDeps);
416
- if (!addedNewPkgs) {
417
- return { componentsAndManifests, mergedRootPolicy };
418
- }
419
- const mergedRootPolicyWithMissingDeps = await this.addConfiguredAspectsToWorkspacePolicy();
420
- return {
421
- mergedRootPolicy: mergedRootPolicyWithMissingDeps,
422
- componentsAndManifests: await this._getComponentsManifests(installer, mergedRootPolicyWithMissingDeps, options),
423
- };
424
- }
425
-
426
- /**
427
- * The function `tryWriteConfigFiles` attempts to write workspace config files, and if it fails, it logs an error
428
- * message.
429
- * @returns If the condition `!shouldWrite` is true, then nothing is being returned. Otherwise, if the `writeConfigFiles`
430
- * function is successfully executed, nothing is being returned. If an error occurs during the execution of
431
- * `writeConfigFiles`, an error message is being returned.
432
- */
433
- private async tryWriteConfigFiles(clearCache: boolean) {
434
- const shouldWrite = this.wsConfigFiles.isWorkspaceConfigWriteEnabled();
435
- if (!shouldWrite) return;
436
- if (clearCache) {
437
- await this.workspace.clearCache({ skipClearFailedToLoadEnvs: true });
438
- }
439
- const { err } = await this.wsConfigFiles.writeConfigFiles({
440
- clean: true,
441
- silent: true,
442
- dedupe: true,
443
- throw: false,
444
- });
445
- if (err) {
446
- this.logger.consoleFailure(
447
- `failed generating workspace config files, please run "bit ws-config write" manually. error: ${err.message}`
448
- );
449
- }
450
- }
451
-
452
- private async addConfiguredAspectsToWorkspacePolicy(): Promise<WorkspacePolicy> {
453
- const rootPolicy = this.dependencyResolver.getWorkspacePolicy();
454
- const aspectsPackages = await this.workspace.getConfiguredUserAspectsPackages({ externalsOnly: true });
455
- aspectsPackages.forEach((aspectsPackage) => {
456
- rootPolicy.add(
457
- {
458
- dependencyId: aspectsPackage.packageName,
459
- value: {
460
- version: aspectsPackage.version,
461
- },
462
- lifecycleType: 'runtime',
463
- },
464
- // If it's already exist from the root, take the version from the root policy
465
- { skipIfExisting: true }
466
- );
467
- });
468
- return rootPolicy;
469
- }
470
-
471
- private async addConfiguredGeneratorEnvsToWorkspacePolicy(rootPolicy: WorkspacePolicy): Promise<void> {
472
- const configuredEnvs = this.generator.getConfiguredEnvs();
473
- const resolvedEnvs = compact(
474
- await Promise.all(
475
- configuredEnvs.map(async (envIdStr) => {
476
- if (this.envs.isCoreEnv(envIdStr)) {
477
- return undefined;
478
- }
479
- const parsedId = await this.workspace.resolveComponentId(envIdStr);
480
- // If we have the env in the workspace, we don't want to install it
481
- const inWs = await this.workspace.hasId(parsedId);
482
- if (inWs) {
483
- return undefined;
484
- }
485
- const comps = await this.workspace.importAndGetMany(
486
- [parsedId],
487
- `to get the env ${parsedId.toString()} for installation`
488
- );
489
- const idWithVersion = await this.workspace.resolveEnvIdWithPotentialVersionForConfig(parsedId);
490
- const version = idWithVersion.split('@')[1] || '*';
491
- const packageName = this.dependencyResolver.getPackageName(comps[0]);
492
- return {
493
- packageName,
494
- version,
495
- };
496
- })
497
- )
498
- );
499
-
500
- resolvedEnvs.forEach((env) => {
501
- rootPolicy.add(
502
- {
503
- dependencyId: env.packageName,
504
- value: {
505
- version: env.version,
506
- },
507
- lifecycleType: 'runtime',
508
- },
509
- // If it's already exist from the root, take the version from the root policy
510
- { skipIfExisting: true }
511
- );
512
- });
513
- }
514
-
515
- private async _addMissingPackagesToRootPolicy(
516
- rootDeps: Set<string>,
517
- options?: WorkspaceInstallOptions
518
- ): Promise<boolean> {
519
- const packages = await this._getMissingPackagesWithoutRootDeps(rootDeps);
520
- if (packages && packages.length) {
521
- await this._addPackages(packages, options);
522
- }
523
- return packages.length > 0;
524
- }
525
-
526
- private async _getMissingPackagesWithoutRootDeps(rootDeps: Set<string>) {
527
- const packages = await this._getAllMissingPackages();
528
- return packages.filter((pkg) => !rootDeps.has(pkg));
529
- }
530
-
531
- private async _getAllMissingPackages(): Promise<string[]> {
532
- const comps = await this.workspace.list();
533
- return uniq(
534
- comps
535
- .map((comp) => {
536
- const data = comp.state.issues.getIssue(IssuesClasses.MissingPackagesDependenciesOnFs)?.data || [];
537
- return data.map((d) => d.missingPackages).flat();
538
- })
539
- .flat()
540
- );
541
- }
542
-
543
- private async _getComponentsManifests(
544
- dependencyInstaller: DependencyInstaller,
545
- rootPolicy: WorkspacePolicy,
546
- installOptions: GetComponentsAndManifestsOptions
547
- ): Promise<ComponentsAndManifests> {
548
- const componentDirectoryMap = await this.getComponentsDirectory([]);
549
- let manifests = await dependencyInstaller.getComponentManifests({
550
- ...installOptions,
551
- componentDirectoryMap,
552
- rootPolicy,
553
- rootDir: this.workspace.path,
554
- referenceLocalPackages: this.dependencyResolver.hasRootComponents() && installOptions.nodeLinker === 'isolated',
555
- });
556
-
557
- if (this.dependencyResolver.hasRootComponents()) {
558
- const rootManifests = await this._getRootManifests(manifests);
559
- await this._updateRootDirs(Object.keys(rootManifests));
560
- manifests = {
561
- ...manifests,
562
- ...rootManifests,
563
- };
564
- }
565
- return {
566
- componentDirectoryMap,
567
- manifests,
568
- };
569
- }
570
-
571
- /**
572
- * This function returns a list of old non-loaded environments names.
573
- * @returns an array of strings called `oldNonLoadedEnvs`. This array contains the names of environment variables that
574
- * failed to load as extensions and are also don't have an env.jsonc file.
575
- * If this list is not empty, then the user might need to run another install to make sure all dependencies resolved
576
- * correctly
577
- */
578
- public getOldNonLoadedEnvs() {
579
- const nonLoadedEnvs = this.envs.getFailedToLoadEnvs();
580
- const envsWithoutManifest = Array.from(this.dependencyResolver.envsWithoutManifest);
581
- const oldNonLoadedEnvs = intersection(nonLoadedEnvs, envsWithoutManifest);
582
- return oldNonLoadedEnvs;
583
- }
584
-
585
- private async _updateRootDirs(rootDirs: string[]) {
586
- try {
587
- const bitRootCompsDir = getBitRootsDir(this.workspace.path);
588
- const existingDirs = await fs.readdir(bitRootCompsDir);
589
- await Promise.all(
590
- existingDirs.map(async (dirName) => {
591
- const dirPath = path.join(bitRootCompsDir, dirName);
592
- if (!rootDirs.includes(dirPath)) {
593
- await fs.remove(dirPath);
594
- }
595
- })
596
- );
597
- } catch (err: any) {
598
- if (err.code !== 'ENOENT') throw err;
599
- }
600
- await Promise.all(rootDirs.map((dirPath) => fs.mkdir(dirPath, { recursive: true })));
601
- }
602
-
603
- private async _getRootManifests(
604
- manifests: Record<string, ProjectManifest>
605
- ): Promise<Record<string, ProjectManifest>> {
606
- const nonRootManifests = Object.values(manifests).filter(({ name }) => name !== 'workspace');
607
- const workspaceDeps = this.dependencyResolver.getWorkspaceDepsOfBitRoots(nonRootManifests);
608
- const workspaceDepsMeta = Object.keys(workspaceDeps).reduce((acc, depName) => {
609
- acc[depName] = { injected: true };
610
- return acc;
611
- }, {});
612
- const envManifests = await this._getEnvManifests(workspaceDeps, workspaceDepsMeta);
613
- const appManifests = await this._getAppManifests(manifests, workspaceDeps, workspaceDepsMeta);
614
- return {
615
- ...envManifests,
616
- ...appManifests,
617
- };
618
- }
619
-
620
- private async _getEnvManifests(
621
- workspaceDeps: Record<string, string>,
622
- workspaceDepsMeta: Record<string, { injected: true }>
623
- ): Promise<Record<string, ProjectManifest>> {
624
- const envs = await this._getAllUsedEnvIds();
625
- return Object.fromEntries(
626
- await Promise.all(
627
- envs.map(async (envId) => {
628
- return [
629
- await this.getRootComponentDirByRootId(this.workspace.path, envId),
630
- {
631
- dependencies: {
632
- ...(await this._getEnvDependencies(envId)),
633
- ...workspaceDeps,
634
- ...(await this._getEnvPackage(envId)),
635
- },
636
- dependenciesMeta: workspaceDepsMeta,
637
- installConfig: {
638
- hoistingLimits: 'workspaces',
639
- },
640
- },
641
- ];
642
- })
643
- )
644
- );
645
- }
646
-
647
- private async _getEnvDependencies(envId: ComponentID): Promise<Record<string, string>> {
648
- const policy = await this.dependencyResolver.getEnvPolicyFromEnvId(envId);
649
- if (!policy) return {};
650
- return Object.fromEntries(
651
- policy.selfPolicy.entries
652
- .filter(({ force, value }) => force && value.version !== '-')
653
- .map(({ dependencyId, value }) => [dependencyId, value.version])
654
- );
655
- }
656
-
657
- /**
658
- * Return the package name of the env with its version.
659
- * (only if the env is not a core env and is not in the workspace)
660
- * @param envId
661
- * @returns
662
- */
663
- private async _getEnvPackage(envId: ComponentID): Promise<Record<string, string> | undefined> {
664
- if (this.envs.isCoreEnv(envId.toStringWithoutVersion())) return undefined;
665
- const inWs = await this.workspace.hasId(envId);
666
- if (inWs) return undefined;
667
- const envComponent = await this.envs.getEnvComponentByEnvId(envId.toString(), envId.toString());
668
- if (!envComponent) return undefined;
669
- const packageName = this.dependencyResolver.getPackageName(envComponent);
670
- const version = envId.version;
671
- const finalVersion = snapToSemver(version as string);
672
- return { [packageName]: finalVersion };
673
- }
674
-
675
- private async _getAppManifests(
676
- manifests: Record<string, ProjectManifest>,
677
- workspaceDeps: Record<string, string>,
678
- workspaceDepsMeta: Record<string, { injected: true }>
679
- ): Promise<Record<string, ProjectManifest>> {
680
- return Object.fromEntries(
681
- compact(
682
- await Promise.all(
683
- (
684
- await this.app.listAppsComponents()
685
- ).map(async (app) => {
686
- const appPkgName = this.dependencyResolver.getPackageName(app);
687
- const appManifest = Object.values(manifests).find(({ name }) => name === appPkgName);
688
- if (!appManifest) return null;
689
- const envId = await this.envs.calculateEnvId(app);
690
- return [
691
- await this.getRootComponentDirByRootId(this.workspace.path, app.id),
692
- {
693
- ...omit(appManifest, ['name', 'version']),
694
- dependencies: {
695
- ...(await this._getEnvDependencies(envId)),
696
- ...appManifest.dependencies,
697
- ...workspaceDeps,
698
- },
699
- dependenciesMeta: {
700
- ...appManifest.dependenciesMeta,
701
- ...workspaceDepsMeta,
702
- },
703
- installConfig: {
704
- hoistingLimits: 'workspaces',
705
- },
706
- },
707
- ];
708
- })
709
- )
710
- )
711
- );
712
- }
713
-
714
- private async _getAllUsedEnvIds(): Promise<ComponentID[]> {
715
- const envs = new Map<string, ComponentID>();
716
- const components = await this.workspace.list();
717
- await pMapSeries(components, async (component) => {
718
- const envId = await this.envs.calculateEnvId(component);
719
- envs.set(envId.toString(), envId);
720
- });
721
- return Array.from(envs.values());
722
- }
723
-
724
- /**
725
- * Updates out-of-date dependencies in the workspace.
726
- *
727
- * @param options.all {Boolean} updates all outdated dependencies without showing a prompt.
728
- */
729
- async updateDependencies(options: {
730
- forceVersionBump?: 'major' | 'minor' | 'patch' | 'compatible';
731
- patterns?: string[];
732
- all: boolean;
733
- }): Promise<ComponentMap<string> | null> {
734
- const componentPolicies = await this._getComponentsWithDependencyPolicies();
735
- const variantPatterns = this.variants.raw();
736
- const variantPoliciesByPatterns = this._variantPatternsToDepPolicesDict(variantPatterns);
737
- const components = await this.workspace.list();
738
- const outdatedPkgs = await this.dependencyResolver.getOutdatedPkgsFromPolicies({
739
- rootDir: this.workspace.path,
740
- variantPoliciesByPatterns,
741
- componentPolicies,
742
- components,
743
- patterns: options.patterns,
744
- forceVersionBump: options.forceVersionBump,
745
- });
746
- if (outdatedPkgs == null) {
747
- this.logger.consoleFailure('No dependencies found that match the patterns');
748
- return null;
749
- }
750
- let outdatedPkgsToUpdate!: MergedOutdatedPkg[];
751
- if (options.all) {
752
- outdatedPkgsToUpdate = outdatedPkgs;
753
- } else {
754
- this.logger.off();
755
- outdatedPkgsToUpdate = await pickOutdatedPkgs(outdatedPkgs);
756
- this.logger.on();
757
- }
758
- if (outdatedPkgsToUpdate.length === 0) {
759
- this.logger.consoleSuccess('No outdated dependencies found');
760
- if (options.forceVersionBump === 'compatible') {
761
- this.logger.console(
762
- "If you want to find new versions that don't match the current version ranges, retry with the --latest flag"
763
- );
764
- }
765
- return null;
766
- }
767
- const { updatedVariants, updatedComponents } = this.dependencyResolver.applyUpdates(outdatedPkgsToUpdate, {
768
- variantPoliciesByPatterns,
769
- });
770
- await this._updateVariantsPolicies(variantPatterns, updatedVariants);
771
- await this._updateComponentsConfig(updatedComponents);
772
- await this.workspace._reloadConsumer();
773
- return this._installModules({ dedupe: true });
774
- }
775
-
776
- async addDuplicateComponentAndPackageIssue(components: Component[]) {
777
- const workspacePolicy = this.dependencyResolver.getWorkspacePolicy();
778
- components.forEach((component) => {
779
- if (component.state._consumer.removed) return;
780
- const pkgName = componentIdToPackageName(component.state._consumer);
781
- const found = workspacePolicy.find(pkgName);
782
- if (found) {
783
- component.state.issues.getOrCreate(IssuesClasses.DuplicateComponentAndPackage).data = found.dependencyId;
784
- }
785
- });
786
- }
787
-
788
- private async _getComponentsWithDependencyPolicies() {
789
- const allComponentIds = await this.workspace.listIds();
790
- const componentPolicies = [] as Array<{ componentId: ComponentID; policy: any }>;
791
- (
792
- await Promise.all<ComponentConfigFile | undefined>(
793
- allComponentIds.map((componentId) => this.workspace.componentConfigFile(componentId))
794
- )
795
- ).forEach((componentConfigFile, index) => {
796
- if (!componentConfigFile) return;
797
- const depResolverConfig = componentConfigFile.aspects.get(DependencyResolverAspect.id);
798
- if (!depResolverConfig) return;
799
- const componentId = allComponentIds[index];
800
- componentPolicies.push({ componentId, policy: depResolverConfig.config.policy });
801
- });
802
- return componentPolicies;
803
- }
804
-
805
- private _variantPatternsToDepPolicesDict(variantPatterns: Patterns): Record<string, VariantPolicyConfigObject> {
806
- const variantPoliciesByPatterns: Record<string, VariantPolicyConfigObject> = {};
807
- for (const [variantPattern, extensions] of Object.entries(variantPatterns)) {
808
- if (extensions[DependencyResolverAspect.id]?.policy) {
809
- variantPoliciesByPatterns[variantPattern] = extensions[DependencyResolverAspect.id]?.policy;
810
- }
811
- }
812
- return variantPoliciesByPatterns;
813
- }
814
-
815
- private async _updateComponentsConfig(updatedComponents: UpdatedComponent[]) {
816
- if (updatedComponents.length === 0) return;
817
- await Promise.all(
818
- updatedComponents.map(({ componentId, config }) => {
819
- return this.workspace.addSpecificComponentConfig(componentId, DependencyResolverAspect.id, config, {
820
- shouldMergeWithExisting: true,
821
- shouldMergeWithPrevious: true,
822
- });
823
- })
824
- );
825
- await this.workspace.bitMap.write('update (dependencies)');
826
- }
827
-
828
- private async _updateVariantsPolicies(variantPatterns: Record<string, any>, updateVariantPolicies: string[]) {
829
- for (const variantPattern of updateVariantPolicies) {
830
- this.variants.setExtension(
831
- variantPattern,
832
- DependencyResolverAspect.id,
833
- variantPatterns[variantPattern][DependencyResolverAspect.id],
834
- { overrideExisting: true }
835
- );
836
- }
837
- await this.dependencyResolver.persistConfig(this.workspace.path);
838
- }
839
-
840
- /**
841
- * Uninstall the specified packages from dependencies.
842
- *
843
- * @param {string[]} the list of packages that should be removed from dependencies.
844
- */
845
- async uninstallDependencies(packages: string[]) {
846
- this.dependencyResolver.removeFromRootPolicy(packages);
847
- await this.dependencyResolver.persistConfig(this.workspace.path);
848
- return this._installModules({ dedupe: true });
849
- }
850
-
851
- /**
852
- * This function returns all the locations of the external links that should be created inside node_modules.
853
- * This information may then be passed to the package manager, which will create the links on its own.
854
- */
855
- async calculateLinks(
856
- options: WorkspaceLinkOptions = {}
857
- ): Promise<{ linkResults: WorkspaceLinkResults; linkedRootDeps: Record<string, string> }> {
858
- await pMapSeries(this.preLinkSlot.values(), (fn) => fn(options)); // import objects if not disabled in options
859
- const compDirMap = await this.getComponentsDirectory([]);
860
- const linker = this.dependencyResolver.getLinker({
861
- rootDir: this.workspace.path,
862
- linkingOptions: options,
863
- });
864
- const { linkResults: res, linkedRootDeps } = await linker.calculateLinkedDeps(
865
- this.workspace.path,
866
- compDirMap,
867
- options
868
- );
869
- const workspaceRes = res as WorkspaceLinkResults;
870
-
871
- const legacyResults = await this.linkCodemods(compDirMap, options);
872
- workspaceRes.legacyLinkResults = legacyResults.linksResults;
873
- workspaceRes.legacyLinkCodemodResults = legacyResults.codemodResults;
874
-
875
- if (this.dependencyResolver.hasRootComponents() && options.linkToBitRoots) {
876
- await this._linkAllComponentsToBitRoots(compDirMap);
877
- }
878
- return { linkResults: res, linkedRootDeps };
879
- }
880
-
881
- async linkCodemods(compDirMap: ComponentMap<string>, options?: { rewire?: boolean }) {
882
- const bitIds = compDirMap.toArray().map(([component]) => component.id);
883
- return linkToNodeModulesWithCodemod(this.workspace, bitIds, options?.rewire ?? false);
884
- }
885
-
886
- async link(options: WorkspaceLinkOptions = {}): Promise<WorkspaceLinkResults> {
887
- const { linkResults, linkedRootDeps } = await this.calculateLinks(options);
888
- await createLinks(options.linkToDir ?? this.workspace.path, linkedRootDeps, {
889
- avoidHardLink: true,
890
- skipIfSymlinkValid: true,
891
- });
892
- return linkResults;
893
- }
894
-
895
- private async _linkAllComponentsToBitRoots(compDirMap: ComponentMap<string>) {
896
- const envs = await this._getAllUsedEnvIds();
897
- const apps = (await this.app.listAppsComponents()).map((component) => component.id);
898
- await Promise.all(
899
- [...envs, ...apps].map(async (id) => {
900
- const dir = await this.getRootComponentDirByRootId(this.workspace.path, id);
901
- await fs.mkdirp(dir);
902
- })
903
- );
904
- await linkPkgsToBitRoots(
905
- this.workspace.path,
906
- compDirMap.components.map((component) => this.dependencyResolver.getPackageName(component))
907
- );
908
- }
909
-
910
- private async getRootComponentDirByRootId(workspacePath: string, rootComponentId: ComponentID): Promise<string> {
911
- // Root directories for local envs and apps are created without their version number.
912
- // This is done in order to avoid changes to the lockfile after such components are tagged.
913
- const id = (await this.workspace.hasId(rootComponentId))
914
- ? rootComponentId.toStringWithoutVersion()
915
- : rootComponentId.toString();
916
- return getRootComponentDir(workspacePath, id);
917
- }
918
-
919
- /**
920
- * Generate a filter to pass to the installer
921
- * This will filter deps which are come from remotes which defined in scope.json
922
- * those components comes from local remotes, usually doesn't have a package in a registry
923
- * so no reason to try to install them (it will fail)
924
- */
925
- private async generateFilterFnForDepsFromLocalRemote() {
926
- // TODO: once scope create a new API for this, replace it with the new one
927
- const remotes = await this.workspace.scope._legacyRemotes();
928
- const reg = await this.dependencyResolver.getRegistries();
929
- const packageScopes = Object.keys(reg.scopes);
930
- return (dependencyList: DependencyList): DependencyList => {
931
- const filtered = dependencyList.filter((dep) => {
932
- if (!(dep instanceof ComponentDependency)) {
933
- return true;
934
- }
935
- if (remotes.isHub(dep.componentId.scope)) {
936
- return true;
937
- }
938
- if (packageScopes.some((scope) => dep.packageName.startsWith(`@${scope}/`))) {
939
- return true;
940
- }
941
- return false;
942
- });
943
- return filtered;
944
- };
945
- }
946
-
947
- private async getComponentsDirectory(ids: ComponentID[]): Promise<ComponentMap<string>> {
948
- const components = ids.length
949
- ? await this.workspace.getMany(ids)
950
- : await this.workspace.list(undefined, { loadSeedersAsAspects: false });
951
- return ComponentMap.as<string>(components, (component) => this.workspace.componentDir(component.id));
952
- }
953
-
954
- private async onRootAspectAddedSubscriber(_aspectId: ComponentID, inWs: boolean): Promise<void> {
955
- if (!inWs) {
956
- await this.install();
957
- }
958
- }
959
- private async onAspectsResolveSubscriber(aspectComponents: Component[]): Promise<void> {
960
- let needLink = false;
961
- let needInstall = false;
962
- const promises = aspectComponents.map(async (aspectComponent) => {
963
- const aspectIdStr = aspectComponent.id.toString();
964
- if (this.visitedAspects.has(aspectIdStr)) return;
965
-
966
- this.visitedAspects.add(aspectIdStr);
967
- const packagePath = await this.workspace.getComponentPackagePath(aspectComponent);
968
- const exists = await pathExists(packagePath);
969
- if (!exists) {
970
- const inWs = await this.workspace.hasId(aspectComponent.id);
971
- if (inWs) {
972
- needLink = true;
973
- } else {
974
- needInstall = true;
975
- }
976
- }
977
- });
978
- await Promise.all(promises);
979
- if (needInstall) {
980
- await this.install();
981
- return;
982
- }
983
- if (needLink) {
984
- await this.link();
985
- }
986
- }
987
-
988
- static slots = [Slot.withType<PreLinkSlot>(), Slot.withType<PreInstallSlot>(), Slot.withType<PostInstallSlot>()];
989
- static dependencies = [
990
- DependencyResolverAspect,
991
- WorkspaceAspect,
992
- LoggerAspect,
993
- VariantsAspect,
994
- CLIAspect,
995
- CompilerAspect,
996
- IssuesAspect,
997
- EnvsAspect,
998
- ApplicationAspect,
999
- IpcEventsAspect,
1000
- GeneratorAspect,
1001
- WorkspaceConfigFilesAspect,
1002
- ];
1003
-
1004
- static runtime = MainRuntime;
1005
-
1006
- static async provider(
1007
- [
1008
- dependencyResolver,
1009
- workspace,
1010
- loggerExt,
1011
- variants,
1012
- cli,
1013
- compiler,
1014
- issues,
1015
- envs,
1016
- app,
1017
- ipcEvents,
1018
- generator,
1019
- wsConfigFiles,
1020
- ]: [
1021
- DependencyResolverMain,
1022
- Workspace,
1023
- LoggerMain,
1024
- VariantsMain,
1025
- CLIMain,
1026
- CompilerMain,
1027
- IssuesMain,
1028
- EnvsMain,
1029
- ApplicationMain,
1030
- IpcEventsMain,
1031
- GeneratorMain,
1032
- WorkspaceConfigFilesMain
1033
- ],
1034
- _,
1035
- [preLinkSlot, preInstallSlot, postInstallSlot]: [PreLinkSlot, PreInstallSlot, PostInstallSlot]
1036
- ) {
1037
- const logger = loggerExt.createLogger(InstallAspect.id);
1038
- ipcEvents.registerGotEventSlot(async (eventName) => {
1039
- if (eventName !== 'onPostInstall') return;
1040
- logger.debug('got onPostInstall event, clear workspace and all components cache');
1041
- await workspace.clearCache();
1042
- workspace.clearAllComponentsCache();
1043
- await pMapSeries(postInstallSlot.values(), (fn) => fn());
1044
- });
1045
- const installExt = new InstallMain(
1046
- dependencyResolver,
1047
- logger,
1048
- workspace,
1049
- variants,
1050
- compiler,
1051
- envs,
1052
- wsConfigFiles,
1053
- app,
1054
- generator,
1055
- preLinkSlot,
1056
- preInstallSlot,
1057
- postInstallSlot,
1058
- ipcEvents
1059
- );
1060
- if (issues) {
1061
- issues.registerAddComponentsIssues(installExt.addDuplicateComponentAndPackageIssue.bind(installExt));
1062
- }
1063
- generator.registerOnComponentCreate(installExt.onComponentCreate.bind(installExt));
1064
- const commands: CommandList = [
1065
- new InstallCmd(installExt, workspace, logger),
1066
- new UninstallCmd(installExt),
1067
- new UpdateCmd(installExt),
1068
- new LinkCommand(installExt, workspace, logger),
1069
- ];
1070
- // For now do not automate installation during aspect resolving
1071
- // workspace.registerOnAspectsResolve(installExt.onAspectsResolveSubscriber.bind(installExt));
1072
- if (workspace) {
1073
- workspace.registerOnRootAspectAdded(installExt.onRootAspectAddedSubscriber.bind(installExt));
1074
- }
1075
- cli.register(...commands);
1076
- return installExt;
1077
- }
1078
- }
1079
-
1080
- type ComponentsAndManifests = {
1081
- componentDirectoryMap: ComponentMap<string>;
1082
- manifests: Record<string, ProjectManifest>;
1083
- };
1084
-
1085
- function hasComponentsFromWorkspaceInMissingDeps({
1086
- componentDirectoryMap,
1087
- manifests,
1088
- }: ComponentsAndManifests): boolean {
1089
- const missingDeps = new Set<string>(
1090
- componentDirectoryMap
1091
- .toArray()
1092
- .map(([{ state }]) => {
1093
- const issue = state.issues.getIssue(IssuesClasses.MissingPackagesDependenciesOnFs);
1094
- if (!issue) return [];
1095
- return issue.data.map((d) => d.missingPackages).flat();
1096
- })
1097
- .flat()
1098
- );
1099
- return Object.values(manifests).some(({ name }) => name && missingDeps.has(name));
1100
- }
1101
-
1102
- InstallAspect.addRuntime(InstallMain);
1103
-
1104
- export default InstallMain;
1105
-
1106
- function manifestsHash(manifests: Record<string, ProjectManifest>): string {
1107
- // We don't care if the type of the dependency changes as it doesn't change the node_modules structure
1108
- const depsByProjectPaths = mapValues(manifests, (manifest) => ({
1109
- ...manifest.devDependencies,
1110
- ...manifest.dependencies,
1111
- }));
1112
- return hash(depsByProjectPaths);
1113
- }