diff --git a/scopes/component/renaming/renaming.main.runtime.ts b/scopes/component/renaming/renaming.main.runtime.ts index 47ce12b16f1..ac532963e77 100644 --- a/scopes/component/renaming/renaming.main.runtime.ts +++ b/scopes/component/renaming/renaming.main.runtime.ts @@ -54,10 +54,14 @@ 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); } + if (isSourceCompEnv) { + await this.workspace.replaceEnvForAllComponents(sourceId, targetId); + } if (isTagged) { const config = await this.getConfig(sourceComp); await this.newComponentHelper.writeAndAddNewComp(sourceComp, targetId, options, config); @@ -91,8 +95,6 @@ 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); 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 50fc9aeb698..8e52806f673 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'; @@ -184,31 +183,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) => { - if (aspectId === sourceId.toString()) { - 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()) { - envConfig.env = targetId.toString(); - this.markAsChanged(); - } - } - }); - componentMap.config = config; - }); - } - removeComponent(id: ComponentID) { this.legacyBitMap.removeComponent(id._legacy); } diff --git a/scopes/workspace/workspace/envs-subcommands/envs-replace.cmd.ts b/scopes/workspace/workspace/envs-subcommands/envs-replace.cmd.ts index 6e0901aa75e..76776d2037e 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 53d0b0f3d9a..a43c35b9de5 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. @@ -1705,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;