From 18f2cc3f1761948cf3c195f02f104637d9f4b71b Mon Sep 17 00:00:00 2001 From: Isaac Poole Date: Sat, 13 Jul 2024 14:38:06 -0400 Subject: [PATCH 1/2] Create command to add new env file --- package.json | 11 ++++++++++- src/EnvironmentTreeviewProvider.ts | 10 ++++++++++ src/extension.ts | 26 +++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index dd7d20a..fe3223a 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,11 @@ "browser": "./dist/extension.js", "contributes": { "commands": [ + { + "command": "environments.create", + "title": "Create", + "icon": "$(add)" + }, { "command": "environments.refresh", "title": "Refresh", @@ -44,6 +49,10 @@ ], "menus": { "view/title": [ + { + "command": "environments.create", + "group": "navigation" + }, { "command": "environments.refresh", "group": "navigation" @@ -127,4 +136,4 @@ "webpack": "^5.92.1", "webpack-cli": "^5.1.4" } -} +} \ No newline at end of file diff --git a/src/EnvironmentTreeviewProvider.ts b/src/EnvironmentTreeviewProvider.ts index d4d1cdc..0b09f35 100644 --- a/src/EnvironmentTreeviewProvider.ts +++ b/src/EnvironmentTreeviewProvider.ts @@ -5,6 +5,16 @@ import type { EnvironmentContent, EnvironmentKeyValue } from "./types"; export class EnvironmentTreeviewProvider implements vscode.TreeDataProvider { + context: vscode.ExtensionContext; + + constructor(context: vscode.ExtensionContext) { + this.context = context; + } + + create(fileName: string) { + vscode.workspace.fs.writeFile(this.context., []) + } + async add(element: EnvironmentFileTreeItem, key: string, value: string) { let content = new TextDecoder().decode( await vscode.workspace.fs.readFile(element.uri) diff --git a/src/extension.ts b/src/extension.ts index 0e1b671..5dfb4c7 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -6,7 +6,7 @@ import { } from "./EnvironmentTreeviewProvider"; export function activate(context: vscode.ExtensionContext) { - const treeDataProvider = new EnvironmentTreeviewProvider(); + const treeDataProvider = new EnvironmentTreeviewProvider(context); vscode.window.createTreeView("environments-sidebar", { treeDataProvider, }); @@ -15,6 +15,30 @@ export function activate(context: vscode.ExtensionContext) { treeDataProvider, }); + vscode.commands.registerCommand("environments.create", async () => { + const workspaceFolders = vscode.workspace.workspaceFolders; + let workplaceFolder: vscode.WorkspaceFolder | undefined; + + if (!workspaceFolders || workspaceFolders.length == 0) { + vscode.window.showErrorMessage("No workspace folder is open"); + } else if (workspaceFolders.length == 1) { + workplaceFolder = workspaceFolders[0]; + } else { + workplaceFolder = await vscode.window.showQuickPick( + workspaceFolders.map((folder) => folder.uri) || [] + ); + } + + const fileName = await vscode.window.showInputBox({ + prompt: "Enter the name of the new environment file", + value: ".env", + }); + + if (fileName) { + treeDataProvider.create(fileName); + } + }); + vscode.commands.registerCommand( "environments.add", async (element: EnvironmentFileTreeItem) => { From 56abdfe0ec1b961e3059f2417d059fda4ae4806a Mon Sep 17 00:00:00 2001 From: Isaac Poole <55164207+isfopo@users.noreply.github.com> Date: Sat, 13 Jul 2024 20:01:46 -0400 Subject: [PATCH 2/2] creates new file --- package.json | 6 +++--- src/EnvironmentTreeviewProvider.ts | 14 ++++++++++++-- src/extension.ts | 10 +++++----- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index fe3223a..483fd00 100644 --- a/package.json +++ b/package.json @@ -50,12 +50,12 @@ "menus": { "view/title": [ { - "command": "environments.create", + "command": "environments.refresh", "group": "navigation" }, { - "command": "environments.refresh", - "group": "navigation" + "command": "environments.create", + "group": "context" } ], "view/item/context": [ diff --git a/src/EnvironmentTreeviewProvider.ts b/src/EnvironmentTreeviewProvider.ts index 0b09f35..fee3f43 100644 --- a/src/EnvironmentTreeviewProvider.ts +++ b/src/EnvironmentTreeviewProvider.ts @@ -11,8 +11,18 @@ export class EnvironmentTreeviewProvider this.context = context; } - create(fileName: string) { - vscode.workspace.fs.writeFile(this.context., []) + create(workspace: string, fileName: string) { + vscode.workspace.fs.writeFile( + vscode.Uri.joinPath( + vscode.Uri.from({ + path: workspace, + scheme: "file", + }), + fileName + ), + new Uint8Array() + ); + this.refresh(); } async add(element: EnvironmentFileTreeItem, key: string, value: string) { diff --git a/src/extension.ts b/src/extension.ts index 5dfb4c7..a99e0b9 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -17,15 +17,15 @@ export function activate(context: vscode.ExtensionContext) { vscode.commands.registerCommand("environments.create", async () => { const workspaceFolders = vscode.workspace.workspaceFolders; - let workplaceFolder: vscode.WorkspaceFolder | undefined; + let workplaceFolder: string | undefined; if (!workspaceFolders || workspaceFolders.length == 0) { vscode.window.showErrorMessage("No workspace folder is open"); } else if (workspaceFolders.length == 1) { - workplaceFolder = workspaceFolders[0]; + workplaceFolder = workspaceFolders[0].uri.fsPath; } else { workplaceFolder = await vscode.window.showQuickPick( - workspaceFolders.map((folder) => folder.uri) || [] + workspaceFolders.map((folder) => folder.uri.fsPath) || [] ); } @@ -34,8 +34,8 @@ export function activate(context: vscode.ExtensionContext) { value: ".env", }); - if (fileName) { - treeDataProvider.create(fileName); + if (workplaceFolder && fileName) { + treeDataProvider.create(workplaceFolder, fileName); } });