From dd65becb0440dcf905b45ec69d608b19d4d2cc4d Mon Sep 17 00:00:00 2001 From: Nitsan Cohen <77798308+NitsanCohen770@users.noreply.github.com> Date: Wed, 23 Aug 2023 01:17:12 +0300 Subject: [PATCH 1/9] fixed env config renaming --- scopes/workspace/workspace/bit-map.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/scopes/workspace/workspace/bit-map.ts b/scopes/workspace/workspace/bit-map.ts index 50fc9aeb6981..3c9f290df2ed 100644 --- a/scopes/workspace/workspace/bit-map.ts +++ b/scopes/workspace/workspace/bit-map.ts @@ -192,14 +192,15 @@ export class BitMap { const config = componentMap.config; if (!config) return; Object.keys(config).forEach((aspectId) => { - if (aspectId === sourceId.toString()) { + const fullSourceId = this.getBitmapEntry(sourceId).id.toStringWithoutVersion(); + if (aspectId === fullSourceId) { config[targetId.toString()] = config[aspectId]; delete config[aspectId]; this.markAsChanged(); } if (aspectId === EnvsAspect.id) { const envConfig = config[aspectId]; - if (envConfig !== REMOVE_EXTENSION_SPECIAL_SIGN && envConfig.env === sourceId.toString()) { + if (envConfig !== REMOVE_EXTENSION_SPECIAL_SIGN && envConfig.env !== sourceId.toString()) { envConfig.env = targetId.toString(); this.markAsChanged(); } From 4a585e8fef806bc54cf5247062b46eeb834a5a77 Mon Sep 17 00:00:00 2001 From: Nitsan Cohen <77798308+NitsanCohen770@users.noreply.github.com> Date: Wed, 23 Aug 2023 03:39:41 +0300 Subject: [PATCH 2/9] fix if check and env with version --- scopes/workspace/workspace/bit-map.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scopes/workspace/workspace/bit-map.ts b/scopes/workspace/workspace/bit-map.ts index 3c9f290df2ed..0bb921082c76 100644 --- a/scopes/workspace/workspace/bit-map.ts +++ b/scopes/workspace/workspace/bit-map.ts @@ -193,6 +193,7 @@ export class BitMap { if (!config) return; Object.keys(config).forEach((aspectId) => { const fullSourceId = this.getBitmapEntry(sourceId).id.toStringWithoutVersion(); + const aspectIdWithoutVersion = ComponentID.fromString(aspectId).toStringWithoutVersion(); if (aspectId === fullSourceId) { config[targetId.toString()] = config[aspectId]; delete config[aspectId]; @@ -200,7 +201,7 @@ export class BitMap { } if (aspectId === EnvsAspect.id) { const envConfig = config[aspectId]; - if (envConfig !== REMOVE_EXTENSION_SPECIAL_SIGN && envConfig.env !== sourceId.toString()) { + if (envConfig !== REMOVE_EXTENSION_SPECIAL_SIGN && envConfig.env === fullSourceId) { envConfig.env = targetId.toString(); this.markAsChanged(); } From bb9784c963b9ec48529a4ef00d0374e4f1176085 Mon Sep 17 00:00:00 2001 From: Nitsan Cohen <77798308+NitsanCohen770@users.noreply.github.com> Date: Wed, 23 Aug 2023 03:53:02 +0300 Subject: [PATCH 3/9] fix replace env with version on bitmap --- scopes/workspace/workspace/bit-map.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scopes/workspace/workspace/bit-map.ts b/scopes/workspace/workspace/bit-map.ts index 0bb921082c76..aa1f3c436a6c 100644 --- a/scopes/workspace/workspace/bit-map.ts +++ b/scopes/workspace/workspace/bit-map.ts @@ -194,7 +194,7 @@ export class BitMap { Object.keys(config).forEach((aspectId) => { const fullSourceId = this.getBitmapEntry(sourceId).id.toStringWithoutVersion(); const aspectIdWithoutVersion = ComponentID.fromString(aspectId).toStringWithoutVersion(); - if (aspectId === fullSourceId) { + if (aspectIdWithoutVersion === fullSourceId) { config[targetId.toString()] = config[aspectId]; delete config[aspectId]; this.markAsChanged(); From 3cc14240807d98845255845b0c6e9b61992be885 Mon Sep 17 00:00:00 2001 From: Nitsan Cohen <77798308+NitsanCohen770@users.noreply.github.com> Date: Wed, 23 Aug 2023 11:53:17 +0300 Subject: [PATCH 4/9] fix rename new component issue --- scopes/workspace/workspace/bit-map.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/scopes/workspace/workspace/bit-map.ts b/scopes/workspace/workspace/bit-map.ts index aa1f3c436a6c..e5ba2dd4b050 100644 --- a/scopes/workspace/workspace/bit-map.ts +++ b/scopes/workspace/workspace/bit-map.ts @@ -191,14 +191,23 @@ export class BitMap { this.legacyBitMap.components.forEach((componentMap) => { const config = componentMap.config; if (!config) return; + Object.keys(config).forEach((aspectId) => { - const fullSourceId = this.getBitmapEntry(sourceId).id.toStringWithoutVersion(); + let fullSourceId: string; + try { + fullSourceId = this.getBitmapEntry(sourceId).id.toStringWithoutVersion(); + } catch (error) { + // If entry not found it's probably new, keep the sourceId as is + fullSourceId = sourceId.toString(); + } + const aspectIdWithoutVersion = ComponentID.fromString(aspectId).toStringWithoutVersion(); if (aspectIdWithoutVersion === fullSourceId) { config[targetId.toString()] = config[aspectId]; delete config[aspectId]; this.markAsChanged(); } + if (aspectId === EnvsAspect.id) { const envConfig = config[aspectId]; if (envConfig !== REMOVE_EXTENSION_SPECIAL_SIGN && envConfig.env === fullSourceId) { @@ -207,6 +216,7 @@ export class BitMap { } } }); + componentMap.config = config; }); } From 4c52a07f3eba39f8f30f57da8f41918505aff91b Mon Sep 17 00:00:00 2001 From: Nitsan Cohen <77798308+NitsanCohen770@users.noreply.github.com> Date: Wed, 23 Aug 2023 12:08:45 +0300 Subject: [PATCH 5/9] remove redundant getBitmapEntry --- scopes/workspace/workspace/bit-map.ts | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/scopes/workspace/workspace/bit-map.ts b/scopes/workspace/workspace/bit-map.ts index e5ba2dd4b050..4e688ea2abe8 100644 --- a/scopes/workspace/workspace/bit-map.ts +++ b/scopes/workspace/workspace/bit-map.ts @@ -191,15 +191,8 @@ export class BitMap { this.legacyBitMap.components.forEach((componentMap) => { const config = componentMap.config; if (!config) return; - Object.keys(config).forEach((aspectId) => { - let fullSourceId: string; - try { - fullSourceId = this.getBitmapEntry(sourceId).id.toStringWithoutVersion(); - } catch (error) { - // If entry not found it's probably new, keep the sourceId as is - fullSourceId = sourceId.toString(); - } + const fullSourceId = sourceId.toStringWithoutVersion(); const aspectIdWithoutVersion = ComponentID.fromString(aspectId).toStringWithoutVersion(); if (aspectIdWithoutVersion === fullSourceId) { From 3b76652a7df66a3510395b95c76e9b93ed79fa66 Mon Sep 17 00:00:00 2001 From: Nitsan Cohen <77798308+NitsanCohen770@users.noreply.github.com> Date: Wed, 23 Aug 2023 15:49:18 +0300 Subject: [PATCH 6/9] replace the method with new workspace method --- scopes/component/renaming/renaming.main.runtime.ts | 4 ++-- scopes/workspace/workspace/bit-map.ts | 7 +++---- .../workspace/envs-subcommands/envs-replace.cmd.ts | 13 ++++++------- scopes/workspace/workspace/workspace.ts | 12 ++++++++++++ 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/scopes/component/renaming/renaming.main.runtime.ts b/scopes/component/renaming/renaming.main.runtime.ts index 47ce12b16f1f..a0b934721dc8 100644 --- a/scopes/component/renaming/renaming.main.runtime.ts +++ b/scopes/component/renaming/renaming.main.runtime.ts @@ -54,6 +54,7 @@ make sure this argument is the name only, without the scope-name. to change the const sourceComp = await this.workspace.get(sourceId); const sourcePackageName = this.workspace.componentPackageName(sourceComp); const targetId = this.newComponentHelper.getNewComponentId(targetName, undefined, options?.scope); + const isSourceCompEnv = this.envs.isEnv(sourceComp); if (!options.preserve) { await this.refactoring.refactorVariableAndClasses(sourceComp, sourceId, targetId); this.refactoring.refactorFilenames(sourceComp, sourceId, targetId); @@ -91,8 +92,7 @@ make sure this argument is the name only, without the scope-name. to change the writeToPath: this.newComponentHelper.getNewComponentPath(targetId), }); } - - this.workspace.bitMap.renameAspectInConfig(sourceId, targetId); + isSourceCompEnv && (await this.workspace.replaceEnvForAllComponents(sourceId, targetId)); await this.workspace.bitMap.write(); await linkToNodeModulesByComponents([targetComp], this.workspace); // link the new-name to node-modules diff --git a/scopes/workspace/workspace/bit-map.ts b/scopes/workspace/workspace/bit-map.ts index 4e688ea2abe8..c777eddd64bc 100644 --- a/scopes/workspace/workspace/bit-map.ts +++ b/scopes/workspace/workspace/bit-map.ts @@ -192,10 +192,9 @@ export class BitMap { const config = componentMap.config; if (!config) return; Object.keys(config).forEach((aspectId) => { - const fullSourceId = sourceId.toStringWithoutVersion(); - + const sourceIdwithoutVersion = sourceId.toStringWithoutVersion(); const aspectIdWithoutVersion = ComponentID.fromString(aspectId).toStringWithoutVersion(); - if (aspectIdWithoutVersion === fullSourceId) { + if (aspectIdWithoutVersion === sourceIdwithoutVersion) { config[targetId.toString()] = config[aspectId]; delete config[aspectId]; this.markAsChanged(); @@ -203,7 +202,7 @@ export class BitMap { if (aspectId === EnvsAspect.id) { const envConfig = config[aspectId]; - if (envConfig !== REMOVE_EXTENSION_SPECIAL_SIGN && envConfig.env === fullSourceId) { + if (envConfig !== REMOVE_EXTENSION_SPECIAL_SIGN && envConfig.env === sourceIdwithoutVersion) { envConfig.env = targetId.toString(); this.markAsChanged(); } diff --git a/scopes/workspace/workspace/envs-subcommands/envs-replace.cmd.ts b/scopes/workspace/workspace/envs-subcommands/envs-replace.cmd.ts index 6e0901aa75e5..76776d2037e0 100644 --- a/scopes/workspace/workspace/envs-subcommands/envs-replace.cmd.ts +++ b/scopes/workspace/workspace/envs-subcommands/envs-replace.cmd.ts @@ -20,12 +20,11 @@ export class EnvsReplaceCmd implements Command { constructor(private workspace: Workspace) {} - async report([oldEnv, env]: [string, string]) { - const envId = await this.workspace.resolveComponentId(env); - const components = await this.workspace.getComponentsUsingEnv(oldEnv, true, true); - const componentIds = components.map((comp) => comp.id); - await this.workspace.setEnvToComponents(envId, componentIds); - return `added ${chalk.bold(envId.toString())} env to the following component(s): -${componentIds.map((compId) => compId.toString()).join('\n')}`; + async report([currentEnv, newEnv]: [string, string]) { + const currentEnvId = await this.workspace.resolveComponentId(currentEnv); + const newEnvId = await this.workspace.resolveComponentId(newEnv); + const changedComponentIds = await this.workspace.replaceEnvForAllComponents(currentEnvId, newEnvId); + return `added ${chalk.bold(newEnvId.toString())} env to the following component(s): +${changedComponentIds.map((compId) => compId.toString()).join('\n')}`; } } diff --git a/scopes/workspace/workspace/workspace.ts b/scopes/workspace/workspace/workspace.ts index 53d0b0f3d9a3..f7ed341da601 100644 --- a/scopes/workspace/workspace/workspace.ts +++ b/scopes/workspace/workspace/workspace.ts @@ -1697,6 +1697,18 @@ the following envs are used in this workspace: ${availableEnvs.join(', ')}`); await this.bitMap.write(); } + /** + * replace the env for all components in the workspace that are using it + * returns the components ids that were changed. + */ + + async replaceEnvForAllComponents(currentEnv: ComponentID, newEnv: ComponentID) { + const components = await this.getComponentsUsingEnv(currentEnv.toString(), true, true); + const componentIds = components.map((comp) => comp.id); + await this.setEnvToComponents(newEnv, componentIds); + return componentIds; + } + /** * helpful when a user provides an env-string to be set and this env has no version. * in the workspace config, a custom-env needs to be set with a version unless it's part of the workspace. From 610fcc49adef1b7e6454fd71190c01fc24c8c7bc Mon Sep 17 00:00:00 2001 From: Nitsan Cohen <77798308+NitsanCohen770@users.noreply.github.com> Date: Wed, 23 Aug 2023 16:01:24 +0300 Subject: [PATCH 7/9] remove renameAspectInConfig as it is not used now --- scopes/workspace/workspace/bit-map.ts | 29 --------------------------- 1 file changed, 29 deletions(-) diff --git a/scopes/workspace/workspace/bit-map.ts b/scopes/workspace/workspace/bit-map.ts index c777eddd64bc..0e5249b5cf37 100644 --- a/scopes/workspace/workspace/bit-map.ts +++ b/scopes/workspace/workspace/bit-map.ts @@ -184,35 +184,6 @@ export class BitMap { } } - /** - * helpful when reaming an aspect and this aspect is used in the config of other components. - */ - renameAspectInConfig(sourceId: ComponentID, targetId: ComponentID) { - this.legacyBitMap.components.forEach((componentMap) => { - const config = componentMap.config; - if (!config) return; - Object.keys(config).forEach((aspectId) => { - const sourceIdwithoutVersion = sourceId.toStringWithoutVersion(); - const aspectIdWithoutVersion = ComponentID.fromString(aspectId).toStringWithoutVersion(); - if (aspectIdWithoutVersion === sourceIdwithoutVersion) { - config[targetId.toString()] = config[aspectId]; - delete config[aspectId]; - this.markAsChanged(); - } - - if (aspectId === EnvsAspect.id) { - const envConfig = config[aspectId]; - if (envConfig !== REMOVE_EXTENSION_SPECIAL_SIGN && envConfig.env === sourceIdwithoutVersion) { - envConfig.env = targetId.toString(); - this.markAsChanged(); - } - } - }); - - componentMap.config = config; - }); - } - removeComponent(id: ComponentID) { this.legacyBitMap.removeComponent(id._legacy); } From 0dbb915b534534737958efea4e0d216261c945b8 Mon Sep 17 00:00:00 2001 From: Nitsan Cohen <77798308+NitsanCohen770@users.noreply.github.com> Date: Wed, 23 Aug 2023 16:24:09 +0300 Subject: [PATCH 8/9] remove import --- scopes/workspace/workspace/bit-map.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/scopes/workspace/workspace/bit-map.ts b/scopes/workspace/workspace/bit-map.ts index 0e5249b5cf37..8e52806f6735 100644 --- a/scopes/workspace/workspace/bit-map.ts +++ b/scopes/workspace/workspace/bit-map.ts @@ -7,7 +7,6 @@ import ComponentMap from '@teambit/legacy/dist/consumer/bit-map/component-map'; import { REMOVE_EXTENSION_SPECIAL_SIGN } from '@teambit/legacy/dist/consumer/config'; import { BitError } from '@teambit/bit-error'; import { LaneId } from '@teambit/lane-id'; -import EnvsAspect from '@teambit/envs'; export type MergeOptions = { mergeStrategy?: 'theirs' | 'ours' | 'manual'; From 24fdf8334f6712c7a740bac83e2d79a29eec3d16 Mon Sep 17 00:00:00 2001 From: Nitsan Cohen <77798308+NitsanCohen770@users.noreply.github.com> Date: Wed, 23 Aug 2023 18:48:01 +0300 Subject: [PATCH 9/9] fix rename new components --- scopes/component/renaming/renaming.main.runtime.ts | 4 +++- scopes/workspace/workspace/workspace.ts | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/scopes/component/renaming/renaming.main.runtime.ts b/scopes/component/renaming/renaming.main.runtime.ts index a0b934721dc8..ac532963e777 100644 --- a/scopes/component/renaming/renaming.main.runtime.ts +++ b/scopes/component/renaming/renaming.main.runtime.ts @@ -59,6 +59,9 @@ make sure this argument is the name only, without the scope-name. to change the await this.refactoring.refactorVariableAndClasses(sourceComp, sourceId, targetId); this.refactoring.refactorFilenames(sourceComp, sourceId, targetId); } + if (isSourceCompEnv) { + await this.workspace.replaceEnvForAllComponents(sourceId, targetId); + } if (isTagged) { const config = await this.getConfig(sourceComp); await this.newComponentHelper.writeAndAddNewComp(sourceComp, targetId, options, config); @@ -92,7 +95,6 @@ make sure this argument is the name only, without the scope-name. to change the writeToPath: this.newComponentHelper.getNewComponentPath(targetId), }); } - isSourceCompEnv && (await this.workspace.replaceEnvForAllComponents(sourceId, targetId)); await this.workspace.bitMap.write(); await linkToNodeModulesByComponents([targetComp], this.workspace); // link the new-name to node-modules diff --git a/scopes/workspace/workspace/workspace.ts b/scopes/workspace/workspace/workspace.ts index f7ed341da601..a43c35b9de59 100644 --- a/scopes/workspace/workspace/workspace.ts +++ b/scopes/workspace/workspace/workspace.ts @@ -1717,12 +1717,16 @@ the following envs are used in this workspace: ${availableEnvs.join(', ')}`); async resolveEnvIdWithPotentialVersionForConfig(envId: ComponentID): Promise { const isCore = this.aspectLoader.isCoreAspect(envId.toStringWithoutVersion()); const existsOnWorkspace = await this.hasId(envId); + const isNew = !envId.hasVersion(); + if (isNew) { + return envId.toString(); + } if (isCore || existsOnWorkspace) { // the env needs to be without version return envId.toStringWithoutVersion(); } // the env must include a version - if (envId.hasVersion()) { + if (!isNew) { return envId.toString(); } const extensions = this.harmony.get('teambit.harmony/config').extensions;