-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
73 lines (62 loc) · 2.02 KB
/
index.ts
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
import { initializeApp } from 'firebase/app';
import { getDatabase, ref, onChildAdded, child, push, update} from "firebase/database";
import * as readline from 'readline';
import * as colors from 'colors';
const firebaseApp = initializeApp({
apiKey: "AIzaSyDIU4aipsb4TpwLra2Yts7qrKaTPwdkJCw",
authDomain: "djvu-b393d.firebaseapp.com",
projectId: "djvu-b393d",
storageBucket: "djvu-b393d.appspot.com",
messagingSenderId: "191172952226",
appId: "1:191172952226:web:54d4699f42bce6388257fb",
databaseURL: 'https://djvu-b393d-default-rtdb.firebaseio.com/'
});
const db = getDatabase(firebaseApp);
const chatsRef = ref(db, 'chats');
const rl = readline.createInterface(process.stdin, process.stdout);
// Logs a message keeping prompt on last line
function log(message: string) {
readline.cursorTo(process.stdout, 0, undefined);
console.log(message);
rl.prompt(true);
}
// Read input line from user and delete it from console
function prompt(message: string) {
return new Promise<string>(resolve => {
rl.question(message, userInput => {
readline.moveCursor(process.stdout, 0, -1);
readline.clearLine(process.stdout, 0);
resolve(userInput);
});
});
}
(async () => {
const newVisitKey = push(child(ref(db), 'visits')).key;
const updates = {};
updates['/visits/' + newVisitKey] = {
date: new Date(),
};
update(ref(db), updates);
// Get user name
const userName = (await prompt('Твоё имя (аноним): ')).trim() || 'аноним';
updates['/visits/' + newVisitKey] = {
date: new Date(),
userName
};
update(ref(db), updates);
// Write new messages to console
onChildAdded(chatsRef, snapshot => {
const message = snapshot!.val();
log(`${colors.yellow(message.user)}: ${message.text}`);
});
// Prompt for messages to send
while (true) {
const newMsgKey = push(child(ref(db), 'chats')).key;
const updates = {};
updates['/chats/' + newMsgKey] = {
text: await prompt('> '),
user: userName,
};
update(ref(db), updates);
}
})();