-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepl.js
More file actions
236 lines (208 loc) · 5.17 KB
/
Copy pathrepl.js
File metadata and controls
236 lines (208 loc) · 5.17 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/env node
/**
* Interactive Firestore REPL: type snippets using the modular API (like addDoc, getDocs).
* Commands: `exit` / `quit` / `.exit`: leave and close the connection.
* Several lines: end with \\ to continue on the next line.
*/
import readline from "node:readline";
import util from "node:util";
import db from "./firebase.js";
import * as firestore from "firebase/firestore";
const tty = process.stdout.isTTY;
const S = {
R: tty ? "\x1b[0m" : "",
dim: tty ? "\x1b[2m" : "",
red: tty ? "\x1b[31m" : "",
green: tty ? "\x1b[32m" : "",
yellow: tty ? "\x1b[33m" : "",
brandBg: tty ? "\x1b[44m" : "",
brandFg: tty ? "\x1b[1;97m" : "",
promptDim: tty ? "\x1b[2;37m" : "",
promptMark: tty ? "\x1b[32m" : "",
};
function rlAnsi(seq) {
return tty ? `\x01${seq}\x02` : "";
}
const PROMPT = tty
? `${rlAnsi(S.promptDim)}firestore${rlAnsi(S.R)}${rlAnsi(S.promptMark)}>${rlAnsi(S.R)} `
: "firestore> ";
const CONTINUE_PROMPT = tty
? `${rlAnsi("\x1b[2;36m")} ...>${rlAnsi(S.R)} `
: " ...> ";
function clearTerminal() {
if (!tty) return;
process.stdout.write("\x1b[3J\x1b[2J\x1b[H");
}
function printBrandBar() {
if (!tty) {
console.log("firestore repl");
return;
}
const label = " \u25C6 firestore repl ";
const cols = process.stdout.columns ?? 80;
const pad = Math.max(0, cols - label.length);
process.stdout.write(
`${S.brandBg}${S.brandFg}${label}${" ".repeat(pad)}${S.R}\n\n`
);
}
function printTagline() {
const gap = `${S.dim} `;
console.log(`${S.dim} modular Firestore shell · v9 API${S.R}
${S.dim} ${S.yellow}help${gap}paste examples from the docs
${S.dim} ${S.yellow}clear${gap}${S.yellow}cls${gap}wipe the screen (not JavaScript, built in)
${S.dim} ${S.yellow}exit${gap}${S.yellow}quit${gap}${S.yellow}.exit${S.dim}${S.R}
`);
}
function printExamples() {
const c = (text) => `${S.dim} # ${text}${S.R}`;
console.log(`
${S.dim} examples CRUD on ${S.green}users/user1${S.dim} (run in order if you want a clean flow)${S.R}
${c("CREATE: setDoc, fixed id users/user1")}
${S.green}await setDoc(doc(db, "users", "user1"), { name: "Ali", age: 25 })${S.R}
${c("READ: getDoc, print .data()")}
${S.green}console.log((await getDoc(doc(db, "users", "user1"))).data())${S.R}
${c("UPDATE: updateDoc, merge fields")}
${S.green}await updateDoc(doc(db, "users", "user1"), { age: 30 })${S.R}
${c("DELETE: deleteDoc")}
${S.green}await deleteDoc(doc(db, "users", "user1"))${S.R}
${c("long line? end with backslash, continue on the next line")}
`);
}
function paintStartup() {
clearTerminal();
printBrandBar();
printTagline();
}
function paintHelp() {
clearTerminal();
printBrandBar();
printTagline();
printExamples();
}
const {
collection,
doc,
addDoc,
setDoc,
getDoc,
getDocs,
updateDoc,
deleteDoc,
query,
where,
orderBy,
limit,
startAfter,
writeBatch,
runTransaction,
Timestamp,
increment,
arrayUnion,
arrayRemove,
serverTimestamp,
terminate,
} = firestore;
const bindings = {
db,
firestore,
collection,
doc,
addDoc,
setDoc,
getDoc,
getDocs,
updateDoc,
deleteDoc,
query,
where,
orderBy,
limit,
startAfter,
writeBatch,
runTransaction,
Timestamp,
increment,
arrayUnion,
arrayRemove,
serverTimestamp,
};
const paramNames = Object.keys(bindings);
function buildRunner(code) {
const body = `
"use strict";
return (async () => {
${code}
})();
`;
return new Function(...paramNames, body)(...paramNames.map((k) => bindings[k]));
}
async function repl() {
paintStartup();
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: PROMPT,
terminal: true,
});
let buffer = "";
let exiting = false;
const runLine = async (line) => {
const trimmed = line.trim();
if (!trimmed) return;
const lower = trimmed.toLowerCase();
if (lower === "exit" || lower === "quit" || lower === ".exit") {
exiting = true;
rl.close();
return;
}
if (lower === "help" || lower === "?") {
paintHelp();
return;
}
if (
lower === "clear" ||
lower === "cls" ||
lower === "clc" ||
lower === "clear screen"
) {
clearTerminal();
return;
}
try {
const pending = buffer + line;
if (/\\\s*$/.test(line)) {
buffer = pending.replace(/\\\s*$/, "\n");
rl.setPrompt(CONTINUE_PROMPT);
return;
}
buffer = "";
rl.setPrompt(PROMPT);
const code = pending;
const result = await buildRunner(code);
if (result !== undefined) {
console.log(
util.inspect(result, { colors: tty, depth: 8, maxArrayLength: 100 })
);
}
} catch (err) {
console.error(`${S.red}${err && err.stack ? err.stack : err}${S.R}`);
}
};
rl.prompt();
for await (const line of rl) {
await runLine(line);
if (exiting) break;
rl.prompt();
}
try {
await terminate(db);
} catch {
// ignore shutdown races
}
if (tty) console.log(`\n${S.dim} ok, later${S.R}\n`);
else console.log("\nok, later\n");
}
repl().catch((err) => {
console.error(`${S.red}${err && err.stack ? err.stack : err}${S.R}`);
process.exitCode = 1;
});