Skip to content

Commit

Permalink
Merge pull request #19 from isfopo/issue/Create-command-to-add-key
Browse files Browse the repository at this point in the history
  • Loading branch information
isfopo authored Jul 11, 2024
2 parents c2d007e + d137feb commit eb3b8cf
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
"command": "environments.flip",
"title": "Flip",
"icon": "$(sync)"
},
{
"command": "environments.add",
"title": "Add",
"icon": "$(add)"
}
],
"menus": {
Expand All @@ -45,6 +50,11 @@
}
],
"view/item/context": [
{
"command": "environments.add",
"when": "viewItem == file",
"group": "inline"
},
{
"command": "environments.edit",
"when": "viewItem == keyValue-string",
Expand Down Expand Up @@ -117,4 +127,4 @@
"webpack": "^5.92.1",
"webpack-cli": "^5.1.4"
}
}
}
13 changes: 13 additions & 0 deletions src/EnvironmentTreeviewProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ import type { EnvironmentContent, EnvironmentKeyValue } from "./types";
export class EnvironmentTreeviewProvider
implements vscode.TreeDataProvider<vscode.TreeItem>
{
async add(element: EnvironmentFileTreeItem, key: string, value: string) {
let content = new TextDecoder().decode(
await vscode.workspace.fs.readFile(element.uri)
);

content += `${key}="${value}"\n`;

vscode.workspace.fs.writeFile(
element.uri,
new TextEncoder().encode(content)
);
}

flip(element: EnvironmentKeyValueTreeItem) {
this.edit(element, element.value.value === "true" ? "false" : "true");
this.refresh();
Expand Down
29 changes: 29 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as vscode from "vscode";
import {
EnvironmentFileTreeItem,
EnvironmentKeyValueTreeItem,
EnvironmentTreeviewProvider,
} from "./EnvironmentTreeviewProvider";
Expand All @@ -14,6 +15,34 @@ export function activate(context: vscode.ExtensionContext) {
treeDataProvider,
});

vscode.commands.registerCommand(
"environments.add",
async (element: EnvironmentFileTreeItem) => {
const key = await vscode.window.showInputBox({
prompt: "Enter the key for the new environment variable",
});

if (!key) {
return;
} else if (key.includes(" ")) {
vscode.window.showErrorMessage(
"Environment variable keys cannot contain spaces"
);
return;
}

const value = await vscode.window.showInputBox({
prompt: `Enter the value for ${key}`,
});

if (!value) {
return;
}

treeDataProvider.add(element, key, value);
}
);

vscode.commands.registerCommand("environments.refresh", () =>
treeDataProvider.refresh()
);
Expand Down

0 comments on commit eb3b8cf

Please sign in to comment.