-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcli.ts
More file actions
88 lines (76 loc) · 2.2 KB
/
cli.ts
File metadata and controls
88 lines (76 loc) · 2.2 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
import { constants } from "node:fs";
import { access } from "node:fs/promises";
import type { CancellationToken, LogOutputChannel } from "vscode";
import { workspace } from "vscode";
import { CLI_PATHS, LOCALSTACK_DOCKER_IMAGE_NAME } from "../constants.ts";
import { exec } from "./exec.ts";
import { spawn } from "./spawn.ts";
import type { SpawnOptions } from "./spawn.ts";
const IMAGE_NAME = LOCALSTACK_DOCKER_IMAGE_NAME; // not using the import directly as the constant name should match the env var
const LOCALSTACK_LDM_PREVIEW = "1";
const findLocalStack = async (): Promise<string> => {
// Check if a custom path is configured
const config = workspace.getConfiguration("localstack");
const customLocation = config.get<string | null>("cli.location");
if (customLocation) {
try {
await access(customLocation, constants.X_OK);
return customLocation;
} catch (error) {
throw new Error(
`Configured LocalStack CLI location '${customLocation}' is not accessible: ${error instanceof Error ? error.message : String(error)}`,
{ cause: error },
);
}
}
// Fall back to default search paths
for (const CLI_PATH of CLI_PATHS) {
try {
await access(CLI_PATH, constants.X_OK);
return CLI_PATH;
} catch {
// Continue to next path
}
}
throw new Error(
"LocalStack CLI could not be found in any of the default locations",
);
};
export const execLocalStack = async (
args: string[],
options: {
outputChannel: LogOutputChannel;
// cancellationToken?: CancellationToken;
},
) => {
const cli = await findLocalStack();
const response = await exec([`"${cli}"`, ...args].join(" "), {
env: {
...process.env,
IMAGE_NAME,
LOCALSTACK_LDM_PREVIEW,
},
});
return response;
};
export const spawnLocalStack = async (
args: string[],
options: {
outputChannel: LogOutputChannel;
cancellationToken?: CancellationToken;
onStderr?: SpawnOptions["onStderr"];
},
) => {
const cli = await findLocalStack();
return spawn(`"${cli}"`, args, {
outputChannel: options.outputChannel,
outputLabel: `localstack.${args[0]}`,
cancellationToken: options.cancellationToken,
environment: {
...process.env,
IMAGE_NAME,
LOCALSTACK_LDM_PREVIEW,
},
onStderr: options.onStderr,
});
};