-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlicense.ts
More file actions
75 lines (66 loc) · 2.03 KB
/
license.ts
File metadata and controls
75 lines (66 loc) · 2.03 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
import { homedir, platform } from "node:os";
import { join } from "node:path";
import type { CancellationToken, LogOutputChannel } from "vscode";
import { execLocalStack } from "./cli.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(outputChannel: LogOutputChannel) {
try {
const licenseInfoResponse = await execLocalStack(["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(outputChannel: LogOutputChannel) {
try {
await execLocalStack(["license", "activate"], {
outputChannel,
});
} catch (error) {
outputChannel.error(error instanceof Error ? error : String(error));
}
}
export async function activateLicenseUntilValid(
outputChannel: LogOutputChannel,
cancellationToken: CancellationToken,
): Promise<void> {
while (true) {
if (cancellationToken.isCancellationRequested) {
break;
}
const licenseIsValid = await checkIsLicenseValid(outputChannel);
if (licenseIsValid) {
break;
}
await activateLicense(outputChannel);
// Wait before trying again
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}