forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPackageLookup.ts
More file actions
40 lines (30 loc) · 1.02 KB
/
PackageLookup.ts
File metadata and controls
40 lines (30 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { BasePackage } from './base/BasePackage';
export class PackageLookup {
private _packageMap: Map<string, BasePackage>;
public constructor() {
this._packageMap = new Map<string, BasePackage>();
}
public loadTree(root: BasePackage): void {
const queue: BasePackage[] = [root];
// We want the lookup to return the shallowest match, so this is a breadth first
// traversal
for (;;) {
const current: BasePackage | undefined = queue.shift();
if (!current) {
break;
}
for (const child of current.children) {
queue.push(child);
}
const key: string = current.nameAndVersion;
if (!this._packageMap.has(key)) {
this._packageMap.set(key, current);
}
}
}
public getPackage(nameAndVersion: string): BasePackage | undefined {
return this._packageMap.get(nameAndVersion);
}
}