-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchat.js
109 lines (94 loc) · 3.04 KB
/
chat.js
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
#!/usr/bin/env node
const readline = require('readline');
const fs = require('fs');
const path = require('path');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const chatLog = [];
let first_person = '';
let second_person = '';
let current_person = '';
let fileChosen = '';
rl.question('Continue from previous chat? y/N: ', (cont) => {
if (cont === "Y" || cont === "y") {
chatLog.push(" \n");
const files = fs.readdirSync(path.join(__dirname)).filter((filename) => filename.includes(".md") && !filename.includes("README"));
if (files.length === 0) {
console.log("No Files Available");
rl.close();
return;
}
let iterator = 0;
files.forEach((File1) => {
console.log(File1, iterator);
iterator++;
})
rl.question("Please choose a file, choose by the number next to the file: ", (itera) => {
fileChosen = files[itera];
fs.readFile(fileChosen, 'utf-8',(err, data) => {
data = data.split("\n");
if (err) {
throw err;
}
try {
first_person = data[0].split(":")[0].replaceAll("*", "");
second_person = data[1].split(":")[0].replaceAll("*", "");
current_person = first_person;
chatCont();
} catch {
console.log("Log unfinished, or log is not accessible. The log either has only 1 persons input or it has been most likely corrupted.")
rl.close();
}
});
});
} else {
rl.question('Enter first person name: ', (name1) => {
first_person = name1;
rl.question('Enter second person name: ', (name2) => {
second_person = name2;
current_person = first_person;
chat();
});
});
}
});
function chatCont() {
rl.question(`${current_person}: `, (message) => {
if (message === "byebye") {
const data = chatLog.join("");
fs.appendFileSync(fileChosen, data);
console.log(`wrote to file ${fileChosen}`);
rl.close()
} else {
chatLog.push(`**${current_person}**: ${message}`);
chatLog.push(" \n");
current_person = current_person === first_person ? second_person : first_person;
chatCont();
}
});
}
function chat() {
rl.question(`${current_person}: `, (message) => {
if (message.toLowerCase() === 'byebye') {
const date = new Date();
const dateString = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
const filename = `${first_person}-${second_person}-chatlog-${dateString}.md`;
const data = chatLog.join(''); // added two spaces before the line break
fs.writeFile(filename, data, 'utf8', function(err) {
if (err) {
console.log('An error occured while writing to file');
return;
}
console.log(`Chat log saved to file: ${filename}`);
});
rl.close();
} else {
chatLog.push(`**${current_person}**: ${message}`);
chatLog.push(" \n");
current_person = current_person === first_person ? second_person : first_person;
chat();
}
});
}