forked from alesdi/firestore-explorer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextension.ts
More file actions
207 lines (181 loc) · 6.19 KB
/
Copy pathextension.ts
File metadata and controls
207 lines (181 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from "vscode";
import * as path from "path";
import * as fs from "fs";
import copyPath from "./commands/copyPath";
import init from "./commands/init";
import orderBy from "./commands/oderBy";
import openPath from "./commands/openPath";
import openServiceAccountSettings from "./commands/openServiceAccountSettings";
import { scheme } from "./constants";
import { DocumentFileSystemProvider } from "./editor/DocumentFileSystemProvider";
import ExplorerDataProvider from "./explorer/ExplorerDataProvider";
import { CollectionItem, Item } from "./explorer/items";
import initializeFirestore from "./utilities/initializeFirestore";
import { openCollectionAsTable } from "./webview/openCollectionAsTable";
function loadTemplates(context: vscode.ExtensionContext): { label: string, data: any }[] {
// Read from user/workspace settings
const config = vscode.workspace.getConfiguration("firestore-studio");
const templates = config.get<any[]>("documentTemplates") || [];
return templates.map(t => ({
label: t.name,
// template is stored as a JSON string → parse it
data: (() => {
try {
return JSON.parse(t.template);
} catch {
return {};
}
})()
}));
}
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export async function activate(context: vscode.ExtensionContext) {
const explorerDataProvider = new ExplorerDataProvider();
const explorerView = vscode.window.createTreeView('firestore-studio-view', {
treeDataProvider: explorerDataProvider,
});
context.subscriptions.push(
vscode.commands.registerCommand(
"firestore-studio.setServiceAccountKeyPath",
openServiceAccountSettings
),
);
context.subscriptions.push(
vscode.commands.registerCommand(
"firestore-studio.init",
init
),
);
context.subscriptions.push(
vscode.commands.registerCommand(
"firestore-studio.openPath",
openPath
)
);
context.subscriptions.push(
vscode.commands.registerCommand(
"firestore-studio.refreshExplorer",
() => explorerDataProvider.refresh()
)
);
context.subscriptions.push(
vscode.commands.registerCommand(
"firestore-studio.copyPath",
copyPath
)
);
context.subscriptions.push(
vscode.commands.registerCommand(
"firestore-studio.orderBy",
(item: Item) => orderBy(item, explorerDataProvider)
)
);
context.subscriptions.push(
vscode.commands.registerCommand(
"firestore-studio.showMoreItems",
(path: string) => {
console.log("Show more items for path:", path);
explorerDataProvider.showMoreItems(path);
}
)
);
context.subscriptions.push(
vscode.commands.registerCommand(
"firestore-studio.openCollectionAsTable",
(item: CollectionItem) => {
openCollectionAsTable(item);
}
)
);
context.subscriptions.push(
vscode.commands.registerCommand("firestore-studio.createDocument", async (item) => {
// Prompt for document ID (optional)
const docId = await vscode.window.showInputBox({
prompt: "Enter Document ID (leave blank for auto-ID)",
placeHolder: "Document ID"
});
// Load templates and let user pick one
const templates = loadTemplates(context);
let data: any;
if (templates.length > 0) {
const picked = await vscode.window.showQuickPick(
templates.map(t => t.label),
{ placeHolder: "Select a template or ESC to enter JSON manually" }
);
if (picked) {
data = templates.find(t => t.label === picked)?.data;
}
}
if (!data) {
const json = await vscode.window.showInputBox({
prompt: "Enter document data as JSON",
placeHolder: '{"field1": "value1", "field2": 123}'
});
if (!json) {
vscode.window.showWarningMessage("No data entered.");
return;
}
try {
data = JSON.parse(json);
} catch (e) {
vscode.window.showErrorMessage("Invalid JSON.");
return;
}
}
try {
const firestore = await initializeFirestore();
const ref = docId
? item.reference.doc(docId)
: item.reference.doc();
await ref.set(data);
// Open the newly created document
console.log("___________________________");
console.log(ref.path);
console.log("__________________________");
vscode.commands.executeCommand(
"firestore-studio.openPath",
ref.path
);
vscode.window.showInformationMessage("Document created!");
vscode.commands.executeCommand("firestore-studio.refreshExplorer");
} catch (err: any) {
vscode.window.showErrorMessage("Failed to create document: " + err.message);
}
})
);
context.subscriptions.push(
vscode.commands.registerCommand("firestore-studio.deleteDocument", async (item) => {
const confirm = await vscode.window.showWarningMessage(
`Are you sure you want to delete the document "${item.label}"?`,
{ modal: true },
"Delete",
"Cancel"
);
if (confirm === "Delete") {
try {
const firestore = await initializeFirestore();
await item.reference.delete();
vscode.window.showInformationMessage("Document deleted!");
vscode.commands.executeCommand("firestore-studio.refreshExplorer");
} catch (err: any) {
vscode.window.showErrorMessage("Failed to delete document: " + err.message);
}
}
})
);
context.subscriptions.push(explorerView);
context.subscriptions.push(
vscode.workspace.onDidChangeConfiguration(
() => {
initializeFirestore(true);
explorerDataProvider.refresh();
}
)
);
context.subscriptions.push(vscode.workspace.registerFileSystemProvider(scheme, new DocumentFileSystemProvider(), { isCaseSensitive: true }));
}
// this method is called when your extension is deactivated
export function deactivate() { }