From a8bcf718c491a43a5aaf31af123db8fb84f0b1d3 Mon Sep 17 00:00:00 2001 From: David First Date: Wed, 24 Jan 2024 14:32:36 -0500 Subject: [PATCH 1/2] improvement(rename), introduce a new flag --delete to delete the old comp instead of deprecating it --- e2e/harmony/rename.e2e.ts | 13 +++++++++++++ scopes/component/renaming/rename.cmd.ts | 2 ++ .../component/renaming/renaming.main.runtime.ts | 16 ++++++++++++---- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/e2e/harmony/rename.e2e.ts b/e2e/harmony/rename.e2e.ts index 6b53b8f9de09..a304a6287154 100644 --- a/e2e/harmony/rename.e2e.ts +++ b/e2e/harmony/rename.e2e.ts @@ -81,6 +81,19 @@ describe('bit rename command', function () { ); }); }); + describe('rename with --delete flag', () => { + before(() => { + helper.command.rename('comp1', 'comp2', '--delete'); + }); + it('should create a new component', () => { + const status = helper.command.statusJson(); + expect(status.newComponents).to.have.lengthOf(1); + }); + it('should delete the original component', () => { + const showRemove = helper.command.showAspectConfig('comp1', Extensions.remove); + expect(showRemove.config.removed).to.be.true; + }); + }); }); describe('rename a new component', () => { before(() => { diff --git a/scopes/component/renaming/rename.cmd.ts b/scopes/component/renaming/rename.cmd.ts index acfe5921a58a..4ae8374ee4dc 100644 --- a/scopes/component/renaming/rename.cmd.ts +++ b/scopes/component/renaming/rename.cmd.ts @@ -8,6 +8,7 @@ export type RenameOptions = { refactor?: boolean; preserve?: boolean; ast?: boolean; + delete?: boolean; }; export class RenameCmd implements Command { @@ -38,6 +39,7 @@ export class RenameCmd implements Command { ['r', 'refactor', 'update the import/require statements in all dependent components (in the same workspace)'], ['', 'preserve', 'avoid renaming files and variables/classes according to the new component name'], ['', 'ast', 'EXPERIMENTAL. use ast to transform files instead of regex'], + ['', 'delete', 'EXPERIMENTAL. instead of deprecating the original component, delete it'], ] as CommandOptions; loader = true; remoteOp = true; diff --git a/scopes/component/renaming/renaming.main.runtime.ts b/scopes/component/renaming/renaming.main.runtime.ts index a3edbcbb464d..bf7a10f23e79 100644 --- a/scopes/component/renaming/renaming.main.runtime.ts +++ b/scopes/component/renaming/renaming.main.runtime.ts @@ -11,6 +11,7 @@ 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 RemoveAspect, { RemoveMain } from '@teambit/remove'; import RefactoringAspect, { MultipleStringsReplacement, RefactoringMain } from '@teambit/refactoring'; import ComponentWriterAspect, { ComponentWriterMain } from '@teambit/component-writer'; import { getBindingPrefixByDefaultScope } from '@teambit/legacy/dist/consumer/config/component-config'; @@ -39,7 +40,8 @@ export class RenamingMain { private componentWriter: ComponentWriterMain, private compiler: CompilerMain, private logger: Logger, - private envs: EnvsMain + private envs: EnvsMain, + private remove: RemoveMain ) {} async rename(sourceIdStr: string, targetName: string, options: RenameOptions): Promise { @@ -61,7 +63,9 @@ make sure this argument is the name only, without the scope-name. to change the if (isTagged) { const config = await this.getConfig(sourceComp); await this.newComponentHelper.writeAndAddNewComp(sourceComp, targetId, options, config); - await this.deprecation.deprecate(sourceId, targetId); + options.delete + ? await this.remove.deleteComps(sourceId.toString()) + : await this.deprecation.deprecate(sourceId, targetId); } else { this.workspace.bitMap.renameNewComponent(sourceId, targetId); await this.workspace.bitMap.write(`rename (${sourceIdStr} to ${targetName})`); @@ -372,6 +376,7 @@ make sure this argument is the name only, without the scope-name. to change the CompilerAspect, LoggerAspect, EnvsAspect, + RemoveAspect, ]; static runtime = MainRuntime; static async provider([ @@ -388,6 +393,7 @@ make sure this argument is the name only, without the scope-name. to change the compiler, loggerMain, envs, + remove, ]: [ CLIMain, Workspace, @@ -401,7 +407,8 @@ make sure this argument is the name only, without the scope-name. to change the ComponentWriterMain, CompilerMain, LoggerMain, - EnvsMain + EnvsMain, + RemoveMain ]) { const logger = loggerMain.createLogger(RenamingAspect.id); const renaming = new RenamingMain( @@ -414,7 +421,8 @@ make sure this argument is the name only, without the scope-name. to change the componentWriter, compiler, logger, - envs + envs, + remove ); cli.register(new RenameCmd(renaming)); From 3a15cdd87fb2707db22ba83ca576442738674d89 Mon Sep 17 00:00:00 2001 From: David First Date: Wed, 24 Jan 2024 15:20:00 -0500 Subject: [PATCH 2/2] fix tests --- e2e/harmony/rename.e2e.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/e2e/harmony/rename.e2e.ts b/e2e/harmony/rename.e2e.ts index a304a6287154..a23beb5f1b22 100644 --- a/e2e/harmony/rename.e2e.ts +++ b/e2e/harmony/rename.e2e.ts @@ -83,6 +83,7 @@ describe('bit rename command', function () { }); describe('rename with --delete flag', () => { before(() => { + helper.scopeHelper.getClonedLocalScope(scopeAfterExport); helper.command.rename('comp1', 'comp2', '--delete'); }); it('should create a new component', () => {