Skip to content

Commit

Permalink
refactor constants and add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nhedger committed Oct 4, 2024
1 parent 705a6c9 commit f95c7c4
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 117 deletions.
33 changes: 19 additions & 14 deletions src/binary-finder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ import { dirname, join } from "node:path";
import { delimiter } from "node:path";
import { Uri, window } from "vscode";
import { config, getLspBin } from "./config";
import {
platformIdentifier,
platformSpecificBinaryName,
platformSpecificNodePackageName,
} from "./constants";
import { downloadBiome, getDownloadedVersion } from "./downloader";
import { debug, info } from "./logger";
import {
binaryName,
fileExists,
getPackageName,
hasNodeDependencies,
packageName,
platform,
} from "./utils";
import { fileExists, hasNodeDependencies } from "./utils";

export type LocatorStrategy = {
/**
Expand Down Expand Up @@ -94,8 +92,8 @@ const vsCodeSettingsStrategy: LocatorStrategy = {
const findPlatformSpecificBinary = async (
bin: Record<string, string>,
): Promise<Uri | undefined> => {
if (platform in bin) {
return findBinary(bin[platform]);
if (platformIdentifier in bin) {
return findBinary(bin[platformIdentifier]);
}

return undefined;
Expand Down Expand Up @@ -147,10 +145,14 @@ const nodeModulesStrategy: LocatorStrategy = {
// We need to resolve the package.json file here because the
// platform-specific packages do not have an entry point.
const binPackage = dirname(
biomePackage.resolve(`${getPackageName()}/package.json`),
biomePackage.resolve(
`${platformSpecificNodePackageName}/package.json`,
),
);

const binPath = Uri.file(join(binPackage, binaryName()));
const binPath = Uri.file(
join(binPackage, platformSpecificBinaryName),
);

if (!(await fileExists(binPath))) {
return undefined;
Expand Down Expand Up @@ -193,7 +195,7 @@ const yarnPnpStrategy: LocatorStrategy = {
}

return yarnPnpApi.resolveRequest(
`${packageName}/${binaryName()}`,
`${platformSpecificNodePackageName}/${platformSpecificBinaryName}`,
biomePackage,
);
} catch {
Expand Down Expand Up @@ -223,7 +225,10 @@ const pathEnvironmentVariableStrategy: LocatorStrategy = {
}

for (const dir of pathEnv.split(delimiter)) {
const biome = Uri.joinPath(Uri.file(dir), binaryName());
const biome = Uri.joinPath(
Uri.file(dir),
platformSpecificBinaryName,
);

if (await fileExists(biome)) {
return biome;
Expand Down
113 changes: 113 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { version, workspace } from "vscode";

/**
* Platform identifier
*
* This constant contains the identifier of the current platform.
*
* @example "linux-x64-musl"
* @example "darwin-arm64"
* @example "win32-x64"
*/
export const platformIdentifier = (() => {
// On Linux, we always use the `musl` flavor because it has the advantage of
// having been built statically. This is meant to improve the compatibility
// with various systems such as NixOS, which handle dynamically linked
// binaries differently.
const flavor = process.platform === "linux" ? "-musl" : "";

return `${process.platform}-${process.arch}${flavor}`;
})();

/**
* Platform-specific binary name
*
* This constant contains the name of the Biome CLI binary for the current
* platform.
*
* @example "biome" (on Linux, macOS, and other Unix-like systems)
* @example "biome.exe" (on Windows)
*/
export const platformSpecificBinaryName = (() => {
return `biome${process.platform === "win32" ? ".exe" : ""}`;
})();

/**
* Platform-specific package name
*
* This constant contains the name of the Biome CLI asset for the current
* platform.
*
* @example "cli-linux-x64"
* @example "cli-darwin-x64"
* @example "cli-win32-x64"
*/
export const platformSpecificPackageName = (() => {
return `cli-${platformIdentifier}`;
})();

/**
* Platform-specific node package name
*
* This constant contains the name of the Biome CLI node package for the current
* platform.
*
* @example "@biomejs/cli-linux-x64"
* @example "@biomejs/cli-darwin-x64"
* @example "@biomejs/cli-win32-x64"
*/
export const platformSpecificNodePackageName = (() => {
return `@biomejs/${platformSpecificPackageName}`;
})();

/**
* Identifiers of the languages supported by the extension
*
* This constant contains a list of identifiers of the languages supported by the
* extension. These identifiers are used determine whether LSP sessions should be
* taking a given file into account or not.
*/
export const supportedLanguageIdentifiers: string[] = [
"astro",
"css",
"graphql",
"javascript",
"javascriptreact",
"json",
"jsonc",
"svelte",
"typescript",
"typescriptreact",
"vue",
];

export type OperatingMode = "single-file" | "single-root" | "multi-root";

/**
* Operating mode of the extension
*
* This constant contains the operating mode of the extension. The operating
* mode determines whether the extension is operating in single-file,
* single-root, or multi-root mode, which impacts how the extension behaves and
* how LSP sessions are created.
*
* This can be a constant because whenever the operating mode changes, VS Code
* reloads the window, which causes the extension to be destroyed and
* recreated.
*/
export const operatingMode: OperatingMode = (() => {
// If there aren't any workspace folders, we assume to be operating in
// single-file mode.
if (workspace.workspaceFolders === undefined) {
return "single-file";
}

// If more than one workspace folder is present, we assume to be operating
// in multi-root mode.
if (workspace.workspaceFolders.length > 1) {
return "multi-root";
}

// Otherwise, we assume to be operating in single-root mode.
return "single-root";
})();
17 changes: 8 additions & 9 deletions src/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import {
window,
workspace,
} from "vscode";
import {
platformSpecificBinaryName,
platformSpecificPackageName,
} from "./constants";
import { error, info } from "./logger";
import { state } from "./state";
import {
binaryExtension,
binaryName,
fileExists,
platformPackageName,
} from "./utils";
import { fileExists } from "./utils";

export const downloadBiome = async (): Promise<Uri | undefined> => {
const version = await promptVersionToDownload();
Expand Down Expand Up @@ -47,7 +46,7 @@ const downloadBiomeVersion = async (
.json();

const asset = releases.assets.find((asset) => {
return asset.name === platformPackageName;
return asset.name === platformSpecificPackageName;
});

if (!asset) {
Expand All @@ -69,7 +68,7 @@ const downloadBiomeVersion = async (
const binPath = Uri.joinPath(
state.context.globalStorageUri,
"global-bin",
`biome${binaryExtension}`,
platformSpecificBinaryName,
);

try {
Expand All @@ -93,7 +92,7 @@ export const getDownloadedVersion = async (): Promise<
const binPath = Uri.joinPath(
state.context.globalStorageUri,
"global-bin",
binaryName("biome"),
platformSpecificBinaryName,
);

if (!(await fileExists(binPath))) {
Expand Down
7 changes: 4 additions & 3 deletions src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import {
isEnabled,
workspaceFolderRequiresConfigFile,
} from "./config";
import { operatingMode, supportedLanguageIdentifiers } from "./constants";
import { error, warn } from "./logger";
import { state } from "./state";
import { directoryExists, fileExists, mode, supportedLanguages } from "./utils";
import { directoryExists, fileExists } from "./utils";

export type Project = {
folder?: WorkspaceFolder;
Expand All @@ -30,7 +31,7 @@ export type ProjectDefinition = {
};

export const createProjects = async () => {
if (mode === "single-file") {
if (operatingMode === "single-file") {
const project = await createSingleFileProject();
return project ? [project] : [];
}
Expand Down Expand Up @@ -260,7 +261,7 @@ export const updateActiveProject = (editor: TextEditor) => {

state.hidden =
editor?.document === undefined ||
!supportedLanguages.includes(editor.document.languageId);
!supportedLanguageIdentifiers.includes(editor.document.languageId);

state.activeProject = project;
};
18 changes: 9 additions & 9 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,18 @@ import {
import { displayName } from "../package.json";
import { findBiomeGlobally, findBiomeLocally } from "./binary-finder";
import { isEnabledGlobally } from "./config";
import { operatingMode, supportedLanguageIdentifiers } from "./constants";
import { debug, error, info, error as logError, warn } from "./logger";
import { type Project, createProjects } from "./project";
import { state } from "./state";
import {
binaryName,
directoryExists,
fileExists,
fileIsExecutable,
generatePlatformSpecificVersionedBinaryName,
hasUntitledDocuments,
hasVSCodeUserDataDocuments,
mode,
shortURI,
subtractURI,
supportedLanguages,
} from "./utils";

export type Session = {
Expand Down Expand Up @@ -104,7 +102,7 @@ const copyBinaryToTemporaryLocation = async (
const location = Uri.joinPath(
state.context.globalStorageUri,
"tmp-bin",
binaryName(`biome-${version}`),
generatePlatformSpecificVersionedBinaryName(version),
);

try {
Expand Down Expand Up @@ -305,7 +303,8 @@ const createLspLogger = (project?: Project): LogOutputChannel => {
// In this case, we display the name of the project and the relative path to
// the project root in the logger name. Additionally, when in a multi-root
// workspace, we prefix the path with the name of the workspace folder.
const prefix = mode === "multi-root" ? `${project.folder.name}::` : "";
const prefix =
operatingMode === "multi-root" ? `${project.folder.name}::` : "";
const path = subtractURI(project.path, project.folder.uri).fsPath;

return window.createOutputChannel(`${displayName} LSP (${prefix}${path})`, {
Expand Down Expand Up @@ -333,7 +332,8 @@ const createLspTraceLogger = (project?: Project): LogOutputChannel => {
// In this case, we display the name of the project and the relative path to
// the project root in the logger name. Additionally, when in a multi-root
// workspace, we prefix the path with the name of the workspace folder.
const prefix = mode === "multi-root" ? `${project.folder.name}::` : "";
const prefix =
operatingMode === "multi-root" ? `${project.folder.name}::` : "";
const path = subtractURI(project.path, project.folder.uri).fsPath;

return window.createOutputChannel(
Expand All @@ -354,7 +354,7 @@ const createLspTraceLogger = (project?: Project): LogOutputChannel => {
*/
const createDocumentSelector = (project?: Project): DocumentFilter[] => {
if (project) {
return supportedLanguages.map((language) => ({
return supportedLanguageIdentifiers.map((language) => ({
language,
scheme: "file",
pattern: Uri.joinPath(project.path, "**", "*").fsPath.replace(
Expand All @@ -364,7 +364,7 @@ const createDocumentSelector = (project?: Project): DocumentFilter[] => {
}));
}

return supportedLanguages.flatMap((language) => {
return supportedLanguageIdentifiers.flatMap((language) => {
return ["untitled", "vscode-userdata"].map((scheme) => ({
language,
scheme,
Expand Down
Loading

0 comments on commit f95c7c4

Please sign in to comment.