-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
417 additions
and
338 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.