-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsetup-status.ts
More file actions
73 lines (64 loc) · 2.06 KB
/
setup-status.ts
File metadata and controls
73 lines (64 loc) · 2.06 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import ms from "ms";
import type { Disposable, LogOutputChannel } from "vscode";
import { createEmitter } from "./emitter.ts";
import type { UnwrapPromise } from "./promises.ts";
import { checkSetupStatus } from "./setup.ts";
import type { TimeTracker } from "./time-tracker.ts";
export type SetupStatus = "ok" | "setup_required";
export interface SetupStatusTracker extends Disposable {
status(): SetupStatus;
statuses(): UnwrapPromise<ReturnType<typeof checkSetupStatus>>;
onChange(callback: (status: SetupStatus) => void): void;
}
/**
* Checks the status of the LocalStack installation.
*/
export async function createSetupStatusTracker(
outputChannel: LogOutputChannel,
timeTracker: TimeTracker,
): Promise<SetupStatusTracker> {
const start = Date.now();
let statuses: UnwrapPromise<ReturnType<typeof checkSetupStatus>> | undefined;
let status: SetupStatus | undefined;
const emitter = createEmitter<SetupStatus>(outputChannel);
const end = Date.now();
outputChannel.trace(
`[setup-status]: Initialized dependencies in ${ms(end - start, { long: true })}`,
);
let timeout: NodeJS.Timeout | undefined;
const startChecking = async () => {
statuses = await checkSetupStatus(outputChannel);
const setupRequired = Object.values(statuses).some(
(check) => check === false,
);
const newStatus = setupRequired ? "setup_required" : "ok";
if (status !== newStatus) {
status = newStatus;
await emitter.emit(status);
}
// TODO: Find a smarter way to check the status (e.g. watch for changes in AWS credentials or LocalStack installation)
timeout = setTimeout(() => void startChecking(), 1_000);
};
await timeTracker.run("setup-status.checkIsSetupRequired", async () => {
await startChecking();
});
return {
status() {
// biome-ignore lint/style/noNonNullAssertion: false positive
return status!;
},
statuses() {
// biome-ignore lint/style/noNonNullAssertion: false positive
return statuses!;
},
onChange(callback) {
emitter.on(callback);
if (status) {
callback(status);
}
},
dispose() {
clearTimeout(timeout);
},
};
}