Skip to content

Commit

Permalink
help
Browse files Browse the repository at this point in the history
  • Loading branch information
emily-shen committed Jan 23, 2025
1 parent 5308789 commit cf9d779
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions packages/cloudflare-workers-bindings-extension/src/show-bindings.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import path from "path";
import * as vscode from "vscode";
import { importWrangler } from "./wrangler";

export type Config = ReturnType<
ReturnType<typeof importWrangler>["experimental_readRawConfig"]
NonNullable<ReturnType<typeof importWrangler>>["experimental_readRawConfig"]
>["rawConfig"];

export type Environment = Required<Config>["env"][string];
Expand Down Expand Up @@ -39,7 +40,7 @@ type EnvNode = {
name: string | null;
};

type BindingNode = Exclude<
export type BindingNode = Exclude<
{
[Name in BindingType]: {
type: "binding";
Expand All @@ -56,7 +57,7 @@ type ResourceNode = {
description?: string;
};

type Node = EnvNode | BindingNode | ResourceNode;
export type Node = EnvNode | BindingNode | ResourceNode;

export class BindingsProvider implements vscode.TreeDataProvider<Node> {
// Event emitter for refreshing the tree
Expand All @@ -83,11 +84,24 @@ export class BindingsProvider implements vscode.TreeDataProvider<Node> {

return item;
}
// this is the header
case "binding": {
return new vscode.TreeItem(
const item = new vscode.TreeItem(
friendlyBindingNames[node.name],
vscode.TreeItemCollapsibleState.Expanded
);
const enabledBindings = ["kv_namespaces", "r2_buckets", "d1_databases"];
if (enabledBindings.includes(node.name)) {
item.contextValue = "binding";
}
item.iconPath = path.join(
__dirname,
"../",
"resources",
"icons",
`${node.name}.svg`
);
return item;
}
case "resource": {
const item = new vscode.TreeItem(
Expand Down Expand Up @@ -600,26 +614,29 @@ function hasBinding<Config extends Record<string, unknown> | Array<unknown>>(
}

// Finds the first wrangler config file in the workspace and parse it
export async function getWranglerConfig(): Promise<Config | null> {
export async function getWranglerConfig(): Promise<Config | undefined> {
const configUri = await getConfigUri();
if (!configUri) {
return null;
return;
}
const workspaceFolder = vscode.workspace.getWorkspaceFolder(configUri);

if (!workspaceFolder) {
return null;
return;
}

const wrangler = importWrangler(workspaceFolder.uri.fsPath);
const { rawConfig } = wrangler.experimental_readRawConfig({
config: configUri.fsPath,
});

return rawConfig;
if (!wrangler) {
return;
} else {
const { rawConfig } = wrangler.experimental_readRawConfig({
config: configUri.fsPath,
});
return rawConfig;
}
}

export async function getConfigUri(): Promise<vscode.Uri | null> {
export async function getConfigUri(): Promise<vscode.Uri | undefined> {
const [configUri] = await vscode.workspace.findFiles(
"wrangler.{toml,jsonc,json}",
null,
Expand Down

0 comments on commit cf9d779

Please sign in to comment.