From c9d3216c3d4289f258672999ef77be23924dbbe6 Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Thu, 2 May 2024 13:29:55 +0200 Subject: [PATCH] feat: unset-peer (#8843) --- .../peer-dependency-component.e2e.ts | 13 +++++++++++ .../dependencies/dependencies-cmd.ts | 16 ++++++++++++++ .../dependencies/dependencies.main.runtime.ts | 22 ++++++++++++++++--- scopes/workspace/workspace/workspace.ts | 11 +++++++--- src/e2e-helper/e2e-command-helper.ts | 3 +++ 5 files changed, 59 insertions(+), 6 deletions(-) diff --git a/e2e/functionalities/peer-dependency-component.e2e.ts b/e2e/functionalities/peer-dependency-component.e2e.ts index d8585f3d7d1a..badf45cf2e04 100644 --- a/e2e/functionalities/peer-dependency-component.e2e.ts +++ b/e2e/functionalities/peer-dependency-component.e2e.ts @@ -100,6 +100,19 @@ describe('set-peer', function () { }); }); }); + describe('unset-peer', () => { + before(() => { + helper.command.unsetPeer('comp2'); + helper.command.snapAllComponents(); + helper.command.build(); + }); + it('should remove the always peer fields from the scope data', () => { + const comp = helper.command.catComponent(`comp2@latest`); + const depResolver = comp.extensions.find(({ name }) => name === 'teambit.dependencies/dependency-resolver'); + expect(depResolver.config.peer).to.eq(undefined); + expect(depResolver.config.defaultPeerRange).to.eq(undefined); + }); + }); }); }); diff --git a/scopes/dependencies/dependencies/dependencies-cmd.ts b/scopes/dependencies/dependencies/dependencies-cmd.ts index a94a20b73aa2..007b740e6294 100644 --- a/scopes/dependencies/dependencies/dependencies-cmd.ts +++ b/scopes/dependencies/dependencies/dependencies-cmd.ts @@ -343,3 +343,19 @@ export class SetPeerCmd implements Command { return `${chalk.green('successfully marked the component as a peer component')}`; } } + +export class UnsetPeerCmd implements Command { + name = 'unset-peer '; + arguments = [{ name: 'component-id', description: 'the component to unset as always peer' }]; + group = 'info'; + description = 'unset a component as always peer'; + alias = ''; + options = []; + + constructor(private deps: DependenciesMain) {} + + async report([componentId]: [string]) { + await this.deps.unsetPeer(componentId); + return `${chalk.green('successfully marked the component as not a peer component')}`; + } +} diff --git a/scopes/dependencies/dependencies/dependencies.main.runtime.ts b/scopes/dependencies/dependencies/dependencies.main.runtime.ts index 917e202536b3..78eb33c50c11 100644 --- a/scopes/dependencies/dependencies/dependencies.main.runtime.ts +++ b/scopes/dependencies/dependencies/dependencies.main.runtime.ts @@ -32,6 +32,7 @@ import { RemoveDependenciesFlags, SetDependenciesFlags, SetPeerCmd, + UnsetPeerCmd, WhyCmd, } from './dependencies-cmd'; import { DependenciesAspect } from './dependencies.aspect'; @@ -77,6 +78,23 @@ export class DependenciesMain { await this.workspace.bitMap.write(`set-peer (${componentId})`); } + async unsetPeer(componentId: string): Promise { + const compId = await this.workspace.resolveComponentId(componentId); + // const config = { peer: true, defaultPeerRange: range }; + const config = await this.workspace.getAspectConfigForComponent(compId, DependencyResolverAspect.id); + if (config) { + if ('peer' in config) { + delete config.peer; + } + if ('defaultPeerRange' in config) { + delete config.defaultPeerRange; + } + } + this.workspace.bitMap.addComponentConfig(compId, DependencyResolverAspect.id, config); + + await this.workspace.bitMap.write(`unset-peer (${componentId})`); + } + async setDependency( componentPattern: string, packages: string[], @@ -392,9 +410,7 @@ export class DependenciesMain { new DependenciesBlameCmd(depsMain), new DependenciesUsageCmd(depsMain), ]; - const whyCmd = new WhyCmd(depsMain); - const setPeerCmd = new SetPeerCmd(depsMain); - cli.register(depsCmd, whyCmd, setPeerCmd); + cli.register(depsCmd, new WhyCmd(depsMain), new SetPeerCmd(depsMain), new UnsetPeerCmd(depsMain)); ComponentLoader.loadDeps = depsMain.loadDependencies.bind(depsMain); diff --git a/scopes/workspace/workspace/workspace.ts b/scopes/workspace/workspace/workspace.ts index 08f73d2e4de1..e959388db984 100644 --- a/scopes/workspace/workspace/workspace.ts +++ b/scopes/workspace/workspace/workspace.ts @@ -953,6 +953,12 @@ it's possible that the version ${component.id.version} belong to ${idStr.split(' return EjectConfResult; } + async getAspectConfigForComponent(id: ComponentID, aspectId: string): Promise { + const extensions = await this.getExtensionsFromScopeAndSpecific(id); + const obj = extensions.toConfigObject(); + return obj[aspectId]; + } + async getExtensionsFromScopeAndSpecific(id: ComponentID, excludeComponentJson = false): Promise { const componentFromScope = await this.scope.get(id); const exclude: ExtensionsOrigin[] = ['WorkspaceVariants']; @@ -1469,9 +1475,8 @@ the following envs are used in this workspace: ${availableEnvs.join(', ')}`); await componentConfigFile.write({ override: true }); } else { if (shouldMergeWithPrevious) { - const extensions = await this.getExtensionsFromScopeAndSpecific(id); - const obj = extensions.toConfigObject(); - config = obj[aspectId] ? merge(obj[aspectId], config) : config; + const existingConfig = await this.getAspectConfigForComponent(id, aspectId); + config = existingConfig ? merge(existingConfig, config) : config; } this.bitMap.addComponentConfig(id, aspectId, config, shouldMergeWithExisting); } diff --git a/src/e2e-helper/e2e-command-helper.ts b/src/e2e-helper/e2e-command-helper.ts index 279b18d2c95a..25e58dec9fcb 100644 --- a/src/e2e-helper/e2e-command-helper.ts +++ b/src/e2e-helper/e2e-command-helper.ts @@ -345,6 +345,9 @@ export default class CommandHelper { setPeer(componentId: string, range = '') { return this.runCmd(`bit set-peer ${componentId} ${range}`); } + unsetPeer(componentId: string) { + return this.runCmd(`bit unset-peer ${componentId}`); + } tagComponent(id: string, tagMsg = 'tag-message', options = '') { return this.runCmd(`bit tag ${id} -m ${tagMsg} ${options} --build`); }