Skip to content

Commit a50027d

Browse files
committed
vscode plugin rewrite
1 parent 8bbaa94 commit a50027d

File tree

8 files changed

+417
-338
lines changed

8 files changed

+417
-338
lines changed

vscode-extension/src/context.ts

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import * as vscode from "vscode";
2+
import { outputChannel } from "./utils";
3+
import path from "node:path";
4+
5+
/* Local state */
6+
let $context: vscode.ExtensionContext | undefined = undefined;
7+
8+
/* Constants */
9+
const OutputKey = "[Context]";
10+
11+
export function setExtensionContext(context: vscode.ExtensionContext) {
12+
$context = context;
13+
}
14+
15+
export function getExtensionContext() {
16+
if (!$context) {
17+
const msg = `${OutputKey} Context not set`;
18+
outputChannel.appendLine(msg);
19+
throw new Error(msg);
20+
}
21+
22+
return $context;
23+
}
24+
25+
export async function getProjectWorkingDir(configPath: string) {
26+
const pwd = path.dirname(await findNearestPackageJson(configPath));
27+
28+
if (!pwd) {
29+
const msg = `${OutputKey} No workspace folder`;
30+
vscode.window.showErrorMessage(msg);
31+
outputChannel.appendLine(msg);
32+
throw new Error(msg);
33+
}
34+
35+
return pwd;
36+
}
37+
38+
async function findNearestPackageJson(startPath: string) {
39+
const rootPath = vscode.workspace.getWorkspaceFolder(
40+
vscode.Uri.file(startPath),
41+
)?.uri.fsPath;
42+
43+
const msg = `${OutputKey} No root folder found. Unable to find package.json`;
44+
45+
if (!rootPath) {
46+
vscode.window.showErrorMessage(msg);
47+
outputChannel.appendLine(msg);
48+
throw new Error(msg);
49+
}
50+
51+
let currentDir = path.dirname(startPath);
52+
53+
while (currentDir.startsWith(rootPath)) {
54+
try {
55+
const packageJsonPath = path.join(currentDir, "package.json");
56+
await vscode.workspace.fs.stat(vscode.Uri.file(packageJsonPath));
57+
return packageJsonPath;
58+
} catch {
59+
currentDir = path.dirname(currentDir);
60+
}
61+
}
62+
63+
vscode.window.showErrorMessage(msg);
64+
outputChannel.appendLine(msg);
65+
throw new Error(msg);
66+
}

0 commit comments

Comments
 (0)