Skip to content

Commit

Permalink
vscode plugin rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
rphlmr committed Nov 20, 2024
1 parent 8bbaa94 commit a50027d
Show file tree
Hide file tree
Showing 8 changed files with 417 additions and 338 deletions.
66 changes: 66 additions & 0 deletions vscode-extension/src/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import * as vscode from "vscode";
import { outputChannel } from "./utils";
import path from "node:path";

/* Local state */
let $context: vscode.ExtensionContext | undefined = undefined;

/* Constants */
const OutputKey = "[Context]";

export function setExtensionContext(context: vscode.ExtensionContext) {
$context = context;
}

export function getExtensionContext() {
if (!$context) {
const msg = `${OutputKey} Context not set`;
outputChannel.appendLine(msg);
throw new Error(msg);
}

return $context;
}

export async function getProjectWorkingDir(configPath: string) {
const pwd = path.dirname(await findNearestPackageJson(configPath));

if (!pwd) {
const msg = `${OutputKey} No workspace folder`;
vscode.window.showErrorMessage(msg);
outputChannel.appendLine(msg);
throw new Error(msg);
}

return pwd;
}

async function findNearestPackageJson(startPath: string) {
const rootPath = vscode.workspace.getWorkspaceFolder(
vscode.Uri.file(startPath),
)?.uri.fsPath;

const msg = `${OutputKey} No root folder found. Unable to find package.json`;

if (!rootPath) {
vscode.window.showErrorMessage(msg);
outputChannel.appendLine(msg);
throw new Error(msg);
}

let currentDir = path.dirname(startPath);

while (currentDir.startsWith(rootPath)) {
try {
const packageJsonPath = path.join(currentDir, "package.json");
await vscode.workspace.fs.stat(vscode.Uri.file(packageJsonPath));
return packageJsonPath;
} catch {
currentDir = path.dirname(currentDir);
}
}

vscode.window.showErrorMessage(msg);
outputChannel.appendLine(msg);
throw new Error(msg);
}
Loading

0 comments on commit a50027d

Please sign in to comment.