-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·306 lines (286 loc) · 8.87 KB
/
index.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env node
"use strict";
import minimist from "minimist";
import figlet from "figlet";
import chalk from "chalk";
import inquirer from "inquirer";
import gradient from "gradient-string";
import { aesEncrypt } from "./lib/cryptoHelper.js";
import { capturePost } from "./lib/editor.js";
import { setDefaultEditor } from "./lib/editor.js";
import { hashPassword, verifyPassword } from "./lib/argon2Helper.js";
import { createPost, postsByYearTree, postsByMonthTree } from "./lib/posts.js";
import { createUser, getUser, getUserId, hasUsers } from "./lib/users.js";
import {
inquireUsername,
inquirePassword,
inquireCredentials,
inquireDisplayPost,
inquireEditPost,
} from "./lib/inquirerHelper.js";
export const tick = "\u2714";
export const cross = "\u2716";
let username;
let password;
let userId;
const argv = minimist(process.argv.slice(2), {
boolean: ["create-post", "view-post", "edit-post", "new-user", "set-editor"],
string: ["help", "month", "year"],
});
if (argv.help !== undefined) {
printHelp(argv.help);
} else if (argv["create-post"]) {
processLogin().then(() => {
processPostCreation();
});
} else if (argv["new-user"]) {
processUserCreation();
} else if (argv["edit-post"]) {
processLogin().then(() => {
inquireEditPost(userId, password);
});
} else if (argv["view-post"]) {
processLogin().then(() => {
inquireDisplayPost(userId, password);
});
} else if (argv.year || argv.month) {
processLogin().then(() => {
postsByMonthTree(userId, password, argv.year, argv.month);
});
} else if (argv["set-editor"]) {
setDefaultEditor();
} else {
main();
}
async function main() {
await welcome();
if (!hasUsers()) {
console.log("\n👤 Let's start by creating your user credentials.\n");
await processUserCreation()
.then(() => {
console.log("\n🌟 Next, let's create your first post.\n");
return processPostCreation();
})
.then(() => {
console.log("\n🔍 Let's see all the posts you've created.\n");
userId = getUserId(username);
return postsByYearTree(userId, password);
})
.then(() => {
console.log("\n📄 Let's view a specific post.");
return inquireDisplayPost(userId, password);
})
.then(() => {
console.log("\n📝 Next, let's learn post editing.");
return inquireEditPost(userId, password);
})
.then(() => {
console.clear();
console.log("");
console.log("");
console.log("\n✨ You're now ready to use the program!");
console.log("");
return menu(userId, password);
})
.catch((error) => {
handleError(error);
});
} else {
await processLogin();
await menu(userId, password);
}
}
async function menu(userId, password) {
const choices = [
{ name: "Create Post", value: "create" },
{ name: "View Post Hierarchy", value: "hierarchy" },
{ name: "View a Specific Post", value: "view" },
{ name: "Edit/Delete Post", value: "edit_delete" },
{ name: "Exit", value: "exit" },
];
while (true) {
const { action } = await inquirer.prompt({
type: "list",
name: "action",
message: "What would you like to do?",
choices,
});
switch (action) {
case "create":
await processPostCreation(userId, password);
break;
case "view":
await inquireDisplayPost(userId, password);
break;
case "edit_delete":
await inquireEditPost(userId, password);
break;
case "hierarchy":
await postsByYearTree(userId, password);
break;
case "exit":
console.log("");
console.log(chalk.green.bold(`Goodbye, ${username}!`));
return;
default:
console.log(chalk.red.bold("Invalid choice, please try again."));
break;
}
}
}
async function welcome() {
return new Promise((resolve, reject) => {
figlet.text("Secret Journal", { font: "Script" }, async (err, data) => {
if (err) {
reject(err);
} else {
console.log(gradient.rainbow.multiline(data));
await typeText(
chalk.blueBright.bold(
"Confidential Journaling @ the Speed of Thought\n"
)
);
console.log("");
resolve();
}
});
});
}
async function processLogin() {
while (true) {
try {
const credentials = await inquireCredentials();
const tempUsername = credentials.username;
const tempPassword = credentials.password;
const userRecord = getUser(tempUsername);
if (!userRecord) {
console.log("");
console.log(
chalk.red.bold(`${cross} Invalid username. Please try again.`)
);
console.log("");
continue;
}
const isMatch = await verifyPassword(userRecord.password, tempPassword);
if (isMatch) {
username = tempUsername;
password = tempPassword;
console.log("");
console.log(
chalk.green.bold(`${tick} User authenticated successfully.`)
);
console.log("");
userId = getUserId(username);
break;
} else {
console.log("");
console.log(
chalk.red.bold(`${cross} Invalid password. Please try again.`)
);
console.log("");
}
} catch (error) {
handleError(error);
}
}
}
async function processUserCreation() {
try {
username = await inquireUsername();
password = await inquirePassword();
const hashedPassword = await hashPassword(password);
createUser(username, hashedPassword);
} catch (error) {
handleError(error);
}
}
async function processPostCreation() {
try {
const result = await capturePost();
if (result) {
const { title, content, date } = result;
const year = new Date().getFullYear();
const month = new Date().getMonth() + 1;
const userId = getUserId(username);
const encryptedTitle = aesEncrypt(title, password);
const encryptedContent = aesEncrypt(content, password);
createPost(encryptedTitle, encryptedContent, date, year, month, userId);
} else {
console.log("User canceled or an error occurred.");
}
} catch (error) {
handleError(error);
}
}
function handleError(msg) {
console.error("Oops! An error occurred:", msg);
}
function printHelp(topic) {
const specificMessages = {
"new-user": `Use the ${chalk.red.bold(
"--new-user"
)} flag to register as a new user for managing separate journals or in case you forget your credentials.`,
"create-post": `Use the ${chalk.red.bold(
"--create-post"
)} flag to create a new post in your journal.`,
"view-post": `Use the ${chalk.red.bold(
"--view-post"
)} flag to search for posts by keyword and view a specific post.`,
"edit-post": `Use the ${chalk.red.bold(
"--edit-post"
)} flag to search for posts by keyword and edit or delete a specific post.`,
year: `The ${chalk.red.bold("--month")} and ${chalk.red.bold(
"--year"
)} flags display a hierarchical view of posts from a specific month and year. For instance, ${chalk.red.bold(
"--month=7"
)} ${chalk.red.bold(
"--year=2024"
)} shows posts from July 2024. Omitting the ${chalk.red.bold(
"--year"
)} flag displays posts from the entered month of the current year.`,
month: `The ${chalk.red.bold("--month")} and ${chalk.red.bold(
"--year"
)} flags display a hierarchical view of posts from a specific month and year. For instance, ${chalk.red.bold(
"--month=7"
)} ${chalk.red.bold(
"--year=2024"
)} shows posts from July 2024. Omitting the ${chalk.red.bold(
"--year"
)} flag displays posts from the entered month of the current year.`,
"set-editor": `Use the ${chalk.red.bold(
"--set-editor"
)} flag to set your default editor for creating and editing posts.`,
};
const allMessages = [
`• To access all program actions from the menu, start without any flags using the ${chalk.red.bold(
"journal"
)} command.`,
`\nYou can also perform specific actions and exit the program by typing ${chalk.red.bold(
"journal"
)} followed by ${chalk.red.bold("--flag-name")}:\n`,
`• ${specificMessages["new-user"]}\n`,
`• ${specificMessages["create-post"]}\n`,
`• ${specificMessages["view-post"]}\n`,
`• ${specificMessages["edit-post"]}\n`,
`• ${specificMessages["set-editor"]}\n`,
`• ${specificMessages["year"]}\n`,
];
if (specificMessages[topic]) {
console.log(chalk.greenBright(specificMessages[topic]));
} else {
allMessages.forEach((message) => {
console.log(chalk.greenBright(message));
});
}
}
async function sleep(ms = 2000) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
async function typeText(text, delay = 25) {
for (let i = 0; i < text.length; i++) {
process.stdout.write(text[i]);
await sleep(delay);
}
}