Skip to content

Commit 789da6e

Browse files
authored
improvement(workspace-API), provide one complete API "hasId" for different scenarios (#9517)
Until now we had two APIs in the Workspace aspect: ``` hasId(componentId: ComponentID): boolean; exists(componentId: ComponentID, opts: { includeDeleted?: boolean } = {}): boolean; ``` Both methods check whether a component ID is part of the workspace. The former checks for an exact match, while the latter ignores the version and provides an option to include deleted components. This is obviously confusing because the method names do not clearly indicate the type of comparison being performed. In this PR, the exists method is deprecated, and hasId is modified (without a breaking change) to include all options: ``` hasId(componentId: ComponentID, opts?: { includeDeleted?: boolean, ignoreVersion?: boolean }): boolean; ```
1 parent 63f48b3 commit 789da6e

File tree

3 files changed

+11
-15
lines changed

3 files changed

+11
-15
lines changed

scopes/component/forking/forking.main.runtime.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class ForkingMain {
6969
async fork(sourceId: string, targetId?: string, options?: ForkOptions): Promise<ComponentID> {
7070
if (!this.workspace) throw new OutsideWorkspaceError();
7171
const sourceCompId = await this.workspace.resolveComponentId(sourceId);
72-
const exists = this.workspace.exists(sourceCompId);
72+
const exists = this.workspace.hasId(sourceCompId, { ignoreVersion: true });
7373
if (exists) {
7474
const existingInWorkspace = await this.workspace.get(sourceCompId);
7575
return this.forkExistingInWorkspace(existingInWorkspace, targetId, options);

scopes/generator/generator/component-generator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ export class ComponentGenerator {
181181
const userEnv = this.options.env;
182182

183183
if (!config && this.envId && !userEnv) {
184-
const isInWorkspace = this.workspace.exists(this.envId);
184+
const isInWorkspace = this.workspace.hasId(this.envId, { ignoreVersion: true });
185185
config = {
186186
[isInWorkspace ? this.envId.toStringWithoutVersion() : this.envId.toString()]: {},
187187
'teambit.envs/envs': {
@@ -226,7 +226,7 @@ export class ComponentGenerator {
226226
// eslint-disable-next-line prefer-const
227227
let { envId, setBy } = getEnvData();
228228
if (envId) {
229-
const isInWorkspace = this.workspace.exists(envId);
229+
const isInWorkspace = this.workspace.hasId(envId, { ignoreVersion: true });
230230
const isSameAsThisEnvId = envId === this.envId?.toString() || envId === this.envId?.toStringWithoutVersion();
231231
if (isSameAsThisEnvId && this.envId) {
232232
envId = isInWorkspace ? this.envId.toStringWithoutVersion() : this.envId.toString();

scopes/workspace/workspace/workspace.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -442,22 +442,18 @@ export class Workspace implements ComponentFactory {
442442
}
443443

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

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

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

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

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

0 commit comments

Comments
 (0)