Skip to content

Commit 0b746e2

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

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

client/src/util.ts

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,8 @@ export async function getDenoCommandPath() {
5050
}
5151

5252
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 {
59-
return exePath;
60-
}
53+
const exePath = workspace.getConfiguration(EXTENSION_NS).get<string>("path")?.trim();
54+
return exePath ? resolveVariables(exePath) : undefined;
6155
}
6256

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

0 commit comments

Comments
 (0)