Skip to content

Commit

Permalink
Add support variable substitution in deno.path
Browse files Browse the repository at this point in the history
  • Loading branch information
zephraph committed Jan 25, 2025
1 parent 6ce6ee6 commit cc87b6c
Showing 1 changed file with 47 additions and 8 deletions.
55 changes: 47 additions & 8 deletions client/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function getDenoCommandName() {

/** Returns the absolute path to an existing deno command. */
export async function getDenoCommandPath() {
const command = getWorkspaceConfigDenoExePath();
const command = await getWorkspaceConfigDenoExePath();
const workspaceFolders = workspace.workspaceFolders;
if (!command || !workspaceFolders) {
return command ?? await getDefaultDenoCommand();
Expand All @@ -49,15 +49,13 @@ export async function getDenoCommandPath() {
}
}

function getWorkspaceConfigDenoExePath() {
const exePath = workspace.getConfiguration(EXTENSION_NS)
.get<string>("path");
// it is possible for the path to be blank. In that case, return undefined
if (typeof exePath === "string" && exePath.trim().length === 0) {
return undefined;
} else {
async function getWorkspaceConfigDenoExePath() {

Check failure on line 52 in client/src/util.ts

View workflow job for this annotation

GitHub Actions / ci

Async function 'getWorkspaceConfigDenoExePath' has no 'await' expression or 'await using' declaration.
let exePath = workspace.getConfiguration(EXTENSION_NS).get<string>("path")?.trim();

Check failure on line 53 in client/src/util.ts

View workflow job for this annotation

GitHub Actions / ci

`exePath` is never reassigned
if (!exePath) {
return exePath;
}

return resolveVariables(exePath);
}

async function getDefaultDenoCommand() {
Expand Down Expand Up @@ -222,3 +220,44 @@ export function readTaskDefinitions(
tasks,
};
}

/**
* Resolves a subset of configuration variables supported by VSCode.
*
* Variables are of the form `${variable}` or `${prefix:variable}`.
*
* @see https://code.visualstudio.com/docs/editor/variables-reference
*/
function resolveVariables(value: string): string {
// Quick check if any variable substitution is needed
if (!value.includes("${")) {
return value;
}

const regex = /\${(?:([^:}]+)|([^:}]+):([^}]+))}/g;
return value.replace(regex, (_, simpleVar, prefix, name) => {
if (simpleVar) {
// Handle simple variables like ${userHome}, ${workspaceFolder}, ${cwd}
switch (simpleVar) {
case "userHome":
return process.env.HOME || process.env.USERPROFILE || "";
case "workspaceFolder":
return workspace.workspaceFolders?.[0]?.uri.fsPath ?? "";
case "cwd":
return process.cwd();
default:
return "";
}
} else {
// Handle prefixed variables like ${workspaceFolder:name} or ${env:VAR}
switch (prefix) {
case "workspaceFolder":
return workspace.workspaceFolders?.find(w => w.name === name)?.uri.fsPath ?? "";
case "env":
return process.env[name] ?? "";
default:
return "";
}
}
});
}

0 comments on commit cc87b6c

Please sign in to comment.