Skip to content

Commit

Permalink
Merge pull request #23 from isfopo:issue/Create-command-to-add-new-en…
Browse files Browse the repository at this point in the history
…v-file

Issue/Create-command-to-add-new-env-file
  • Loading branch information
isfopo authored Jul 14, 2024
2 parents 838e148 + 56abdfe commit 8294747
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
"browser": "./dist/extension.js",
"contributes": {
"commands": [
{
"command": "environments.create",
"title": "Create",
"icon": "$(add)"
},
{
"command": "environments.refresh",
"title": "Refresh",
Expand All @@ -47,6 +52,10 @@
{
"command": "environments.refresh",
"group": "navigation"
},
{
"command": "environments.create",
"group": "context"
}
],
"view/item/context": [
Expand Down Expand Up @@ -127,4 +136,4 @@
"webpack": "^5.92.1",
"webpack-cli": "^5.1.4"
}
}
}
20 changes: 20 additions & 0 deletions src/EnvironmentTreeviewProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@ import type { EnvironmentContent, EnvironmentKeyValue } from "./types";
export class EnvironmentTreeviewProvider
implements vscode.TreeDataProvider<vscode.TreeItem>
{
context: vscode.ExtensionContext;

constructor(context: vscode.ExtensionContext) {
this.context = 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) {
let content = new TextDecoder().decode(
await vscode.workspace.fs.readFile(element.uri)
Expand Down
26 changes: 25 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand All @@ -15,6 +15,30 @@ export function activate(context: vscode.ExtensionContext) {
treeDataProvider,
});

vscode.commands.registerCommand("environments.create", async () => {
const workspaceFolders = vscode.workspace.workspaceFolders;
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].uri.fsPath;
} else {
workplaceFolder = await vscode.window.showQuickPick(
workspaceFolders.map((folder) => folder.uri.fsPath) || []
);
}

const fileName = await vscode.window.showInputBox({
prompt: "Enter the name of the new environment file",
value: ".env",
});

if (workplaceFolder && fileName) {
treeDataProvider.create(workplaceFolder, fileName);
}
});

vscode.commands.registerCommand(
"environments.add",
async (element: EnvironmentFileTreeItem) => {
Expand Down

0 comments on commit 8294747

Please sign in to comment.