@teambit/install 0.0.0-0494a1bc1f010e387719d84abc70a90c0e5b6b7d

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.
Files changed (77) hide show
  1. package/dist/esm.mjs +7 -0
  2. package/dist/exceptions/dependency-type-not-supported-in-policy.d.ts +4 -0
  3. package/dist/exceptions/dependency-type-not-supported-in-policy.js +21 -0
  4. package/dist/exceptions/dependency-type-not-supported-in-policy.js.map +1 -0
  5. package/dist/exceptions/index.d.ts +1 -0
  6. package/dist/exceptions/index.js +20 -0
  7. package/dist/exceptions/index.js.map +1 -0
  8. package/dist/index.d.ts +5 -0
  9. package/dist/index.js +35 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/install.aspect.d.ts +2 -0
  12. package/dist/install.aspect.js +18 -0
  13. package/dist/install.aspect.js.map +1 -0
  14. package/dist/install.cmd.d.ts +55 -0
  15. package/dist/install.cmd.js +154 -0
  16. package/dist/install.cmd.js.map +1 -0
  17. package/dist/install.main.runtime.d.ts +246 -0
  18. package/dist/install.main.runtime.js +1397 -0
  19. package/dist/install.main.runtime.js.map +1 -0
  20. package/dist/link/component-list-links.d.ts +9 -0
  21. package/dist/link/component-list-links.js +97 -0
  22. package/dist/link/component-list-links.js.map +1 -0
  23. package/dist/link/core-aspects-links.d.ts +7 -0
  24. package/dist/link/core-aspects-links.js +77 -0
  25. package/dist/link/core-aspects-links.js.map +1 -0
  26. package/dist/link/get-package-name-from-target.d.ts +1 -0
  27. package/dist/link/get-package-name-from-target.js +13 -0
  28. package/dist/link/get-package-name-from-target.js.map +1 -0
  29. package/dist/link/index.d.ts +1 -0
  30. package/dist/link/index.js +20 -0
  31. package/dist/link/index.js.map +1 -0
  32. package/dist/link/link-row.d.ts +12 -0
  33. package/dist/link/link-row.js +32 -0
  34. package/dist/link/link-row.js.map +1 -0
  35. package/dist/link/link-to-dir.d.ts +2 -0
  36. package/dist/link/link-to-dir.js +35 -0
  37. package/dist/link/link-to-dir.js.map +1 -0
  38. package/dist/link/link.cmd.d.ts +46 -0
  39. package/dist/link/link.cmd.js +144 -0
  40. package/dist/link/link.cmd.js.map +1 -0
  41. package/dist/link/nested-deps-in-nm-links.d.ts +7 -0
  42. package/dist/link/nested-deps-in-nm-links.js +59 -0
  43. package/dist/link/nested-deps-in-nm-links.js.map +1 -0
  44. package/dist/link/rewire-row.d.ts +6 -0
  45. package/dist/link/rewire-row.js +26 -0
  46. package/dist/link/rewire-row.js.map +1 -0
  47. package/dist/pick-outdated-pkgs.d.ts +12 -0
  48. package/dist/pick-outdated-pkgs.js +180 -0
  49. package/dist/pick-outdated-pkgs.js.map +1 -0
  50. package/dist/pick-outdated-pkgs.spec.d.ts +1 -0
  51. package/dist/pick-outdated-pkgs.spec.js +171 -0
  52. package/dist/pick-outdated-pkgs.spec.js.map +1 -0
  53. package/dist/preview-1752093506725.js +7 -0
  54. package/dist/uninstall.cmd.d.ts +12 -0
  55. package/dist/uninstall.cmd.js +26 -0
  56. package/dist/uninstall.cmd.js.map +1 -0
  57. package/dist/update.cmd.d.ts +26 -0
  58. package/dist/update.cmd.js +52 -0
  59. package/dist/update.cmd.js.map +1 -0
  60. package/esm.mjs +7 -0
  61. package/exceptions/dependency-type-not-supported-in-policy.ts +7 -0
  62. package/exceptions/index.ts +1 -0
  63. package/install.cmd.tsx +189 -0
  64. package/link/component-list-links.ts +66 -0
  65. package/link/core-aspects-links.ts +50 -0
  66. package/link/get-package-name-from-target.ts +5 -0
  67. package/link/index.ts +1 -0
  68. package/link/link-row.ts +20 -0
  69. package/link/link-to-dir.ts +13 -0
  70. package/link/link.cmd.ts +112 -0
  71. package/link/nested-deps-in-nm-links.ts +47 -0
  72. package/link/rewire-row.ts +17 -0
  73. package/package.json +95 -0
  74. package/types/asset.d.ts +41 -0
  75. package/types/style.d.ts +42 -0
  76. package/uninstall.cmd.tsx +18 -0
  77. package/update.cmd.tsx +67 -0
@@ -0,0 +1,1397 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = exports.InstallMain = void 0;
7
+ function _pFilter() {
8
+ const data = _interopRequireDefault(require("p-filter"));
9
+ _pFilter = function () {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
14
+ function _fsExtra() {
15
+ const data = _interopRequireWildcard(require("fs-extra"));
16
+ _fsExtra = function () {
17
+ return data;
18
+ };
19
+ return data;
20
+ }
21
+ function _path() {
22
+ const data = _interopRequireDefault(require("path"));
23
+ _path = function () {
24
+ return data;
25
+ };
26
+ return data;
27
+ }
28
+ function _workspace() {
29
+ const data = require("@teambit/workspace.root-components");
30
+ _workspace = function () {
31
+ return data;
32
+ };
33
+ return data;
34
+ }
35
+ function _compiler() {
36
+ const data = require("@teambit/compiler");
37
+ _compiler = function () {
38
+ return data;
39
+ };
40
+ return data;
41
+ }
42
+ function _cli() {
43
+ const data = require("@teambit/cli");
44
+ _cli = function () {
45
+ return data;
46
+ };
47
+ return data;
48
+ }
49
+ function _chalk() {
50
+ const data = _interopRequireDefault(require("chalk"));
51
+ _chalk = function () {
52
+ return data;
53
+ };
54
+ return data;
55
+ }
56
+ function _yesno() {
57
+ const data = _interopRequireDefault(require("yesno"));
58
+ _yesno = function () {
59
+ return data;
60
+ };
61
+ return data;
62
+ }
63
+ function _workspace2() {
64
+ const data = require("@teambit/workspace");
65
+ _workspace2 = function () {
66
+ return data;
67
+ };
68
+ return data;
69
+ }
70
+ function _lodash() {
71
+ const data = require("lodash");
72
+ _lodash = function () {
73
+ return data;
74
+ };
75
+ return data;
76
+ }
77
+ function _generator() {
78
+ const data = require("@teambit/generator");
79
+ _generator = function () {
80
+ return data;
81
+ };
82
+ return data;
83
+ }
84
+ function _pkgModules() {
85
+ const data = require("@teambit/pkg.modules.component-package-name");
86
+ _pkgModules = function () {
87
+ return data;
88
+ };
89
+ return data;
90
+ }
91
+ function _application() {
92
+ const data = require("@teambit/application");
93
+ _application = function () {
94
+ return data;
95
+ };
96
+ return data;
97
+ }
98
+ function _variants() {
99
+ const data = require("@teambit/variants");
100
+ _variants = function () {
101
+ return data;
102
+ };
103
+ return data;
104
+ }
105
+ function _component() {
106
+ const data = require("@teambit/component");
107
+ _component = function () {
108
+ return data;
109
+ };
110
+ return data;
111
+ }
112
+ function _dependenciesFs() {
113
+ const data = require("@teambit/dependencies.fs.linked-dependencies");
114
+ _dependenciesFs = function () {
115
+ return data;
116
+ };
117
+ return data;
118
+ }
119
+ function _pMapSeries() {
120
+ const data = _interopRequireDefault(require("p-map-series"));
121
+ _pMapSeries = function () {
122
+ return data;
123
+ };
124
+ return data;
125
+ }
126
+ function _harmony() {
127
+ const data = require("@teambit/harmony");
128
+ _harmony = function () {
129
+ return data;
130
+ };
131
+ return data;
132
+ }
133
+ function _workspaceModules() {
134
+ const data = require("@teambit/workspace.modules.node-modules-linker");
135
+ _workspaceModules = function () {
136
+ return data;
137
+ };
138
+ return data;
139
+ }
140
+ function _envs() {
141
+ const data = require("@teambit/envs");
142
+ _envs = function () {
143
+ return data;
144
+ };
145
+ return data;
146
+ }
147
+ function _ipcEvents() {
148
+ const data = require("@teambit/ipc-events");
149
+ _ipcEvents = function () {
150
+ return data;
151
+ };
152
+ return data;
153
+ }
154
+ function _componentIssues() {
155
+ const data = require("@teambit/component-issues");
156
+ _componentIssues = function () {
157
+ return data;
158
+ };
159
+ return data;
160
+ }
161
+ function _dependencyResolver() {
162
+ const data = require("@teambit/dependency-resolver");
163
+ _dependencyResolver = function () {
164
+ return data;
165
+ };
166
+ return data;
167
+ }
168
+ function _workspaceConfigFiles() {
169
+ const data = require("@teambit/workspace-config-files");
170
+ _workspaceConfigFiles = function () {
171
+ return data;
172
+ };
173
+ return data;
174
+ }
175
+ function _logger() {
176
+ const data = require("@teambit/logger");
177
+ _logger = function () {
178
+ return data;
179
+ };
180
+ return data;
181
+ }
182
+ function _issues() {
183
+ const data = require("@teambit/issues");
184
+ _issues = function () {
185
+ return data;
186
+ };
187
+ return data;
188
+ }
189
+ function _componentPackageVersion() {
190
+ const data = require("@teambit/component-package-version");
191
+ _componentPackageVersion = function () {
192
+ return data;
193
+ };
194
+ return data;
195
+ }
196
+ function _aspectLoader() {
197
+ const data = require("@teambit/aspect-loader");
198
+ _aspectLoader = function () {
199
+ return data;
200
+ };
201
+ return data;
202
+ }
203
+ function _objectHash() {
204
+ const data = _interopRequireDefault(require("object-hash"));
205
+ _objectHash = function () {
206
+ return data;
207
+ };
208
+ return data;
209
+ }
210
+ function _bundler() {
211
+ const data = require("@teambit/bundler");
212
+ _bundler = function () {
213
+ return data;
214
+ };
215
+ return data;
216
+ }
217
+ function _ui() {
218
+ const data = require("@teambit/ui");
219
+ _ui = function () {
220
+ return data;
221
+ };
222
+ return data;
223
+ }
224
+ function _exceptions() {
225
+ const data = require("./exceptions");
226
+ _exceptions = function () {
227
+ return data;
228
+ };
229
+ return data;
230
+ }
231
+ function _install() {
232
+ const data = require("./install.aspect");
233
+ _install = function () {
234
+ return data;
235
+ };
236
+ return data;
237
+ }
238
+ function _pickOutdatedPkgs() {
239
+ const data = require("./pick-outdated-pkgs");
240
+ _pickOutdatedPkgs = function () {
241
+ return data;
242
+ };
243
+ return data;
244
+ }
245
+ function _link() {
246
+ const data = require("./link");
247
+ _link = function () {
248
+ return data;
249
+ };
250
+ return data;
251
+ }
252
+ function _install2() {
253
+ const data = _interopRequireDefault(require("./install.cmd"));
254
+ _install2 = function () {
255
+ return data;
256
+ };
257
+ return data;
258
+ }
259
+ function _uninstall() {
260
+ const data = _interopRequireDefault(require("./uninstall.cmd"));
261
+ _uninstall = function () {
262
+ return data;
263
+ };
264
+ return data;
265
+ }
266
+ function _update() {
267
+ const data = _interopRequireDefault(require("./update.cmd"));
268
+ _update = function () {
269
+ return data;
270
+ };
271
+ return data;
272
+ }
273
+ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
274
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
275
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
276
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
277
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
278
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
279
+ function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
280
+ class InstallMain {
281
+ constructor(dependencyResolver, logger, workspace, variants, compiler, envs, wsConfigFiles, aspectLoader, app, generator, preLinkSlot, preInstallSlot, postInstallSlot, ipcEvents, harmony) {
282
+ this.dependencyResolver = dependencyResolver;
283
+ this.logger = logger;
284
+ this.workspace = workspace;
285
+ this.variants = variants;
286
+ this.compiler = compiler;
287
+ this.envs = envs;
288
+ this.wsConfigFiles = wsConfigFiles;
289
+ this.aspectLoader = aspectLoader;
290
+ this.app = app;
291
+ this.generator = generator;
292
+ this.preLinkSlot = preLinkSlot;
293
+ this.preInstallSlot = preInstallSlot;
294
+ this.postInstallSlot = postInstallSlot;
295
+ this.ipcEvents = ipcEvents;
296
+ this.harmony = harmony;
297
+ _defineProperty(this, "visitedAspects", new Set());
298
+ _defineProperty(this, "oldNonLoadedEnvs", []);
299
+ }
300
+ /**
301
+ * Install dependencies for all components in the workspace
302
+ *
303
+ * @returns
304
+ * @memberof Workspace
305
+ */
306
+ async install(packages, options) {
307
+ // Check if external package manager mode is enabled
308
+ const workspaceConfig = this.workspace.getWorkspaceConfig();
309
+ const workspaceExtConfig = workspaceConfig.extensions.findExtension(_workspace2().WorkspaceAspect.id);
310
+ if (workspaceExtConfig?.config.externalPackageManager) {
311
+ await this.handleExternalPackageManagerPrompt();
312
+ }
313
+
314
+ // set workspace in install context
315
+ this.workspace.inInstallContext = true;
316
+ this.workspace.inInstallAfterPmContext = false;
317
+ if (packages && packages.length) {
318
+ await this._addPackages(packages, options);
319
+ }
320
+ if (options?.addMissingPeers) {
321
+ const compDirMap = await this.getComponentsDirectory([]);
322
+ const mergedRootPolicy = this.dependencyResolver.getWorkspacePolicy();
323
+ const depsFilterFn = await this.generateFilterFnForDepsFromLocalRemote();
324
+ const pmInstallOptions = {
325
+ dedupe: options?.dedupe,
326
+ copyPeerToRuntimeOnRoot: options?.copyPeerToRuntimeOnRoot ?? true,
327
+ copyPeerToRuntimeOnComponents: options?.copyPeerToRuntimeOnComponents ?? false,
328
+ dependencyFilterFn: depsFilterFn,
329
+ overrides: this.dependencyResolver.config.overrides,
330
+ hoistPatterns: this.dependencyResolver.config.hoistPatterns,
331
+ packageImportMethod: this.dependencyResolver.config.packageImportMethod
332
+ };
333
+ const missingPeers = await this.dependencyResolver.getMissingPeerDependencies(this.workspace.path, mergedRootPolicy, compDirMap, pmInstallOptions);
334
+ if (missingPeers) {
335
+ const missingPeerPackages = Object.entries(missingPeers).map(([peerName, range]) => `${peerName}@${range}`);
336
+ await this._addPackages(missingPeerPackages, options);
337
+ } else {
338
+ this.logger.console('No missing peer dependencies found.');
339
+ }
340
+ }
341
+ await (0, _pMapSeries().default)(this.preInstallSlot.values(), fn => fn(options)); // import objects if not disabled in options
342
+ const res = await this._installModules(options);
343
+ this.workspace.inInstallContext = false;
344
+ await this.ipcEvents.publishIpcEvent('onPostInstall');
345
+ return res;
346
+ }
347
+ registerPreLink(fn) {
348
+ this.preLinkSlot.register(fn);
349
+ }
350
+ registerPreInstall(fn) {
351
+ this.preInstallSlot.register(fn);
352
+ }
353
+ registerPostInstall(fn) {
354
+ this.postInstallSlot.register(fn);
355
+ }
356
+ async onComponentCreate(generateResults, installOptions) {
357
+ this.workspace.inInstallContext = true;
358
+ let runInstall = false;
359
+ let packages = [];
360
+ let installMissing = false;
361
+ const ids = generateResults.map(generateResult => {
362
+ if (generateResult.dependencies && generateResult.dependencies.length) {
363
+ packages = packages.concat(generateResult.dependencies);
364
+ runInstall = true;
365
+ }
366
+ if (generateResult.installMissingDependencies) {
367
+ installMissing = true;
368
+ runInstall = true;
369
+ }
370
+ if (generateResult.isApp || generateResult.isEnv) {
371
+ runInstall = true;
372
+ }
373
+ return generateResult.id;
374
+ });
375
+ const nonLoadedEnvs = [];
376
+ ids.map(id => this.workspace.clearComponentCache(id));
377
+ await (0, _pMapSeries().default)(ids, async id => {
378
+ const component = await this.workspace.get(id);
379
+ // const envId = await this.envs.getEnvId(component);
380
+ const envId = (await this.envs.calculateEnvId(component)).toString();
381
+ const isLoaded = this.envs.isEnvRegistered(envId);
382
+ if (!isLoaded) {
383
+ nonLoadedEnvs.push(envId);
384
+ }
385
+ return component;
386
+ });
387
+ if (nonLoadedEnvs.length) {
388
+ runInstall = true;
389
+ }
390
+ if (!runInstall) {
391
+ this.workspace.inInstallContext = false;
392
+ return;
393
+ }
394
+ // this.logger.console(
395
+ // `the following environments are not installed yet: ${nonLoadedEnvs.join(', ')}. installing them now...`
396
+ // );
397
+ await this.install(packages, _objectSpread(_objectSpread({}, installOptions), {}, {
398
+ addMissingDeps: installMissing,
399
+ skipIfExisting: true,
400
+ writeConfigFiles: false
401
+ // skipPrune: true,
402
+ }));
403
+ }
404
+ async _addPackages(packages, options) {
405
+ if (options?.lifecycleType === 'dev') {
406
+ throw new (_exceptions().DependencyTypeNotSupportedInPolicy)(options?.lifecycleType);
407
+ }
408
+ this.logger.debug(`installing the following packages: ${packages.join()}`);
409
+ const resolver = await this.dependencyResolver.getVersionResolver();
410
+ const resolvedPackagesP = packages.map(async packageName => {
411
+ try {
412
+ return await resolver.resolveRemoteVersion(packageName, {
413
+ rootDir: this.workspace.path
414
+ });
415
+ } catch (error) {
416
+ if (options?.skipUnavailable) {
417
+ return;
418
+ }
419
+ throw error;
420
+ }
421
+ });
422
+ const resolvedPackages = await Promise.all(resolvedPackagesP);
423
+ const newWorkspacePolicyEntries = [];
424
+ resolvedPackages.forEach(resolvedPackage => {
425
+ if (resolvedPackage?.version) {
426
+ const versionWithPrefix = this.dependencyResolver.getVersionWithSavePrefix({
427
+ version: resolvedPackage.version,
428
+ overridePrefix: options?.savePrefix,
429
+ wantedRange: resolvedPackage.wantedRange
430
+ });
431
+ newWorkspacePolicyEntries.push({
432
+ dependencyId: resolvedPackage.packageName,
433
+ value: {
434
+ version: versionWithPrefix
435
+ },
436
+ lifecycleType: options?.lifecycleType || 'runtime'
437
+ });
438
+ }
439
+ });
440
+ this.dependencyResolver.addToRootPolicy(newWorkspacePolicyEntries, {
441
+ updateExisting: options?.updateExisting ?? false,
442
+ skipIfExisting: options?.skipIfExisting ?? false
443
+ });
444
+ await this.dependencyResolver.persistConfig('install');
445
+ }
446
+ async _installModules(options) {
447
+ const pm = this.dependencyResolver.getPackageManager();
448
+ this.logger.console(`installing dependencies in workspace using ${pm?.name} (${_chalk().default.cyan(this.dependencyResolver.packageManagerName)})`);
449
+ this.logger.debug(`installing dependencies in workspace with options`, options);
450
+ const depsFilterFn = await this.generateFilterFnForDepsFromLocalRemote();
451
+ const hasRootComponents = this.dependencyResolver.hasRootComponents();
452
+ // TODO: pass get install options
453
+ const installer = this.dependencyResolver.getInstaller({});
454
+ const calcManifestsOpts = {
455
+ copyPeerToRuntimeOnComponents: options?.copyPeerToRuntimeOnComponents ?? false,
456
+ copyPeerToRuntimeOnRoot: options?.copyPeerToRuntimeOnRoot ?? true,
457
+ dedupe: !hasRootComponents && options?.dedupe,
458
+ dependencyFilterFn: depsFilterFn,
459
+ nodeLinker: this.dependencyResolver.nodeLinker()
460
+ };
461
+ const linkOpts = {
462
+ linkTeambitBit: true,
463
+ linkCoreAspects: this.dependencyResolver.linkCoreAspects(),
464
+ linkDepsResolvedFromEnv: !hasRootComponents,
465
+ linkNestedDepsInNM: !this.workspace.isLegacy && !hasRootComponents
466
+ };
467
+ const {
468
+ linkedRootDeps
469
+ } = await this.calculateLinks(linkOpts);
470
+ // eslint-disable-next-line prefer-const
471
+ let {
472
+ mergedRootPolicy,
473
+ componentsAndManifests: current
474
+ } = await this._getComponentsManifestsAndRootPolicy(installer, _objectSpread(_objectSpread({}, calcManifestsOpts), {}, {
475
+ addMissingDeps: options?.addMissingDeps,
476
+ skipUnavailable: options?.skipUnavailable,
477
+ linkedRootDeps
478
+ }));
479
+ const pmInstallOptions = _objectSpread(_objectSpread({}, calcManifestsOpts), {}, {
480
+ autoInstallPeers: this.dependencyResolver.config.autoInstallPeers,
481
+ dependenciesGraph: options?.dependenciesGraph,
482
+ includeOptionalDeps: options?.includeOptionalDeps,
483
+ neverBuiltDependencies: this.dependencyResolver.config.neverBuiltDependencies,
484
+ overrides: this.dependencyResolver.config.overrides,
485
+ hoistPatterns: this.dependencyResolver.config.hoistPatterns,
486
+ hoistInjectedDependencies: this.dependencyResolver.config.hoistInjectedDependencies,
487
+ packageImportMethod: this.dependencyResolver.config.packageImportMethod,
488
+ rootComponents: hasRootComponents,
489
+ updateAll: options?.updateAll,
490
+ optimizeReportForNonTerminal: options?.optimizeReportForNonTerminal,
491
+ lockfileOnly: options?.lockfileOnly
492
+ });
493
+ const prevManifests = new Set();
494
+ // TODO: this make duplicate
495
+ // this.logger.consoleSuccess();
496
+ const linkedDependencies = {
497
+ [this.workspace.path]: linkedRootDeps
498
+ };
499
+ const compDirMap = await this.getComponentsDirectory([]);
500
+ let installCycle = 0;
501
+ let hasMissingLocalComponents = true;
502
+ const forcedHarmonyVersion = this.dependencyResolver.harmonyVersionInRootPolicy();
503
+ /* eslint-disable no-await-in-loop */
504
+ do {
505
+ // In case there are missing local components,
506
+ // we'll need to make another round of installation as on the first round the missing local components
507
+ // are not added to the manifests.
508
+ // This is an issue when installation is done using root components.
509
+ hasMissingLocalComponents = hasRootComponents && hasComponentsFromWorkspaceInMissingDeps(current);
510
+ const {
511
+ dependenciesChanged
512
+ } = await installer.installComponents(this.workspace.path, current.manifests, mergedRootPolicy, current.componentDirectoryMap, {
513
+ linkedDependencies,
514
+ installTeambitBit: false,
515
+ forcedHarmonyVersion
516
+ }, pmInstallOptions);
517
+ this.workspace.inInstallAfterPmContext = true;
518
+ let cacheCleared = false;
519
+ await this.linkCodemods(compDirMap);
520
+ const oldNonLoadedEnvs = this.setOldNonLoadedEnvs();
521
+ await this.reloadMovedEnvs();
522
+ await this.reloadNonLoadedEnvs();
523
+ const shouldClearCacheOnInstall = this.shouldClearCacheOnInstall();
524
+ if (options?.compile ?? true) {
525
+ const compileStartTime = process.hrtime();
526
+ const compileOutputMessage = `compiling components`;
527
+ this.logger.setStatusLine(compileOutputMessage);
528
+ if (shouldClearCacheOnInstall) {
529
+ // We need to clear cache before compiling the components or it might compile them with the default env
530
+ // incorrectly in case the env was not loaded correctly before the installation.
531
+ // We don't want to clear the failed to load envs because we want to show the warning at the end
532
+ // await this.workspace.clearCache({ skipClearFailedToLoadEnvs: true });
533
+ await this.workspace.clearCache();
534
+ cacheCleared = true;
535
+ }
536
+ await this.compiler.compileOnWorkspace([], {
537
+ initiator: _compiler().CompilationInitiator.Install
538
+ });
539
+
540
+ // Right now we don't need to load extensions/execute load slot at this point
541
+ // await this.compiler.compileOnWorkspace([], { initiator: CompilationInitiator.Install }, undefined, {
542
+ // executeLoadSlot: true,
543
+ // loadExtensions: true,
544
+ // });
545
+ this.logger.consoleSuccess(compileOutputMessage, compileStartTime);
546
+ }
547
+ if (options?.writeConfigFiles ?? true) {
548
+ await this.tryWriteConfigFiles(!cacheCleared && shouldClearCacheOnInstall);
549
+ }
550
+ if (!dependenciesChanged) break;
551
+ if (!options?.recurringInstall) break;
552
+ if (!oldNonLoadedEnvs.length) break;
553
+ prevManifests.add(manifestsHash(current.manifests));
554
+ // If we run compile we do the clear cache before the compilation so no need to clean it again (it's an expensive
555
+ // operation)
556
+ if (!cacheCleared && shouldClearCacheOnInstall) {
557
+ // We need to clear cache before creating the new component manifests.
558
+ // this.workspace.consumer.componentLoader.clearComponentsCache();
559
+ // We don't want to clear the failed to load envs because we want to show the warning at the end
560
+ await this.workspace.clearCache({
561
+ skipClearFailedToLoadEnvs: true
562
+ });
563
+ }
564
+ current = await this._getComponentsManifests(installer, mergedRootPolicy, calcManifestsOpts);
565
+ installCycle += 1;
566
+ } while ((!prevManifests.has(manifestsHash(current.manifests)) || hasMissingLocalComponents) && installCycle < 5);
567
+ if (!options?.lockfileOnly && !options?.skipPrune) {
568
+ // We clean node_modules only after the last install.
569
+ // Otherwise, we might load an env from a location that we later remove.
570
+ try {
571
+ await installer.pruneModules(this.workspace.path);
572
+ // Ignoring the error here as it's not critical and we don't want to fail the install process
573
+ } catch (err) {
574
+ this.logger.error(`failed running pnpm prune with error`, err);
575
+ }
576
+ // After pruning we need reload moved envs, as during the pruning the old location might be deleted
577
+ await this.reloadMovedEnvs();
578
+ }
579
+ // this is now commented out because we assume we don't need it anymore.
580
+ // even when the env was not loaded before and it is loaded now, it should be fine because the dependencies-data
581
+ // is only about the auto-detect-deps. there are two more steps: version-resolution and apply-overrides that
582
+ // disregard the dependencies-cache.
583
+ // await this.workspace.consumer.componentFsCache.deleteAllDependenciesDataCache();
584
+ /* eslint-enable no-await-in-loop */
585
+ return current.componentDirectoryMap;
586
+ }
587
+ shouldClearCacheOnInstall() {
588
+ const nonLoadedEnvs = this.envs.getFailedToLoadEnvs();
589
+ return nonLoadedEnvs.length > 0;
590
+ }
591
+
592
+ /**
593
+ * This function is very important to fix some issues that might happen during the installation process.
594
+ * The case is the following:
595
+ * during/before the installation process we load some envs from their bit.env files
596
+ * this contains code like:
597
+ * protected tsconfigPath = require.resolve('./config/tsconfig.json');
598
+ * protected eslintConfigPath = require.resolve('./config/eslintrc.cjs');
599
+ * When we load that file, we calculated the resolved path, and it's stored in the env
600
+ * object instance.
601
+ * then later on during the install we move the env to another location (like bit roots)
602
+ * which points to a .pnpm folder with some peers, that changed during the install
603
+ * then when we take this env object and call write ws config for example
604
+ * or compile
605
+ * we use that resolved path to calculate the final tsconfig
606
+ * however that file is no longer exists which result in an error
607
+ * This function will check if an env folder doesn't exist anymore, and will re-load it
608
+ * from its new location.
609
+ * This usually happen when we have install running in the middle of the process followed
610
+ * by other bit ops.
611
+ * examples:
612
+ * bit new - which might run few installs then other ops.
613
+ * bit switch - which might run few installs then other ops, and potentially change the
614
+ * peer deps during the install.
615
+ * bit server (vscode plugin) - which keep the process always live, so any install ops
616
+ * that change the location, will cause the vscode plugin/bit server to crash later.
617
+ * @returns
618
+ */
619
+ async reloadMovedEnvs() {
620
+ this.logger.debug('reloadMovedEnvs');
621
+ const allEnvs = this.envs.getAllRegisteredEnvs();
622
+ const movedEnvs = await (0, _pFilter().default)(allEnvs, async env => {
623
+ if (!env.__path) return false;
624
+ const regularPathExists = await (0, _fsExtra().pathExists)(env.__path);
625
+ const resolvedPathExists = await (0, _fsExtra().pathExists)(env.__resolvedPath);
626
+ return !regularPathExists || !resolvedPathExists;
627
+ });
628
+ const idsToLoad = movedEnvs.map(env => env.id);
629
+ const componentIdsToLoad = idsToLoad.map(id => _component().ComponentID.fromString(id));
630
+ await this.reloadEnvs(componentIdsToLoad);
631
+ }
632
+ async reloadRegisteredEnvs() {
633
+ const allEnvs = this.envs.getAllRegisteredEnvs();
634
+ const idsToLoad = (0, _lodash().compact)(allEnvs.map(env => env.id));
635
+ const componentIdsToLoad = idsToLoad.map(id => _component().ComponentID.fromString(id));
636
+ await this.reloadEnvs(componentIdsToLoad);
637
+ }
638
+ async reloadNonLoadedEnvs() {
639
+ const nonLoadedEnvs = this.envs.getFailedToLoadEnvs();
640
+ const componentIdsToLoad = nonLoadedEnvs.map(id => _component().ComponentID.fromString(id));
641
+ await this.reloadEnvs(componentIdsToLoad);
642
+ }
643
+ async reloadEnvs(componentIdsToLoad) {
644
+ if (componentIdsToLoad.length && this.workspace) {
645
+ const aspects = await this.workspace.resolveAspects(undefined, componentIdsToLoad, {
646
+ requestedOnly: true,
647
+ excludeCore: true,
648
+ throwOnError: false
649
+ // Theoretically we should use skipDeps here, but according to implementation at the moment
650
+ // it will lead to plugins not load, and we need them to be loaded.
651
+ // This is a bug in the flow and should be fixed.
652
+ // skipDeps: true,
653
+ });
654
+ await Promise.all(aspects.map(async aspectDef => {
655
+ const id = aspectDef.component?.id;
656
+ if (!id) return;
657
+ await this.workspace.clearComponentCache(id);
658
+ }));
659
+ await this.reloadAspects(aspects || []);
660
+
661
+ // Keeping this here for now, it was removed as part of #9138 as now that we load envs of envs
662
+ // correctly first it seems to be not needed anymore.
663
+ // But there might be cases where it will be needed. So keeping it here for now.
664
+
665
+ // This is a very special case which we need to compile our envs before loading them correctly.
666
+ // const grouped = groupBy(aspects, (aspectDef) => {
667
+ // return aspectDef.component?.id.toStringWithoutVersion() === 'bitdev.general/envs/bit-env';
668
+ // });
669
+ // await this.reloadAspects(grouped.true || []);
670
+ // const otherEnvs = grouped.false || [];
671
+ // await Promise.all(
672
+ // otherEnvs.map(async (aspectDef) => {
673
+ // const id = aspectDef.component?.id;
674
+ // if (!id) return;
675
+ // await this.workspace.clearComponentCache(id);
676
+ // })
677
+ // );
678
+ // await this.reloadAspects(grouped.false || []);
679
+ }
680
+ }
681
+ async reloadAspects(aspects) {
682
+ const groups = await this.groupAspectsToLoad(aspects);
683
+ // We need to make sure we load group by group and not in parallel
684
+ await (0, _pMapSeries().default)(groups, async group => {
685
+ await this.reloadOneAspectsGroup(group);
686
+ });
687
+ }
688
+ async reloadOneAspectsGroup(group) {
689
+ const aspects = group.aspects || [];
690
+ if (group.workspace && !group.envOfAspect) {
691
+ aspects.forEach(aspectDef => {
692
+ if (aspectDef.component?.id) {
693
+ this.workspace.clearComponentCache(aspectDef.component.id);
694
+ }
695
+ });
696
+ }
697
+ const loadedPlugins = (0, _lodash().compact)(await Promise.all(aspects.map(aspectDef => {
698
+ const localPath = aspectDef.aspectPath;
699
+ const component = aspectDef.component;
700
+ if (!component) return undefined;
701
+ const plugins = this.aspectLoader.getPlugins(component, localPath);
702
+ if (plugins.has()) {
703
+ return plugins.load(_cli().MainRuntime.name);
704
+ }
705
+ })));
706
+ await Promise.all(loadedPlugins.map(plugin => {
707
+ const runtime = plugin.getRuntime(_cli().MainRuntime);
708
+ return runtime?.provider(undefined, undefined, undefined, this.harmony);
709
+ }));
710
+ }
711
+
712
+ /**
713
+ * This function groups the components to aspects to load into groups.
714
+ * The order of the groups is important, the first group should be loaded first.
715
+ * The order inside the group is not important.
716
+ * The groups are:
717
+ * 1. aspects definitions without components (this should be an empty group, if it's not, we should check why).
718
+ * 2. aspects which are not in the workspace but in the scope / node modules.
719
+ * 3. envs of aspects (which are also aspects)
720
+ * 4. other aspects (the rest)
721
+ * @param aspects
722
+ * @returns
723
+ */
724
+ async groupAspectsToLoad(aspects) {
725
+ const groups = (0, _lodash().groupBy)(aspects, aspectDef => {
726
+ if (!aspectDef.component) return 'no-comp';
727
+ if (!this.workspace.hasId(aspectDef.component.id)) return 'scope';
728
+ return 'workspace';
729
+ });
730
+ const workspaceSubGroups = await this.regroupEnvsIdsFromTheList(groups.workspace || []);
731
+ return [{
732
+ comps: false,
733
+ workspace: false,
734
+ aspects: groups.noComp || []
735
+ }, {
736
+ comps: true,
737
+ workspace: false,
738
+ aspects: groups.scope || []
739
+ }, {
740
+ comps: true,
741
+ workspace: true,
742
+ envOfAspect: true,
743
+ aspects: workspaceSubGroups.envOfAspect
744
+ }, {
745
+ comps: true,
746
+ workspace: true,
747
+ aspects: workspaceSubGroups.otherAspects
748
+ }];
749
+ }
750
+ async regroupEnvsIdsFromTheList(aspects) {
751
+ const envsOfAspects = new Set();
752
+ await Promise.all(aspects.map(async aspectDef => {
753
+ if (!aspectDef.component) return;
754
+ const envId = aspectDef.component ? await this.envs.calculateEnvId(aspectDef.component) : undefined;
755
+ if (envId) {
756
+ envsOfAspects.add(envId.toString());
757
+ }
758
+ }));
759
+ const groups = (0, _lodash().groupBy)(aspects, aspectDef => {
760
+ const id = aspectDef.component?.id.toString();
761
+ const idWithoutVersion = aspectDef.component?.id.toStringWithoutVersion();
762
+ if (id && envsOfAspects.has(id) || idWithoutVersion && envsOfAspects.has(idWithoutVersion)) {
763
+ return 'envOfAspect';
764
+ }
765
+ return 'otherAspects';
766
+ });
767
+ return groups;
768
+ }
769
+ async _getComponentsManifestsAndRootPolicy(installer, options) {
770
+ const mergedRootPolicy = await this.addConfiguredAspectsToWorkspacePolicy();
771
+ await this.addConfiguredGeneratorEnvsToWorkspacePolicy(mergedRootPolicy);
772
+ const componentsAndManifests = await this._getComponentsManifests(installer, mergedRootPolicy, options);
773
+ if (!options?.addMissingDeps) {
774
+ return {
775
+ componentsAndManifests,
776
+ mergedRootPolicy
777
+ };
778
+ }
779
+ const rootDeps = new Set(Object.keys(_objectSpread(_objectSpread(_objectSpread({}, componentsAndManifests.manifests[this.workspace.path].devDependencies), componentsAndManifests.manifests[this.workspace.path].dependencies), options.linkedRootDeps)));
780
+ Object.values((0, _lodash().omit)(componentsAndManifests.manifests, [this.workspace.path])).forEach(manifest => {
781
+ if (manifest.name) {
782
+ rootDeps.add(manifest.name); // eslint-disable-line @typescript-eslint/no-non-null-assertion
783
+ }
784
+ });
785
+ const addedNewPkgs = await this._addMissingPackagesToRootPolicy(rootDeps, {
786
+ skipUnavailable: options?.skipUnavailable
787
+ });
788
+ if (!addedNewPkgs) {
789
+ return {
790
+ componentsAndManifests,
791
+ mergedRootPolicy
792
+ };
793
+ }
794
+ const mergedRootPolicyWithMissingDeps = await this.addConfiguredAspectsToWorkspacePolicy();
795
+ await this.addConfiguredGeneratorEnvsToWorkspacePolicy(mergedRootPolicyWithMissingDeps);
796
+ return {
797
+ mergedRootPolicy: mergedRootPolicyWithMissingDeps,
798
+ componentsAndManifests: await this._getComponentsManifests(installer, mergedRootPolicyWithMissingDeps, options)
799
+ };
800
+ }
801
+
802
+ /**
803
+ * The function `tryWriteConfigFiles` attempts to write workspace config files, and if it fails, it logs an error
804
+ * message.
805
+ * @returns If the condition `!shouldWrite` is true, then nothing is being returned. Otherwise, if the `writeConfigFiles`
806
+ * function is successfully executed, nothing is being returned. If an error occurs during the execution of
807
+ * `writeConfigFiles`, an error message is being returned.
808
+ */
809
+ async tryWriteConfigFiles(clearCache) {
810
+ const shouldWrite = this.wsConfigFiles.isWorkspaceConfigWriteEnabled();
811
+ if (!shouldWrite) return;
812
+ if (clearCache) {
813
+ await this.workspace.clearCache({
814
+ skipClearFailedToLoadEnvs: true
815
+ });
816
+ }
817
+ const {
818
+ err
819
+ } = await this.wsConfigFiles.writeConfigFiles({
820
+ clean: true,
821
+ silent: true,
822
+ dedupe: true,
823
+ throw: false
824
+ });
825
+ if (err) {
826
+ this.logger.consoleFailure(`failed generating workspace config files, please run "bit ws-config write" manually. error: ${err.message}`);
827
+ }
828
+ }
829
+ async addConfiguredAspectsToWorkspacePolicy() {
830
+ const rootPolicy = this.dependencyResolver.getWorkspacePolicy();
831
+ const aspectsPackages = await this.workspace.getConfiguredUserAspectsPackages({
832
+ externalsOnly: true
833
+ });
834
+ aspectsPackages.forEach(aspectsPackage => {
835
+ rootPolicy.add({
836
+ dependencyId: aspectsPackage.packageName,
837
+ value: {
838
+ version: aspectsPackage.version
839
+ },
840
+ lifecycleType: 'runtime'
841
+ },
842
+ // If it's already exist from the root, take the version from the root policy
843
+ {
844
+ skipIfExisting: true
845
+ });
846
+ });
847
+ return rootPolicy;
848
+ }
849
+ async addConfiguredGeneratorEnvsToWorkspacePolicy(rootPolicy) {
850
+ const configuredEnvs = this.generator.getConfiguredEnvs();
851
+ const resolvedEnvs = (0, _lodash().compact)(await Promise.all(configuredEnvs.map(async envIdStr => {
852
+ if (this.envs.isCoreEnv(envIdStr)) {
853
+ return undefined;
854
+ }
855
+ const parsedId = await this.workspace.resolveComponentId(envIdStr);
856
+ // If we have the env in the workspace, we don't want to install it
857
+ const inWs = await this.workspace.hasId(parsedId);
858
+ if (inWs) {
859
+ return undefined;
860
+ }
861
+ const comps = await this.workspace.importAndGetMany([parsedId], `to get the env ${parsedId.toString()} for installation`);
862
+ const idWithVersion = await this.workspace.resolveEnvIdWithPotentialVersionForConfig(parsedId);
863
+ const version = idWithVersion.split('@')[1] || '*';
864
+ const packageName = this.dependencyResolver.getPackageName(comps[0]);
865
+ return {
866
+ packageName,
867
+ version
868
+ };
869
+ })));
870
+ resolvedEnvs.forEach(env => {
871
+ rootPolicy.add({
872
+ dependencyId: env.packageName,
873
+ value: {
874
+ version: env.version
875
+ },
876
+ lifecycleType: 'runtime'
877
+ },
878
+ // If it's already exist from the root, take the version from the root policy
879
+ {
880
+ skipIfExisting: true
881
+ });
882
+ });
883
+ }
884
+ async _addMissingPackagesToRootPolicy(rootDeps, options) {
885
+ const packages = await this._getMissingPackagesWithoutRootDeps(rootDeps);
886
+ if (packages && packages.length) {
887
+ await this._addPackages(packages, options);
888
+ }
889
+ return packages.length > 0;
890
+ }
891
+ async _getMissingPackagesWithoutRootDeps(rootDeps) {
892
+ const packages = await this._getAllMissingPackages();
893
+ return packages.filter(pkg => !rootDeps.has(pkg));
894
+ }
895
+ async _getAllMissingPackages() {
896
+ const comps = await this.workspace.list();
897
+ return (0, _lodash().uniq)(comps.map(comp => {
898
+ const data = comp.state.issues.getIssue(_componentIssues().IssuesClasses.MissingPackagesDependenciesOnFs)?.data || [];
899
+ return data.map(d => d.missingPackages).flat();
900
+ }).flat());
901
+ }
902
+ async _getComponentsManifests(dependencyInstaller, rootPolicy, installOptions) {
903
+ const componentDirectoryMap = await this.getComponentsDirectory([]);
904
+ let manifests = await dependencyInstaller.getComponentManifests(_objectSpread(_objectSpread({}, installOptions), {}, {
905
+ componentDirectoryMap,
906
+ rootPolicy,
907
+ rootDir: this.workspace.path,
908
+ referenceLocalPackages: this.dependencyResolver.hasRootComponents() && installOptions.nodeLinker === 'isolated'
909
+ }));
910
+ if (this.dependencyResolver.hasRootComponents()) {
911
+ const rootManifests = await this._getRootManifests(manifests);
912
+ await this._updateRootDirs(Object.keys(rootManifests));
913
+ manifests = _objectSpread(_objectSpread({}, manifests), rootManifests);
914
+ }
915
+ return {
916
+ componentDirectoryMap,
917
+ manifests
918
+ };
919
+ }
920
+ setOldNonLoadedEnvs() {
921
+ const nonLoadedEnvs = this.envs.getFailedToLoadEnvs();
922
+ const envsWithoutManifest = Array.from(this.dependencyResolver.envsWithoutManifest);
923
+ const oldNonLoadedEnvs = (0, _lodash().intersection)(nonLoadedEnvs, envsWithoutManifest);
924
+ this.oldNonLoadedEnvs = oldNonLoadedEnvs;
925
+ return oldNonLoadedEnvs;
926
+ }
927
+
928
+ /**
929
+ * This function returns a list of old non-loaded environments names.
930
+ * @returns an array of strings called `oldNonLoadedEnvs`. This array contains the names of environment variables that
931
+ * failed to load as extensions and are also don't have an env.jsonc file.
932
+ * If this list is not empty, then the user might need to run another install to make sure all dependencies resolved
933
+ * correctly
934
+ */
935
+ getOldNonLoadedEnvs() {
936
+ return this.oldNonLoadedEnvs;
937
+ }
938
+ async _updateRootDirs(rootDirs) {
939
+ try {
940
+ const existingDirs = await _fsExtra().default.readdir(this.workspace.rootComponentsPath);
941
+ await Promise.all(existingDirs.map(async dirName => {
942
+ const dirPath = _path().default.join(this.workspace.rootComponentsPath, dirName);
943
+ if (!rootDirs.includes(dirPath)) {
944
+ await _fsExtra().default.remove(dirPath);
945
+ }
946
+ }));
947
+ } catch (err) {
948
+ if (err.code !== 'ENOENT') throw err;
949
+ }
950
+ await Promise.all(rootDirs.map(dirPath => _fsExtra().default.mkdir(dirPath, {
951
+ recursive: true
952
+ })));
953
+ }
954
+ async _getRootManifests(manifests) {
955
+ const nonRootManifests = Object.values(manifests).filter(({
956
+ name
957
+ }) => name !== 'workspace');
958
+ const workspaceDeps = this.dependencyResolver.getWorkspaceDepsOfBitRoots(nonRootManifests);
959
+ const workspaceDepsMeta = Object.keys(workspaceDeps).reduce((acc, depName) => {
960
+ acc[depName] = {
961
+ injected: true
962
+ };
963
+ return acc;
964
+ }, {});
965
+ const envManifests = await this._getEnvManifests(workspaceDeps, workspaceDepsMeta);
966
+ const appManifests = await this._getAppManifests(manifests, workspaceDeps, workspaceDepsMeta);
967
+ return _objectSpread(_objectSpread({}, envManifests), appManifests);
968
+ }
969
+ async _getEnvManifests(workspaceDeps, workspaceDepsMeta) {
970
+ const envs = await this._getAllUsedEnvIds();
971
+ return Object.fromEntries(await Promise.all(envs.map(async envId => {
972
+ return [await this.getRootComponentDirByRootId(this.workspace.rootComponentsPath, envId), {
973
+ dependencies: _objectSpread(_objectSpread(_objectSpread({}, await this._getEnvDependencies(envId)), workspaceDeps), await this._getEnvPackage(envId)),
974
+ dependenciesMeta: workspaceDepsMeta,
975
+ installConfig: {
976
+ hoistingLimits: 'workspaces'
977
+ }
978
+ }];
979
+ })));
980
+ }
981
+ async _getEnvDependencies(envId) {
982
+ const policy = await this.dependencyResolver.getEnvPolicyFromEnvId(envId);
983
+ if (!policy) return {};
984
+ return Object.fromEntries(policy.selfPolicy.entries.filter(({
985
+ force,
986
+ value
987
+ }) => force && value.version !== '-').map(({
988
+ dependencyId,
989
+ value
990
+ }) => [dependencyId, value.version]));
991
+ }
992
+
993
+ /**
994
+ * Return the package name of the env with its version.
995
+ * (only if the env is not a core env and is not in the workspace)
996
+ * @param envId
997
+ * @returns
998
+ */
999
+ async _getEnvPackage(envId) {
1000
+ if (this.envs.isCoreEnv(envId.toStringWithoutVersion())) return undefined;
1001
+ const inWs = await this.workspace.hasId(envId);
1002
+ if (inWs) return undefined;
1003
+ const envComponent = await this.envs.getEnvComponentByEnvId(envId.toString(), envId.toString());
1004
+ if (!envComponent) return undefined;
1005
+ const packageName = this.dependencyResolver.getPackageName(envComponent);
1006
+ const version = envId.version;
1007
+ const finalVersion = (0, _componentPackageVersion().snapToSemver)(version);
1008
+ return {
1009
+ [packageName]: finalVersion
1010
+ };
1011
+ }
1012
+ async _getAppManifests(manifests, workspaceDeps, workspaceDepsMeta) {
1013
+ return Object.fromEntries((0, _lodash().compact)(await Promise.all((await this.app.listAppsComponents()).map(async app => {
1014
+ const appPkgName = this.dependencyResolver.getPackageName(app);
1015
+ const appManifest = Object.values(manifests).find(({
1016
+ name
1017
+ }) => name === appPkgName);
1018
+ if (!appManifest) return null;
1019
+ const envId = await this.envs.calculateEnvId(app);
1020
+ return [await this.getRootComponentDirByRootId(this.workspace.rootComponentsPath, app.id), _objectSpread(_objectSpread({}, (0, _lodash().omit)(appManifest, ['name', 'version'])), {}, {
1021
+ dependencies: _objectSpread(_objectSpread(_objectSpread({}, await this._getEnvDependencies(envId)), appManifest.dependencies), workspaceDeps),
1022
+ dependenciesMeta: _objectSpread(_objectSpread({}, appManifest.dependenciesMeta), workspaceDepsMeta),
1023
+ installConfig: {
1024
+ hoistingLimits: 'workspaces'
1025
+ }
1026
+ })];
1027
+ }))));
1028
+ }
1029
+ async _getAllUsedEnvIds() {
1030
+ const envs = new Map();
1031
+ const components = await this.workspace.list();
1032
+ await (0, _pMapSeries().default)(components, async component => {
1033
+ const envId = await this.envs.calculateEnvId(component);
1034
+ envs.set(envId.toString(), envId);
1035
+ });
1036
+ return Array.from(envs.values());
1037
+ }
1038
+
1039
+ /**
1040
+ * Updates out-of-date dependencies in the workspace.
1041
+ *
1042
+ * @param options.all {Boolean} updates all outdated dependencies without showing a prompt.
1043
+ */
1044
+ async updateDependencies(options) {
1045
+ const componentPolicies = await this.workspace.getComponentsWithDependencyPolicies();
1046
+ const variantPoliciesByPatterns = this.workspace.variantPatternsToDepPolicesDict();
1047
+ const components = await this.workspace.list();
1048
+ const outdatedPkgs = await this.dependencyResolver.getOutdatedPkgsFromPolicies({
1049
+ rootDir: this.workspace.path,
1050
+ variantPoliciesByPatterns,
1051
+ componentPolicies,
1052
+ components,
1053
+ patterns: options.patterns,
1054
+ forceVersionBump: options.forceVersionBump
1055
+ });
1056
+ if (outdatedPkgs == null) {
1057
+ this.logger.consoleFailure('No dependencies found that match the patterns');
1058
+ return null;
1059
+ }
1060
+ let outdatedPkgsToUpdate;
1061
+ if (options.all) {
1062
+ outdatedPkgsToUpdate = outdatedPkgs;
1063
+ } else {
1064
+ this.logger.off();
1065
+ outdatedPkgsToUpdate = await (0, _pickOutdatedPkgs().pickOutdatedPkgs)(outdatedPkgs);
1066
+ this.logger.on();
1067
+ }
1068
+ if (outdatedPkgsToUpdate.length === 0) {
1069
+ this.logger.consoleSuccess('No outdated dependencies found');
1070
+ if (options.forceVersionBump === 'compatible') {
1071
+ this.logger.console("If you want to find new versions that don't match the current version ranges, retry with the --latest flag");
1072
+ }
1073
+ return null;
1074
+ }
1075
+ const {
1076
+ updatedVariants,
1077
+ updatedComponents
1078
+ } = this.dependencyResolver.applyUpdates(outdatedPkgsToUpdate, {
1079
+ variantPoliciesByPatterns
1080
+ });
1081
+ await this._updateVariantsPolicies(updatedVariants);
1082
+ await this._updateComponentsConfig(updatedComponents);
1083
+ await this.workspace._reloadConsumer();
1084
+ return this._installModules({
1085
+ dedupe: true
1086
+ });
1087
+ }
1088
+ async addDuplicateComponentAndPackageIssue(components) {
1089
+ const workspacePolicy = this.dependencyResolver.getWorkspacePolicy();
1090
+ components.forEach(component => {
1091
+ if (component.state._consumer.removed) return;
1092
+ const pkgName = (0, _pkgModules().componentIdToPackageName)(component.state._consumer);
1093
+ const found = workspacePolicy.find(pkgName);
1094
+ if (found) {
1095
+ component.state.issues.getOrCreate(_componentIssues().IssuesClasses.DuplicateComponentAndPackage).data = found.dependencyId;
1096
+ }
1097
+ });
1098
+ }
1099
+ async _updateComponentsConfig(updatedComponents) {
1100
+ if (updatedComponents.length === 0) return;
1101
+ await Promise.all(updatedComponents.map(({
1102
+ componentId,
1103
+ config
1104
+ }) => {
1105
+ return this.workspace.addSpecificComponentConfig(componentId, _dependencyResolver().DependencyResolverAspect.id, config, {
1106
+ shouldMergeWithExisting: true,
1107
+ shouldMergeWithPrevious: true
1108
+ });
1109
+ }));
1110
+ await this.workspace.bitMap.write('update (dependencies)');
1111
+ }
1112
+ async _updateVariantsPolicies(updateVariantPolicies) {
1113
+ const variantPatterns = this.variants.raw();
1114
+ for (const variantPattern of updateVariantPolicies) {
1115
+ this.variants.setExtension(variantPattern, _dependencyResolver().DependencyResolverAspect.id, variantPatterns[variantPattern][_dependencyResolver().DependencyResolverAspect.id], {
1116
+ overrideExisting: true
1117
+ });
1118
+ }
1119
+ await this.dependencyResolver.persistConfig('update dependencies');
1120
+ }
1121
+
1122
+ /**
1123
+ * Uninstall the specified packages from dependencies.
1124
+ *
1125
+ * @param {string[]} the list of packages that should be removed from dependencies.
1126
+ */
1127
+ async uninstallDependencies(packages) {
1128
+ this.dependencyResolver.removeFromRootPolicy(packages);
1129
+ await this.dependencyResolver.persistConfig('uninstall dependencies');
1130
+ return this._installModules({
1131
+ dedupe: true
1132
+ });
1133
+ }
1134
+
1135
+ /**
1136
+ * This function returns all the locations of the external links that should be created inside node_modules.
1137
+ * This information may then be passed to the package manager, which will create the links on its own.
1138
+ */
1139
+ async calculateLinks(options = {}) {
1140
+ await (0, _pMapSeries().default)(this.preLinkSlot.values(), fn => fn(options)); // import objects if not disabled in options
1141
+ const compDirMap = await this.getComponentsDirectory([]);
1142
+ const linker = this.dependencyResolver.getLinker({
1143
+ rootDir: this.workspace.path,
1144
+ linkingOptions: options
1145
+ });
1146
+ const {
1147
+ linkResults: res,
1148
+ linkedRootDeps
1149
+ } = await linker.calculateLinkedDeps(this.workspace.path, compDirMap, options);
1150
+ const workspaceRes = res;
1151
+ const legacyResults = await this.linkCodemods(compDirMap, options);
1152
+ workspaceRes.legacyLinkResults = legacyResults.linksResults;
1153
+ workspaceRes.legacyLinkCodemodResults = legacyResults.codemodResults;
1154
+ if (this.dependencyResolver.hasRootComponents() && options.linkToBitRoots) {
1155
+ await this._linkAllComponentsToBitRoots(compDirMap);
1156
+ }
1157
+ return {
1158
+ linkResults: res,
1159
+ linkedRootDeps
1160
+ };
1161
+ }
1162
+ async linkCodemods(compDirMap, options) {
1163
+ const bitIds = compDirMap.toArray().map(([component]) => component.id);
1164
+ return (0, _workspaceModules().linkToNodeModulesWithCodemod)(this.workspace, bitIds, options?.rewire ?? false);
1165
+ }
1166
+ async link(options = {}) {
1167
+ const {
1168
+ linkResults,
1169
+ linkedRootDeps
1170
+ } = await this.calculateLinks(options);
1171
+ await (0, _dependenciesFs().createLinks)(options.linkToDir ?? this.workspace.path, linkedRootDeps, {
1172
+ avoidHardLink: true,
1173
+ skipIfSymlinkValid: true
1174
+ });
1175
+ return linkResults;
1176
+ }
1177
+ async _linkAllComponentsToBitRoots(compDirMap) {
1178
+ const envs = await this._getAllUsedEnvIds();
1179
+ const apps = (await this.app.listAppsComponents()).map(component => component.id);
1180
+ await Promise.all([...envs, ...apps].map(async id => {
1181
+ const dir = await this.getRootComponentDirByRootId(this.workspace.rootComponentsPath, id);
1182
+ await _fsExtra().default.mkdirp(dir);
1183
+ }));
1184
+ await (0, _workspace().linkPkgsToRootComponents)({
1185
+ rootComponentsPath: this.workspace.rootComponentsPath,
1186
+ workspacePath: this.workspace.path
1187
+ }, compDirMap.components.map(component => this.dependencyResolver.getPackageName(component)));
1188
+ }
1189
+ async getRootComponentDirByRootId(rootComponentsPath, rootComponentId) {
1190
+ // Root directories for local envs and apps are created without their version number.
1191
+ // This is done in order to avoid changes to the lockfile after such components are tagged.
1192
+ const id = this.workspace.hasId(rootComponentId) ? rootComponentId.toStringWithoutVersion() : rootComponentId.toString();
1193
+ return (0, _workspace().getRootComponentDir)(rootComponentsPath, id);
1194
+ }
1195
+
1196
+ /**
1197
+ * Generate a filter to pass to the installer
1198
+ * This will filter deps which are come from remotes which defined in scope.json
1199
+ * those components comes from local remotes, usually doesn't have a package in a registry
1200
+ * so no reason to try to install them (it will fail)
1201
+ */
1202
+ async generateFilterFnForDepsFromLocalRemote() {
1203
+ const remotes = await this.workspace.scope.getRemoteScopes();
1204
+ const reg = await this.dependencyResolver.getRegistries();
1205
+ const packageScopes = Object.keys(reg.scopes);
1206
+ return dependencyList => {
1207
+ const filtered = dependencyList.filter(dep => {
1208
+ if (!(dep instanceof _dependencyResolver().ComponentDependency)) {
1209
+ return true;
1210
+ }
1211
+ if (remotes.isHub(dep.componentId.scope)) {
1212
+ return true;
1213
+ }
1214
+ if (packageScopes.some(scope => dep.packageName.startsWith(`@${scope}/`))) {
1215
+ return true;
1216
+ }
1217
+ return false;
1218
+ });
1219
+ return filtered;
1220
+ };
1221
+ }
1222
+ async getComponentsDirectory(ids) {
1223
+ const components = ids.length ? await this.workspace.getMany(ids) : await this.workspace.list(undefined, {
1224
+ loadSeedersAsAspects: false
1225
+ });
1226
+ return _component().ComponentMap.as(components, component => this.workspace.componentDir(component.id));
1227
+ }
1228
+ async onRootAspectAddedSubscriber(_aspectId, inWs) {
1229
+ if (!inWs) {
1230
+ await this.install();
1231
+ }
1232
+ }
1233
+ async onAspectsResolveSubscriber(aspectComponents) {
1234
+ let needLink = false;
1235
+ let needInstall = false;
1236
+ const promises = aspectComponents.map(async aspectComponent => {
1237
+ const aspectIdStr = aspectComponent.id.toString();
1238
+ if (this.visitedAspects.has(aspectIdStr)) return;
1239
+ this.visitedAspects.add(aspectIdStr);
1240
+ const packagePath = await this.workspace.getComponentPackagePath(aspectComponent);
1241
+ const exists = await (0, _fsExtra().pathExists)(packagePath);
1242
+ if (!exists) {
1243
+ const inWs = await this.workspace.hasId(aspectComponent.id);
1244
+ if (inWs) {
1245
+ needLink = true;
1246
+ } else {
1247
+ needInstall = true;
1248
+ }
1249
+ }
1250
+ });
1251
+ await Promise.all(promises);
1252
+ if (needInstall) {
1253
+ await this.install();
1254
+ return;
1255
+ }
1256
+ if (needLink) {
1257
+ await this.link();
1258
+ }
1259
+ }
1260
+ async onComponentChange(component) {
1261
+ const isEnv = this.envs.isEnv(component);
1262
+ if (isEnv) {
1263
+ await this.reloadEnvs([component.id]);
1264
+ }
1265
+ }
1266
+ static async provider([dependencyResolver, workspace, loggerExt, variants, cli, compiler, issues, envs, app, ipcEvents, generator, wsConfigFiles, aspectLoader, bundler, ui], _, [preLinkSlot, preInstallSlot, postInstallSlot], harmony) {
1267
+ const logger = loggerExt.createLogger(_install().InstallAspect.id);
1268
+ const installExt = new InstallMain(dependencyResolver, logger, workspace, variants, compiler, envs, wsConfigFiles, aspectLoader, app, generator, preLinkSlot, preInstallSlot, postInstallSlot, ipcEvents, harmony);
1269
+ ipcEvents.registerGotEventSlot(async eventName => {
1270
+ if (eventName !== 'onPostInstall') return;
1271
+ logger.debug('got onPostInstall event, clear workspace and all components cache');
1272
+ await workspace.clearCache();
1273
+ await installExt.reloadMovedEnvs();
1274
+ await (0, _pMapSeries().default)(postInstallSlot.values(), fn => fn());
1275
+ });
1276
+ if (issues) {
1277
+ issues.registerAddComponentsIssues(installExt.addDuplicateComponentAndPackageIssue.bind(installExt));
1278
+ }
1279
+ generator.registerOnComponentCreate(installExt.onComponentCreate.bind(installExt));
1280
+ const commands = [new (_install2().default)(installExt, workspace, logger), new (_uninstall().default)(installExt), new (_update().default)(installExt), new (_link().LinkCommand)(installExt, workspace, logger)];
1281
+ // For now do not automate installation during aspect resolving
1282
+ // workspace.registerOnAspectsResolve(installExt.onAspectsResolveSubscriber.bind(installExt));
1283
+ if (workspace) {
1284
+ workspace.registerOnRootAspectAdded(installExt.onRootAspectAddedSubscriber.bind(installExt));
1285
+ workspace.registerOnComponentChange(installExt.onComponentChange.bind(installExt));
1286
+ }
1287
+ installExt.registerPostInstall(async () => {
1288
+ if (!ui.getUIServer()) {
1289
+ return;
1290
+ }
1291
+ const components = await workspace.list();
1292
+ await bundler.addNewDevServers(components);
1293
+ });
1294
+ cli.register(...commands);
1295
+ return installExt;
1296
+ }
1297
+ async handleExternalPackageManagerPrompt() {
1298
+ this.logger.clearStatusLine();
1299
+ const question = `Hi, I noticed you use external package manager mode. Do you want to use Bit's package manager instead? [yes(y)/no(n)]`;
1300
+ const shouldSwitchToBitPM = await (0, _yesno().default)({
1301
+ question
1302
+ });
1303
+ if (!shouldSwitchToBitPM) {
1304
+ throw new Error('External package manager mode is enabled. Please use your preferred package manager (npm, yarn, pnpm) to install dependencies instead of "bit install".');
1305
+ }
1306
+
1307
+ // User chose to switch to Bit's package manager
1308
+ await this.disableExternalPackageManagerMode();
1309
+ }
1310
+ async disableExternalPackageManagerMode() {
1311
+ try {
1312
+ // Get the workspace config
1313
+ const workspaceConfig = this.workspace.getWorkspaceConfig();
1314
+
1315
+ // Remove externalPackageManager property
1316
+ const workspaceExtConfig = workspaceConfig.extensions.findExtension(_workspace2().WorkspaceAspect.id);
1317
+ if (workspaceExtConfig?.config.externalPackageManager) {
1318
+ delete workspaceExtConfig.config.externalPackageManager;
1319
+ }
1320
+
1321
+ // Restore default settings
1322
+ const depResolverExt = workspaceConfig.extensions.findExtension('teambit.dependencies/dependency-resolver');
1323
+ if (depResolverExt) {
1324
+ depResolverExt.config.rootComponent = true;
1325
+ }
1326
+
1327
+ // Enable workspace config write
1328
+ const workspaceConfigFilesExt = workspaceConfig.extensions.findExtension('teambit.workspace/workspace-config-files');
1329
+ if (workspaceConfigFilesExt) {
1330
+ workspaceConfigFilesExt.config.enableWorkspaceConfigWrite = true;
1331
+ }
1332
+
1333
+ // Remove postInstall script from package.json (preserve user's existing scripts)
1334
+ await this.removePostInstallScript();
1335
+
1336
+ // Write the updated config
1337
+ await workspaceConfig.write();
1338
+ this.logger.console(_chalk().default.green('✓ Successfully switched to Bit package manager mode'));
1339
+ } catch (error) {
1340
+ this.logger.console(_chalk().default.red('✗ Failed to switch to Bit package manager mode'));
1341
+ throw error;
1342
+ }
1343
+ }
1344
+ async removePostInstallScript() {
1345
+ try {
1346
+ const packageJsonPath = _path().default.join(this.workspace.path, 'package.json');
1347
+ const packageJsonExists = await (0, _fsExtra().pathExists)(packageJsonPath);
1348
+ if (!packageJsonExists) {
1349
+ return;
1350
+ }
1351
+ const packageJson = await _fsExtra().default.readJson(packageJsonPath);
1352
+
1353
+ // Only remove our specific postInstall script, preserve user's custom scripts
1354
+ if (packageJson.scripts?.postinstall === 'bit link && bit compile') {
1355
+ delete packageJson.scripts.postinstall;
1356
+
1357
+ // Clean up empty scripts object
1358
+ if (Object.keys(packageJson.scripts).length === 0) {
1359
+ delete packageJson.scripts;
1360
+ }
1361
+ await _fsExtra().default.writeJson(packageJsonPath, packageJson, {
1362
+ spaces: 2
1363
+ });
1364
+ }
1365
+ } catch {
1366
+ this.logger.console(_chalk().default.yellow('⚠ Warning: Could not remove postInstall script from package.json'));
1367
+ }
1368
+ }
1369
+ }
1370
+ exports.InstallMain = InstallMain;
1371
+ _defineProperty(InstallMain, "slots", [_harmony().Slot.withType(), _harmony().Slot.withType(), _harmony().Slot.withType()]);
1372
+ _defineProperty(InstallMain, "dependencies", [_dependencyResolver().DependencyResolverAspect, _workspace2().WorkspaceAspect, _logger().LoggerAspect, _variants().VariantsAspect, _cli().CLIAspect, _compiler().CompilerAspect, _issues().IssuesAspect, _envs().EnvsAspect, _application().ApplicationAspect, _ipcEvents().IpcEventsAspect, _generator().GeneratorAspect, _workspaceConfigFiles().WorkspaceConfigFilesAspect, _aspectLoader().AspectLoaderAspect, _bundler().BundlerAspect, _ui().UIAspect]);
1373
+ _defineProperty(InstallMain, "runtime", _cli().MainRuntime);
1374
+ function hasComponentsFromWorkspaceInMissingDeps({
1375
+ componentDirectoryMap,
1376
+ manifests
1377
+ }) {
1378
+ const missingDeps = new Set(componentDirectoryMap.toArray().map(([{
1379
+ state
1380
+ }]) => {
1381
+ const issue = state.issues.getIssue(_componentIssues().IssuesClasses.MissingPackagesDependenciesOnFs);
1382
+ if (!issue) return [];
1383
+ return issue.data.map(d => d.missingPackages).flat();
1384
+ }).flat());
1385
+ return Object.values(manifests).some(({
1386
+ name
1387
+ }) => name && missingDeps.has(name));
1388
+ }
1389
+ _install().InstallAspect.addRuntime(InstallMain);
1390
+ var _default = exports.default = InstallMain;
1391
+ function manifestsHash(manifests) {
1392
+ // We don't care if the type of the dependency changes as it doesn't change the node_modules structure
1393
+ const depsByProjectPaths = (0, _lodash().mapValues)(manifests, manifest => _objectSpread(_objectSpread({}, manifest.devDependencies), manifest.dependencies));
1394
+ return (0, _objectHash().default)(depsByProjectPaths);
1395
+ }
1396
+
1397
+ //# sourceMappingURL=install.main.runtime.js.map