Skip to content

Commit cc87b6c

Browse files
committed
Add support variable substitution in deno.path
1 parent 6ce6ee6 commit cc87b6c

File tree

1 file changed

+47
-8
lines changed

1 file changed

+47
-8
lines changed

client/src/util.ts

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export async function getDenoCommandName() {
3131

3232
/** Returns the absolute path to an existing deno command. */
3333
export async function getDenoCommandPath() {
34-
const command = getWorkspaceConfigDenoExePath();
34+
const command = await getWorkspaceConfigDenoExePath();
3535
const workspaceFolders = workspace.workspaceFolders;
3636
if (!command || !workspaceFolders) {
3737
return command ?? await getDefaultDenoCommand();
@@ -49,15 +49,13 @@ export async function getDenoCommandPath() {
4949
}
5050
}
5151

52-
function getWorkspaceConfigDenoExePath() {
53-
const exePath = workspace.getConfiguration(EXTENSION_NS)
54-
.get<string>("path");
55-
// it is possible for the path to be blank. In that case, return undefined
56-
if (typeof exePath === "string" && exePath.trim().length === 0) {
57-
return undefined;
58-
} else {
52+
async function getWorkspaceConfigDenoExePath() {
53+
let exePath = workspace.getConfiguration(EXTENSION_NS).get<string>("path")?.trim();
54+
if (!exePath) {
5955
return exePath;
6056
}
57+
58+
return resolveVariables(exePath);
6159
}
6260

6361
async function getDefaultDenoCommand() {
@@ -222,3 +220,44 @@ export function readTaskDefinitions(
222220
tasks,
223221
};
224222
}
223+
224+
/**
225+
* Resolves a subset of configuration variables supported by VSCode.
226+
*
227+
* Variables are of the form `${variable}` or `${prefix:variable}`.
228+
*
229+
* @see https://code.visualstudio.com/docs/editor/variables-reference
230+
*/
231+
function resolveVariables(value: string): string {
232+
// Quick check if any variable substitution is needed
233+
if (!value.includes("${")) {
234+
return value;
235+
}
236+
237+
const regex = /\${(?:([^:}]+)|([^:}]+):([^}]+))}/g;
238+
return value.replace(regex, (_, simpleVar, prefix, name) => {
239+
if (simpleVar) {
240+
// Handle simple variables like ${userHome}, ${workspaceFolder}, ${cwd}
241+
switch (simpleVar) {
242+
case "userHome":
243+
return process.env.HOME || process.env.USERPROFILE || "";
244+
case "workspaceFolder":
245+
return workspace.workspaceFolders?.[0]?.uri.fsPath ?? "";
246+
case "cwd":
247+
return process.cwd();
248+
default:
249+
return "";
250+
}
251+
} else {
252+
// Handle prefixed variables like ${workspaceFolder:name} or ${env:VAR}
253+
switch (prefix) {
254+
case "workspaceFolder":
255+
return workspace.workspaceFolders?.find(w => w.name === name)?.uri.fsPath ?? "";
256+
case "env":
257+
return process.env[name] ?? "";
258+
default:
259+
return "";
260+
}
261+
}
262+
});
263+
}

0 commit comments

Comments
 (0)