Skip to content

Commit bfca1a2

Browse files
committed
task3
1 parent e784df3 commit bfca1a2

File tree

5 files changed

+65
-13
lines changed

5 files changed

+65
-13
lines changed

database/task-1/people.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

database/task-3/people.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Vicki, Dickens, tofunukuy, [email protected], May 4
2+
Oscar, Durgan, wigitibim, [email protected], March 11
3+
Lynn, Ziemann, qanugemih, [email protected], September 11

src/task-1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { appendFile, mkdir } from "node:fs/promises";
33

44
const fileName = "people.txt";
55
const dirPath = new URL("./../database/task-1", import.meta.url).pathname;
6-
await mkdir(dirPath, { recursive: true });
76
try {
7+
await mkdir(dirPath, { recursive: true });
88
await appendFile(`${dirPath}/${fileName}`, newEntry()); ///kelias ne nuo sito failo, bet nuo project rooto??...
99
} catch (err) {
1010
console.error(err);

src/task-2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ if (login.trim() !== "admin" || password.trim() !== "1234") {
1414
console.log(c.red("Neteisingi prisijungimo duomenys"));
1515
rl.close();
1616
} else {
17-
await mkdir(dirPath, { recursive: true });
1817
try {
18+
await mkdir(dirPath, { recursive: true });
1919
await appendFile(`${dirPath}/${fileName}`, newEntry()); ///kelias ne nuo sito failo, bet nuo project rooto??...
2020
console.log(c.green("Duomenys faile sėkmingai išsaugoti"));
2121
} catch (err) {

src/task-3.js

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,76 @@
11
import { generateRandomUserDataString as newEntry } from "./generateRandomUserDataString.js";
22
import c from "chalk";
3-
import { appendFile, mkdir } from "node:fs/promises";
3+
import { appendFile, mkdir, readFile } from "node:fs/promises";
44
import readline from "node:readline/promises";
55
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;
77

88
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:") + " ");
1115
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();
1419
console.log(c.red("Neteisingi prisijungimo duomenys"));
20+
process.exit();
21+
}
22+
chooseAction();
23+
24+
process.on("exit", () => {
1525
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() {
1859
try {
60+
await mkdir(dirPath, { recursive: true });
1961
await appendFile(`${dirPath}/${fileName}`, newEntry()); ///kelias ne nuo sito failo, bet nuo project rooto??...
2062
console.log(c.green("Duomenys faile sėkmingai išsaugoti"));
2163
} catch (err) {
2264
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));
2575
}
2676
}

0 commit comments

Comments
 (0)