From 99b832e53c5c511019d4f0d0f26c5b05a3c9d515 Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Tue, 30 Apr 2024 17:43:08 +0200 Subject: [PATCH 1/3] feat: unset-peer --- .../dependencies/dependencies-cmd.ts | 16 ++++++++++++++++ .../dependencies/dependencies.main.runtime.ts | 17 +++++++++++++++++ scopes/workspace/workspace/workspace.ts | 11 ++++++++--- 3 files changed, 41 insertions(+), 3 deletions(-) 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..650ee6c1984d 100644 --- a/scopes/dependencies/dependencies/dependencies.main.runtime.ts +++ b/scopes/dependencies/dependencies/dependencies.main.runtime.ts @@ -77,6 +77,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[], diff --git a/scopes/workspace/workspace/workspace.ts b/scopes/workspace/workspace/workspace.ts index 587e1081728f..55c8bb667090 100644 --- a/scopes/workspace/workspace/workspace.ts +++ b/scopes/workspace/workspace/workspace.ts @@ -946,6 +946,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']; @@ -1458,9 +1464,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); } From 49ff6a9c9ec992158df8770085622ec7291878cd Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Tue, 30 Apr 2024 17:48:20 +0200 Subject: [PATCH 2/3] feat: unset-peer --- .../dependencies/dependencies/dependencies.main.runtime.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/scopes/dependencies/dependencies/dependencies.main.runtime.ts b/scopes/dependencies/dependencies/dependencies.main.runtime.ts index 650ee6c1984d..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'; @@ -409,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); From 62216bc446df3fba30c1669ff861502d66d32e14 Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Thu, 2 May 2024 12:11:57 +0200 Subject: [PATCH 3/3] test: unset-peer --- .../peer-dependency-component.e2e.ts | 13 +++++++++++++ src/e2e-helper/e2e-command-helper.ts | 3 +++ 2 files changed, 16 insertions(+) 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/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`); }