Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed env config renaming #7823

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
6 changes: 4 additions & 2 deletions scopes/component/renaming/renaming.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
26 changes: 0 additions & 26 deletions scopes/workspace/workspace/bit-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
Expand Down
13 changes: 6 additions & 7 deletions scopes/workspace/workspace/envs-subcommands/envs-replace.cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')}`;
}
}
18 changes: 17 additions & 1 deletion scopes/workspace/workspace/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -1705,12 +1717,16 @@ the following envs are used in this workspace: ${availableEnvs.join(', ')}`);
async resolveEnvIdWithPotentialVersionForConfig(envId: ComponentID): Promise<string> {
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<ConfigMain>('teambit.harmony/config').extensions;
Expand Down