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

improvement(workspace-API), provide one complete API "hasId" for different scenarios #9517

Merged
merged 1 commit into from
Feb 5, 2025
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
2 changes: 1 addition & 1 deletion scopes/component/forking/forking.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class ForkingMain {
async fork(sourceId: string, targetId?: string, options?: ForkOptions): Promise<ComponentID> {
if (!this.workspace) throw new OutsideWorkspaceError();
const sourceCompId = await this.workspace.resolveComponentId(sourceId);
const exists = this.workspace.exists(sourceCompId);
const exists = this.workspace.hasId(sourceCompId, { ignoreVersion: true });
if (exists) {
const existingInWorkspace = await this.workspace.get(sourceCompId);
return this.forkExistingInWorkspace(existingInWorkspace, targetId, options);
Expand Down
4 changes: 2 additions & 2 deletions scopes/generator/generator/component-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export class ComponentGenerator {
const userEnv = this.options.env;

if (!config && this.envId && !userEnv) {
const isInWorkspace = this.workspace.exists(this.envId);
const isInWorkspace = this.workspace.hasId(this.envId, { ignoreVersion: true });
config = {
[isInWorkspace ? this.envId.toStringWithoutVersion() : this.envId.toString()]: {},
'teambit.envs/envs': {
Expand Down Expand Up @@ -226,7 +226,7 @@ export class ComponentGenerator {
// eslint-disable-next-line prefer-const
let { envId, setBy } = getEnvData();
if (envId) {
const isInWorkspace = this.workspace.exists(envId);
const isInWorkspace = this.workspace.hasId(envId, { ignoreVersion: true });
const isSameAsThisEnvId = envId === this.envId?.toString() || envId === this.envId?.toStringWithoutVersion();
if (isSameAsThisEnvId && this.envId) {
envId = isInWorkspace ? this.envId.toStringWithoutVersion() : this.envId.toString();
Expand Down
20 changes: 8 additions & 12 deletions scopes/workspace/workspace/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,22 +442,18 @@ export class Workspace implements ComponentFactory {
}

/**
* Check if a specific id exist in the workspace
* @param componentId
* whether the given component-id is part of the workspace. default to check for the exact version
*/
hasId(componentId: ComponentID): boolean {
const ids = this.listIds();
const found = ids.find((id) => {
return id.isEqual(componentId);
});
return !!found;
hasId(componentId: ComponentID, opts?: { includeDeleted?: boolean, ignoreVersion?: boolean }): boolean {
const ids = opts?.includeDeleted ? this.listIdsIncludeRemoved() : this.listIds();
return opts?.ignoreVersion ? ids.hasWithoutVersion(componentId) : ids.has(componentId);
}

/**
* given component-ids, return the ones that are part of the workspace
*/
async filterIds(ids: ComponentID[]): Promise<ComponentID[]> {
const workspaceIds = await this.listIds();
const workspaceIds = this.listIds();
return ids.filter((id) => workspaceIds.find((wsId) => wsId.isEqual(id, { ignoreVersion: !id.hasVersion() })));
}

Expand Down Expand Up @@ -988,7 +984,7 @@ it's possible that the version ${component.id.version} belong to ${idStr.split('
if (!envAspect) return;
const envExtId = envAspect.id;
if (!envExtId?.hasVersion()) return;
if (!this.exists(envExtId)) return;
if (!this.hasId(envExtId, { ignoreVersion: true })) return;
envAspect.id = envExtId.changeVersion(undefined);
}

Expand Down Expand Up @@ -1062,7 +1058,7 @@ it's possible that the version ${component.id.version} belong to ${idStr.split('
if (isId) {
// if it's not a pattern but just id, resolve it without multimatch to support specifying id without scope-name
const id = await this.resolveComponentId(pattern);
if (this.exists(id, { includeDeleted: opts.includeDeleted })) return [id];
if (this.hasId(id, { ignoreVersion: true, includeDeleted: opts.includeDeleted })) return [id];
if (throwForNoMatch) throw new MissingBitMapComponent(pattern);
return [];
}
Expand Down Expand Up @@ -1136,7 +1132,7 @@ the following envs are used in this workspace: ${availableEnvs.join(', ')}`);
}

/**
* whether a component exists in the workspace
* @deprecated use `hasId` with "ignoreVersion: true" instead.
*/
exists(componentId: ComponentID, opts: { includeDeleted?: boolean } = {}): boolean {
const allIds = opts.includeDeleted ? this.listIdsIncludeRemoved() : this.consumer.bitmapIdsFromCurrentLane;
Expand Down