-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlicense.ts
More file actions
137 lines (120 loc) · 3.55 KB
/
license.ts
File metadata and controls
137 lines (120 loc) · 3.55 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { homedir, platform } from "node:os";
import { join } from "node:path";
import type { CancellationToken, LogOutputChannel } from "vscode";
import { execLocalStack } from "./cli.ts";
import type { CliStatusTracker } from "./cli.ts";
import { createFileStatusTracker } from "./file-status-tracker.ts";
import type { StatusTracker } from "./file-status-tracker.ts";
/**
* See https://github.com/localstack/localstack/blob/de861e1f656a52eaa090b061bd44fc1a7069715e/localstack-core/localstack/utils/files.py#L38-L55.
* @returns The cache directory for the current platform.
*/
const cacheDirectory = () => {
switch (platform()) {
case "win32":
return join(process.env.LOCALAPPDATA!, "cache");
case "darwin":
return join(homedir(), "Library", "Caches");
default:
return process.env.XDG_CACHE_HOME ?? join(homedir(), ".cache");
}
};
/**
* The file that contains the license information of the LocalStack CLI.
*
* The license file is stored in the cache directory for the current platform.
*/
export const LICENSE_FILENAME = join(
cacheDirectory(),
"localstack-cli",
"license.json",
);
const LICENSE_VALIDITY_MARKER = "license validity: valid";
export async function checkIsLicenseValid(
cliPath: string,
outputChannel: LogOutputChannel,
) {
try {
const licenseInfoResponse = await execLocalStack(
cliPath,
["license", "info"],
{
outputChannel,
},
);
return licenseInfoResponse.stdout.includes(LICENSE_VALIDITY_MARKER);
} catch (error) {
outputChannel.error(error instanceof Error ? error : String(error));
return false;
}
}
export async function activateLicense(
cliPath: string,
outputChannel: LogOutputChannel,
) {
try {
await execLocalStack(cliPath, ["license", "activate"], {
outputChannel,
});
} catch (error) {
outputChannel.error(error instanceof Error ? error : String(error));
}
}
export async function activateLicenseUntilValid(
cliPath: string,
outputChannel: LogOutputChannel,
cancellationToken: CancellationToken,
): Promise<void> {
while (true) {
if (cancellationToken.isCancellationRequested) {
break;
}
const licenseIsValid = await checkIsLicenseValid(cliPath, outputChannel);
if (licenseIsValid) {
break;
}
await activateLicense(cliPath, outputChannel);
// Wait before trying again
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
/**
* Creates a status tracker that monitors the LocalStack license file for changes.
* When the file is changed, the provided check function is called to determine the current setup status.
* Emits status changes to registered listeners.
*
* @param outputChannel - Channel for logging output and trace messages.
* @returns A {@link StatusTracker} instance for querying status, subscribing to changes, and disposing resources.
*/
export function createLicenseStatusTracker(
cliTracker: CliStatusTracker,
authTracker: StatusTracker,
outputChannel: LogOutputChannel,
): StatusTracker {
const licenseTracker = createFileStatusTracker(
outputChannel,
"[setup-status.license]",
[LICENSE_FILENAME],
async () => {
if (cliTracker.outdated()) {
return "setup_required";
}
const cliPath = cliTracker.cliPath();
if (!cliPath) {
return "waiting_for_dependencies";
}
const isLicenseValid = await checkIsLicenseValid(cliPath, outputChannel);
return isLicenseValid ? "ok" : "setup_required";
},
);
authTracker.onChange(() => {
licenseTracker.check();
});
cliTracker.onCliPathChange(() => {
licenseTracker.check();
});
cliTracker.onOutdatedChange(() => {
licenseTracker.check();
});
return licenseTracker;
}