|
1 | 1 | import { generateRandomUserDataString as newEntry } from "./generateRandomUserDataString.js";
|
2 | 2 | import c from "chalk";
|
3 |
| -import { appendFile, mkdir } from "node:fs/promises"; |
| 3 | +import { appendFile, mkdir, readFile } from "node:fs/promises"; |
4 | 4 | import readline from "node:readline/promises";
|
5 | 5 | const fileName = "people.txt";
|
6 |
| -const dirPath = new URL("./../database/task-2", import.meta.url).pathname; |
| 6 | +const dirPath = new URL("./../database/task-3", import.meta.url).pathname; |
7 | 7 |
|
8 | 8 | console.log(c.yellow.bold("LOGIN: "));
|
9 |
| -const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); |
10 |
| -let login = await rl.question(c.bgBlue.bold("username:") + " "); |
| 9 | +let rl = readline.createInterface({ input: process.stdin, output: process.stdout }); |
| 10 | +rl._writeToOutput = function _writeToOutput(stringToWrite) { |
| 11 | + if (rl.stdoutMuted) rl.output.write("*"); |
| 12 | + else rl.output.write(stringToWrite); |
| 13 | +}; |
| 14 | +let username = await rl.question(c.bgBlue.bold("login:") + " "); |
11 | 15 | let password = await rl.question(c.bgBlue.bold("password:") + " ");
|
12 |
| - |
13 |
| -if (login.trim() !== "admin" || password.trim() !== "1234") { |
| 16 | +if (!validateLogin(username, password)) { |
| 17 | + rl.close(); |
| 18 | + console.clear(); |
14 | 19 | console.log(c.red("Neteisingi prisijungimo duomenys"));
|
| 20 | + process.exit(); |
| 21 | +} |
| 22 | +chooseAction(); |
| 23 | + |
| 24 | +process.on("exit", () => { |
15 | 25 | rl.close();
|
16 |
| -} else { |
17 |
| - await mkdir(dirPath, { recursive: true }); |
| 26 | + console.clear(); //to clear password from history. Does it really clears? |
| 27 | + process.exit(); |
| 28 | +}); |
| 29 | + |
| 30 | +function validateLogin(username, password) { |
| 31 | + if (username.trim() !== "admin" || password.trim() !== "1234") { |
| 32 | + return false; |
| 33 | + } |
| 34 | + return true; |
| 35 | +} |
| 36 | +function validateChoice(action) { |
| 37 | + action = action.trim().toLowerCase(); |
| 38 | + if (action === "r" || action === "read") return "r"; |
| 39 | + if (action === "w" || action === "write") return "w"; |
| 40 | + return ""; |
| 41 | +} |
| 42 | + |
| 43 | +async function runAction(actionFunc, callback) { |
| 44 | + await actionFunc(); |
| 45 | + await callback(); |
| 46 | +} |
| 47 | +async function chooseAction() { |
| 48 | + let action = ""; |
| 49 | + while (!validateChoice(action)) { |
| 50 | + console.log(`Exit: ${c.bgBlue("Ctrl + C")}`); |
| 51 | + console.log(`Read: ${c.bgBlue("R")}`); |
| 52 | + console.log(`Write: ${c.bgBlue("W")}`); |
| 53 | + action = await rl.question(``); |
| 54 | + } |
| 55 | + action === "w" ? runAction(writeDataToFile, chooseAction) : runAction(readFileData, chooseAction); |
| 56 | +} |
| 57 | + |
| 58 | +async function writeDataToFile() { |
18 | 59 | try {
|
| 60 | + await mkdir(dirPath, { recursive: true }); |
19 | 61 | await appendFile(`${dirPath}/${fileName}`, newEntry()); ///kelias ne nuo sito failo, bet nuo project rooto??...
|
20 | 62 | console.log(c.green("Duomenys faile sėkmingai išsaugoti"));
|
21 | 63 | } catch (err) {
|
22 | 64 | console.error(err);
|
23 |
| - } finally { |
24 |
| - rl.close(); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +async function readFileData() { |
| 69 | + try { |
| 70 | + await mkdir(dirPath, { recursive: true }); |
| 71 | + let fileData = await readFile(`${dirPath}/${fileName}`); |
| 72 | + console.log(c.green(fileData)); |
| 73 | + } catch (err) { |
| 74 | + console.log(c.red(err.message)); |
25 | 75 | }
|
26 | 76 | }
|
0 commit comments