Skip to content

Commit

Permalink
chore: port biomejs/biome#492
Browse files Browse the repository at this point in the history
  • Loading branch information
nhedger committed Nov 2, 2023
1 parent c00df0a commit c279623
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type ChildProcess, spawn } from "child_process";
import { type Socket, connect } from "net";
import { isAbsolute } from "path";
import { dirname, isAbsolute } from "path";
import { TextDecoder, promisify } from "util";
import {
ExtensionContext,
Expand All @@ -25,6 +25,7 @@ import { StatusBar } from "./statusBar";
import { setContextValue } from "./utils";

import resolveImpl = require("resolve/async");
import { createRequire } from "module";
import type * as resolve from "resolve";

const resolveAsync = promisify<string, resolve.AsyncOpts, string | undefined>(
Expand Down Expand Up @@ -243,16 +244,34 @@ async function getWorkspaceDependency(
outputChannel: OutputChannel,
): Promise<string | undefined> {
for (const workspaceFolder of workspace.workspaceFolders) {
const path = Uri.joinPath(
workspaceFolder.uri,
"node_modules",
".bin",
"biome",
);
// To resolve the @biomejs/cli-*, which is a transitive dependency of the
// @biomejs/biome package, we need to create a custom require function that
// is scoped to @biomejs/biome. This allows us to reliably resolve the
// package regardless of the package manager used by the user.
try {
const requireFromBiome = createRequire(
require.resolve("@biomejs/biome/package.json", {
paths: [workspaceFolder.uri.fsPath],
}),
);
const binaryPackage = dirname(
requireFromBiome.resolve(
`@biomejs/cli-${process.platform}-${process.arch}/package.json`,
),
);

const biomePath = Uri.file(
`${binaryPackage}/biome${process.platform === "win32" ? ".exe" : ""}`,
);

if (await fileExists(path)) {
return path.fsPath;
if (await fileExists(biomePath)) {
return biomePath.fsPath;
}
} catch {
return undefined;
}

return undefined;
}

window.showWarningMessage(
Expand Down Expand Up @@ -353,7 +372,6 @@ async function getSocket(
): Promise<string> {
const process = spawn(command, ["__print_socket"], {
stdio: [null, "pipe", "pipe"],
shell: true,
});

const stdout = { content: "" };
Expand Down Expand Up @@ -430,4 +448,4 @@ export function deactivate(): Thenable<void> | undefined {
return undefined;
}
return client.stop();
}
}

0 comments on commit c279623

Please sign in to comment.