-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
52 lines (44 loc) · 1.4 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
import * as firebase from 'firebase';
import * as readline from 'readline';
import * as colors from 'colors';
firebase.initializeApp({
apiKey: "AIzaSyDdbOngsxV9djoBRfnrx8hMvsskMCGzNaQ",
authDomain: "chat-8208f.firebaseapp.com",
databaseURL: "https://chat-8208f.firebaseio.com",
storageBucket: "chat-8208f.appspot.com",
messagingSenderId: "169961205841"
});
const chatsRef = firebase.database().ref('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 () => {
// Get user name
const userName = (await prompt('Your name (anonymous): ')).trim() || 'anonymous';
// Write new messages to console
chatsRef.limitToLast(100).on('child_added', snapshot => {
const message = snapshot!.val();
log(`${colors.yellow(message.user)}: ${message.text}`);
});
// Prompt for messages to send
while (true) {
chatsRef.push({
text: await prompt('> '),
user: userName,
});
}
})();