Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion extensions/npm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,17 @@
"url": "https://github.com/Idered/iridium"
},
"activationEvents": [
"onView:iridium.npm"
"onView:iridium.npm",
"onCommand:iridium.openClosestPackageJson"
],
"contributes": {
"commands": [
{
"command": "iridium.openClosestPackageJson",
"title": "Open Closest package.json to Current Editor",
"category": "Node Dependencies"
}
],
"viewsContainers": {
"activitybar": [
{
Expand Down
57 changes: 57 additions & 0 deletions extensions/npm/src/commands/openClosestPackageJson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import * as vscode from "vscode";

export const openClosestPackageJson = async () => {
const uri = vscode.window.activeTextEditor?.document.uri;
if (uri === undefined || uri.scheme === "untitled") {
return;
}

const closestPackageJson = await findUpPackageJson(uri);
if (closestPackageJson === undefined) {
await vscode.window.showWarningMessage("No closest package.json found");
return;
}

const uriToOpen = vscode.Uri.joinPath(closestPackageJson, "package.json");
await vscode.window.showTextDocument(uriToOpen);
};

/**
* @param uri Initial URI of textEditor, but not directory!
* @returns Uri with path of closest directory with package.json
*/
async function findUpPackageJson<T extends boolean = false>(
uri: vscode.Uri,
throws?: T
): Promise<T extends true ? vscode.Uri : vscode.Uri | undefined> {
const currentWorkspacePath = vscode.workspace.getWorkspaceFolder(uri);
// TODO maybe allow?
if (!currentWorkspacePath) {
throw new Error(
"Opening closest package.json is not supported for files that are not part of opened workspace"
);
}
const { fs } = vscode.workspace;

let currentUri = vscode.Uri.joinPath(uri, "..");
while (true) {
// eslint-disable-next-line no-await-in-loop
const fileList = await fs.readDirectory(currentUri);
const packageJsonFile = fileList.find(
([name, type]) => name === "package.json" && type === vscode.FileType.File
);
if (packageJsonFile) return currentUri;
if (currentUri.path === currentWorkspacePath.uri.path) {
// console.debug('find package.json: reached workspace boundary')
if (throws) {
throw new Error(
"There is no closest package.json from this file within current workspace"
);
}

return undefined!;
}

currentUri = vscode.Uri.joinPath(currentUri, "..");
}
}
6 changes: 6 additions & 0 deletions extensions/npm/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ClientManager } from "./clients/ClientManager";
import { NpmClient } from "./clients/NpmClient";
import { YarnClient } from "./clients/YarnClient";
import { PnpmClient } from "./clients/PnpmClient";
import { openClosestPackageJson } from "./commands/openClosestPackageJson";

export function activate(context: vscode.ExtensionContext) {
const app = bootstrap({
Expand Down Expand Up @@ -49,6 +50,11 @@ export function activate(context: vscode.ExtensionContext) {
webviewView.webview.postMessage({ type: "CONFIG_UPDATED" });
});
});

vscode.commands.registerCommand(
"iridium.openClosestPackageJson",
openClosestPackageJson
);
}

export function deactivate() {}