Skip to content

Commit

Permalink
fix(scope-rename), rename scope of envs in .bitmap config (#7733)
Browse files Browse the repository at this point in the history
Fixes an issue when scope-renaming multiple components and some of them
are envs of others. It was throwing ScopeNotFound.
This PR fixes it by replacing the env ids inside `config` prop in the
.bitmap file.
  • Loading branch information
davidfirst authored Aug 3, 2023
1 parent 9d3a59e commit 59f8961
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions scopes/component/renaming/renaming.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import ComponentAspect, { Component, ComponentID, ComponentMain } from '@teambit
import { DeprecationAspect, DeprecationMain } from '@teambit/deprecation';
import GraphqlAspect, { GraphqlMain } from '@teambit/graphql';
import { CompilerAspect, CompilerMain } from '@teambit/compiler';
import EnvsAspect, { EnvsMain } from '@teambit/envs';
import NewComponentHelperAspect, { NewComponentHelperMain } from '@teambit/new-component-helper';
import RefactoringAspect, { MultipleStringsReplacement, RefactoringMain } from '@teambit/refactoring';
import ComponentWriterAspect, { ComponentWriterMain } from '@teambit/component-writer';
Expand Down Expand Up @@ -37,7 +38,8 @@ export class RenamingMain {
private config: ConfigMain,
private componentWriter: ComponentWriterMain,
private compiler: CompilerMain,
private logger: Logger
private logger: Logger,
private envs: EnvsMain
) {}

async rename(sourceIdStr: string, targetName: string, options: RenameOptions): Promise<RenameDependencyNameResult> {
Expand Down Expand Up @@ -119,6 +121,17 @@ make sure this argument is the name only, without the scope-name. to change the
if (!componentsUsingOldScope.length && this.workspace.defaultScope !== oldScope) {
throw new OldScopeNotFound(oldScope);
}
const envs = componentsUsingOldScope.filter((c) => this.envs.isEnv(c));
const compsUsingEnv = {};
await Promise.all(
envs.map(async (env) => {
const components = await this.workspace.getComponentsUsingEnv(env.id.toString(), true);
if (!components.length) return;
const componentIds = components.map((comp) => comp.id);
compsUsingEnv[env.id.toString()] = componentIds;
})
);

// verify they're all new.
const exported = componentsUsingOldScope.filter((comp) => comp.id._legacy.hasScope());
if (exported.length) {
Expand All @@ -137,6 +150,16 @@ make sure this argument is the name only, without the scope-name. to change the
componentsUsingOldScope.forEach((comp) => this.workspace.bitMap.setDefaultScope(comp.id, newScope));
}
await this.workspace.bitMap.write();
await this.workspace.clearCache();

await Promise.all(
envs.map(async (env) => {
const componentIds = compsUsingEnv[env.id.toString()];
if (!componentIds.length) return;
await this.workspace.setEnvToComponents(env.id.changeScope(newScope), componentIds);
})
);

const refactoredIds: ComponentID[] = [];
if (options.refactor) {
const legacyComps = componentsUsingOldScope.map((c) => c.state._consumer);
Expand Down Expand Up @@ -339,6 +362,7 @@ make sure this argument is the name only, without the scope-name. to change the
ComponentWriterAspect,
CompilerAspect,
LoggerAspect,
EnvsAspect,
];
static runtime = MainRuntime;
static async provider([
Expand All @@ -354,6 +378,7 @@ make sure this argument is the name only, without the scope-name. to change the
componentWriter,
compiler,
loggerMain,
envs,
]: [
CLIMain,
Workspace,
Expand All @@ -366,7 +391,8 @@ make sure this argument is the name only, without the scope-name. to change the
ConfigMain,
ComponentWriterMain,
CompilerMain,
LoggerMain
LoggerMain,
EnvsMain
]) {
const logger = loggerMain.createLogger(RenamingAspect.id);
const renaming = new RenamingMain(
Expand All @@ -378,7 +404,8 @@ make sure this argument is the name only, without the scope-name. to change the
config,
componentWriter,
compiler,
logger
logger,
envs
);
cli.register(new RenameCmd(renaming));

Expand Down

0 comments on commit 59f8961

Please sign in to comment.