@teambit/isolator 1.0.411 → 1.0.412

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,17 +1,14 @@
1
1
  import { CLIMain } from '@teambit/cli';
2
2
  import { AspectLoaderMain } from '@teambit/aspect-loader';
3
- import { Component } from '@teambit/component';
4
3
  import type { ComponentMain, ComponentFactory } from '@teambit/component';
5
4
  import { GraphMain } from '@teambit/graph';
6
5
  import { SlotRegistry } from '@teambit/harmony';
7
6
  import { DependencyResolverMain, LinkingOptions, NodeLinker } from '@teambit/dependency-resolver';
8
7
  import { Logger, LoggerMain } from '@teambit/logger';
9
- import { ComponentID, ComponentIdList } from '@teambit/component-id';
8
+ import { ComponentID } from '@teambit/component-id';
10
9
  import LegacyScope from '@teambit/legacy/dist/scope/scope';
11
10
  import { GlobalConfigMain } from '@teambit/global-config';
12
- import { DataToPersist } from '@teambit/component.sources';
13
11
  import { PathOsBasedAbsolute } from '@teambit/legacy.utils';
14
- import { Scope } from '@teambit/legacy/dist/scope';
15
12
  import { Capsule } from './capsule';
16
13
  import { Network } from './network';
17
14
  export type ListResults = {
@@ -127,6 +124,12 @@ export type IsolateComponentsOptions = CreateGraphOptions & {
127
124
  * the idea is to have all build artifacts from the previous snap and run deploy pipeline on top of it.
128
125
  */
129
126
  populateArtifactsFrom?: ComponentID[];
127
+ /**
128
+ * relevant when populateArtifactsFrom is set.
129
+ * by default, it uses the package.json created in the previous snap as a base and make the necessary changes.
130
+ * if this is set to true, it will ignore the package.json from the previous snap.
131
+ */
132
+ populateArtifactsIgnorePkgJson?: boolean;
130
133
  /**
131
134
  * Force specific host to get the component from.
132
135
  */
@@ -252,7 +255,9 @@ export declare class IsolatorMain {
252
255
  private getCapsulesPreviousPackageJson;
253
256
  private updateWithCurrentPackageJsonData;
254
257
  private getCurrentPackageJson;
255
- populateComponentsFilesToWriteForCapsule(component: Component, ids: ComponentIdList, legacyScope?: Scope, opts?: IsolateComponentsOptions): Promise<DataToPersist>;
258
+ private populateComponentsFilesToWriteForCapsule;
259
+ private mergePkgJsonFromLastBuild;
260
+ private getCompForArtifacts;
256
261
  private preparePackageJsonToWrite;
257
262
  /**
258
263
  * currently, it writes all artifacts.
@@ -780,11 +780,18 @@ class IsolatorMain {
780
780
  const legacyComponents = [...legacyUnmodifiedComps, ...legacyModifiedComps];
781
781
  if (legacyScope && unmodifiedComps.length) await (0, _component2().importMultipleDistsArtifacts)(legacyScope, legacyUnmodifiedComps);
782
782
  const allIds = _componentId().ComponentIdList.fromArray(legacyComponents.map(c => c.id));
783
+ const getParentsComp = () => {
784
+ const artifactsFrom = opts?.populateArtifactsFrom;
785
+ if (!artifactsFrom) return undefined;
786
+ if (!legacyScope) throw new Error('populateArtifactsFrom is set but legacyScope is not defined');
787
+ return Promise.all(artifactsFrom.map(id => legacyScope.getConsumerComponent(id)));
788
+ };
789
+ const populateArtifactsFromComps = await getParentsComp();
783
790
  await Promise.all(components.map(async component => {
784
791
  const capsule = capsuleList.getCapsule(component.id);
785
792
  if (!capsule) return;
786
793
  const scope = (await _capsuleList().default.capsuleUsePreviouslySavedDists(component)) || opts?.populateArtifactsFrom ? legacyScope : undefined;
787
- const dataToPersist = await this.populateComponentsFilesToWriteForCapsule(component, allIds, scope, opts);
794
+ const dataToPersist = await this.populateComponentsFilesToWriteForCapsule(component, allIds, scope, opts, populateArtifactsFromComps);
788
795
  await dataToPersist.persistAllToCapsule(capsule, {
789
796
  keepExistingCapsule: true
790
797
  });
@@ -968,7 +975,7 @@ class IsolatorMain {
968
975
  packageJson.addOrUpdateProperty('version', currentVersion);
969
976
  return packageJson;
970
977
  }
971
- async populateComponentsFilesToWriteForCapsule(component, ids, legacyScope, opts) {
978
+ async populateComponentsFilesToWriteForCapsule(component, ids, legacyScope, opts, populateArtifactsFromComps) {
972
979
  const legacyComp = component.state._consumer;
973
980
  const dataToPersist = new (_component2().DataToPersist)();
974
981
  const clonedFiles = legacyComp.files.map(file => file.clone());
@@ -987,11 +994,49 @@ class IsolatorMain {
987
994
  await _workspaceModules().PackageJsonTransformer.applyTransformers(component, packageJson);
988
995
  const valuesToMerge = legacyComp.overrides.componentOverridesPackageJsonData;
989
996
  packageJson.mergePackageJsonObject(valuesToMerge);
997
+ if (populateArtifactsFromComps && !opts?.populateArtifactsIgnorePkgJson) {
998
+ const compParent = this.getCompForArtifacts(component, populateArtifactsFromComps);
999
+ this.mergePkgJsonFromLastBuild(compParent, packageJson);
1000
+ }
990
1001
  dataToPersist.addFile(packageJson.toVinylFile());
991
- const artifacts = await this.getArtifacts(component, legacyScope, opts?.populateArtifactsFrom);
1002
+ const artifacts = await this.getArtifacts(component, legacyScope, populateArtifactsFromComps);
992
1003
  dataToPersist.addManyFiles(artifacts);
993
1004
  return dataToPersist;
994
1005
  }
1006
+ mergePkgJsonFromLastBuild(component, packageJson) {
1007
+ const suffix = `for ${component.id.toString()}. to workaround this, use --ignore-last-pkg-json flag`;
1008
+ const aspectsData = component.extensions.findExtension('teambit.pipelines/builder')?.data?.aspectsData;
1009
+ if (!aspectsData) throw new Error(`getPkgJsonFromLastBuild, unable to find builder aspects data ${suffix}`);
1010
+ const data = aspectsData?.find(aspectData => aspectData.aspectId === 'teambit.pkg/pkg');
1011
+ if (!data) throw new Error(`getPkgJsonFromLastBuild, unable to find pkg aspect data ${suffix}`);
1012
+ const pkgJsonLastBuild = data?.data?.pkgJson;
1013
+ if (!pkgJsonLastBuild) throw new Error(`getPkgJsonFromLastBuild, unable to find pkgJson of pkg aspect ${suffix}`);
1014
+ const current = packageJson.packageJsonObject;
1015
+ pkgJsonLastBuild.componentId = current.componentId;
1016
+ pkgJsonLastBuild.version = current.version;
1017
+ const mergeDeps = (currentDeps, depsFromLastBuild) => {
1018
+ if (!depsFromLastBuild) return;
1019
+ if (!currentDeps) return depsFromLastBuild;
1020
+ Object.keys(depsFromLastBuild).forEach(depName => {
1021
+ if (!currentDeps[depName]) return;
1022
+ depsFromLastBuild[depName] = currentDeps[depName];
1023
+ });
1024
+ return depsFromLastBuild;
1025
+ };
1026
+ pkgJsonLastBuild.dependencies = mergeDeps(current.dependencies, pkgJsonLastBuild.dependencies);
1027
+ pkgJsonLastBuild.devDependencies = mergeDeps(current.devDependencies, pkgJsonLastBuild.devDependencies);
1028
+ pkgJsonLastBuild.peerDependencies = mergeDeps(current.peerDependencies, pkgJsonLastBuild.peerDependencies);
1029
+ packageJson.mergePackageJsonObject(pkgJsonLastBuild);
1030
+ }
1031
+ getCompForArtifacts(component, populateArtifactsFromComps) {
1032
+ const compParent = populateArtifactsFromComps.find(comp => comp.id.isEqual(component.id, {
1033
+ ignoreVersion: true
1034
+ }));
1035
+ if (!compParent) {
1036
+ throw new Error(`isolator, unable to find where to populate the artifacts from for ${component.id.toString()}`);
1037
+ }
1038
+ return compParent;
1039
+ }
995
1040
  preparePackageJsonToWrite(component, bitDir, ignoreBitDependencies = true) {
996
1041
  const legacyComp = component.state._consumer;
997
1042
  this.logger.debug(`package-json.preparePackageJsonToWrite. bitDir ${bitDir}.`);
@@ -1029,27 +1074,20 @@ class IsolatorMain {
1029
1074
  * later, this responsibility might move to pkg extension, which could write only artifacts
1030
1075
  * that are set in package.json.files[], to have a similar structure of a package.
1031
1076
  */
1032
- async getArtifacts(component, legacyScope, populateArtifactsFrom) {
1077
+ async getArtifacts(component, legacyScope, populateArtifactsFromComps) {
1033
1078
  const legacyComp = component.state._consumer;
1034
1079
  if (!legacyScope) {
1035
- if (populateArtifactsFrom) throw new Error(`unable to fetch from parent, the legacyScope was not provided`);
1036
1080
  // when capsules are written via the workspace, do not write artifacts, they get created by
1037
1081
  // build-pipeline. when capsules are written via the scope, we do need the dists.
1038
1082
  return [];
1039
1083
  }
1040
- if (legacyComp.buildStatus !== 'succeed' && !populateArtifactsFrom) {
1084
+ if (legacyComp.buildStatus !== 'succeed' && !populateArtifactsFromComps) {
1041
1085
  // this is important for "bit sign" when on lane to not go to the original scope
1042
1086
  return [];
1043
1087
  }
1044
1088
  const artifactFilesToFetch = async () => {
1045
- if (populateArtifactsFrom) {
1046
- const found = populateArtifactsFrom.find(id => id.isEqual(component.id, {
1047
- ignoreVersion: true
1048
- }));
1049
- if (!found) {
1050
- throw new Error(`getArtifacts: unable to find where to populate the artifacts from for ${component.id.toString()}`);
1051
- }
1052
- const compParent = await legacyScope.getConsumerComponent(found);
1089
+ if (populateArtifactsFromComps) {
1090
+ const compParent = this.getCompForArtifacts(component, populateArtifactsFromComps);
1053
1091
  return (0, _component2().getArtifactFilesExcludeExtension)(compParent.extensions, 'teambit.pkg/pkg');
1054
1092
  }
1055
1093
  const extensionsNamesForArtifacts = ['teambit.compilation/compiler'];