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

feat: unset-peer #8843

Merged
merged 4 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions e2e/functionalities/peer-dependency-component.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
});

Expand Down
16 changes: 16 additions & 0 deletions scopes/dependencies/dependencies/dependencies-cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <component-id>';
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')}`;
}
}
22 changes: 19 additions & 3 deletions scopes/dependencies/dependencies/dependencies.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
RemoveDependenciesFlags,
SetDependenciesFlags,
SetPeerCmd,
UnsetPeerCmd,
WhyCmd,
} from './dependencies-cmd';
import { DependenciesAspect } from './dependencies.aspect';
Expand Down Expand Up @@ -77,6 +78,23 @@ export class DependenciesMain {
await this.workspace.bitMap.write(`set-peer (${componentId})`);
}

async unsetPeer(componentId: string): Promise<void> {
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[],
Expand Down Expand Up @@ -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);

Expand Down
11 changes: 8 additions & 3 deletions scopes/workspace/workspace/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<object | undefined> {
const extensions = await this.getExtensionsFromScopeAndSpecific(id);
const obj = extensions.toConfigObject();
return obj[aspectId];
}

async getExtensionsFromScopeAndSpecific(id: ComponentID, excludeComponentJson = false): Promise<ExtensionDataList> {
const componentFromScope = await this.scope.get(id);
const exclude: ExtensionsOrigin[] = ['WorkspaceVariants'];
Expand Down Expand Up @@ -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);
}
Expand Down
3 changes: 3 additions & 0 deletions src/e2e-helper/e2e-command-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
}
Expand Down