-
Notifications
You must be signed in to change notification settings - Fork 69
/
stateAggregator.ts
46 lines (42 loc) · 1.73 KB
/
stateAggregator.ts
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
41
42
43
44
45
46
/*
* Copyright (c) 2022, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { AsyncOptionalCreatable } from '@salesforce/kit';
import { Global } from '../global';
import { AliasAccessor } from './accessors/aliasAccessor';
import { OrgAccessor } from './accessors/orgAccessor';
import { SandboxAccessor } from './accessors/sandboxAccessor';
export class StateAggregator extends AsyncOptionalCreatable {
private static instanceMap: Map<string, StateAggregator> = new Map();
public aliases!: AliasAccessor;
public orgs!: OrgAccessor;
public sandboxes!: SandboxAccessor;
/**
* Reuse a StateAggregator if one was already created for the current global state directory
* Otherwise, create one and adds it to map for future reuse.
* HomeDir might be stubbed in tests
*/
public static async getInstance(): Promise<StateAggregator> {
if (!StateAggregator.instanceMap.has(Global.DIR)) {
StateAggregator.instanceMap.set(Global.DIR, await StateAggregator.create());
}
// TS assertion is valid because there either was one OR it was just now instantiated
return StateAggregator.instanceMap.get(Global.DIR) as StateAggregator;
}
/**
* Clear the cache to force reading from disk.
*
* *NOTE: Only call this method if you must and you know what you are doing.*
*/
public static clearInstance(path = Global.DIR): void {
StateAggregator.instanceMap.delete(path);
}
protected async init(): Promise<void> {
this.orgs = await OrgAccessor.create();
this.sandboxes = await SandboxAccessor.create();
this.aliases = await AliasAccessor.create();
}
}