Skip to content

Commit f9c4d73

Browse files
committed
all tasks fixed to clear console before exit, changed birtday format
1 parent e1f7735 commit f9c4d73

File tree

6 files changed

+49
-40
lines changed

6 files changed

+49
-40
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"task1": "node ./src/task-1.js",
99
"task2": "node ./src/task-2.js",
1010
"task3": "node ./src/task-3.js",
11+
"bonus": "node ./src/task-bonus.js",
1112
"test": "echo \"Error: no test specified\" && exit 1"
1213
},
1314
"type": "module",

src/generateRandomUserDataString.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { faker } from "@faker-js/faker";
2-
import { rand, monthToStr } from "./helpers.js";
2+
import { rand, birthDayFormat } from "./helpers.js";
33

44
export function generateRandomUserDataString() {
55
const sex = rand(0, 1) ? "female" : "male";
66
const firstName = faker.name.firstName(sex);
77
const lastName = faker.name.lastName(sex);
88
const email = faker.internet.email(firstName, lastName);
99
const password = faker.internet.password(9, true);
10-
const birthDate = faker.date.birthdate({ min: 13, max: 115, mode: "age" });
11-
const birthday = monthToStr(birthDate.getMonth()) + " " + birthDate.getDate();
10+
const birthDate = birthDayFormat(faker.date.birthdate({ min: 13, max: 115, mode: "age" }));
1211

13-
return `${firstName}, ${lastName}, ${password}, ${email}, ${birthday}\n`;
12+
return `${firstName}, ${lastName}, ${password}, ${email}, ${birthDate}\n`;
1413
}

src/helpers.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
export function monthToStr(ind) {
2-
const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "November", "October", "December"];
3-
return months[ind];
4-
}
51
export function rand(min, max) {
62
min = Math.ceil(min);
73
max = Math.floor(max);
84
return Math.floor(Math.random() * (max - min + 1) + min);
95
}
6+
export function birthDayFormat(date) {
7+
// const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "November", "October", "December"];
8+
return date.getFullYear() + "-" + pad0(date.getMonth() + 1) + "-" + pad0(date.getDate());
9+
}
10+
function pad0(num) {
11+
let numStr = num.toString();
12+
if (numStr.length < 2) {
13+
return "0" + numStr;
14+
}
15+
return numStr;
16+
}

src/task-2.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,31 @@ import { generateRandomUserDataString as newEntry } from "./generateRandomUserDa
22
import c from "chalk";
33
import { appendFile, mkdir } from "node:fs/promises";
44
import readline from "node:readline/promises";
5+
56
const fileName = "people.txt";
67
const dirPath = new URL("./../database/task-2", import.meta.url).pathname;
7-
88
console.log(c.yellow.bold("LOGIN: "));
99
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
10+
rl.on("SIGINT", closeProgram);
11+
1012
let login = await rl.question(c.bgBlue.bold("username:") + " ");
1113
let password = await rl.question(c.bgBlue.bold("password:") + " ");
1214

1315
if (login.trim() !== "admin" || password.trim() !== "1234") {
14-
console.log(c.red("Neteisingi prisijungimo duomenys"));
15-
rl.close();
16+
closeProgram(c.red("Neteisingi prisijungimo duomenys"));
1617
} else {
1718
try {
1819
await mkdir(dirPath, { recursive: true });
1920
await appendFile(`${dirPath}/${fileName}`, newEntry()); ///kelias ne nuo sito failo, bet nuo project rooto??...
20-
console.log(c.green("Duomenys faile sėkmingai išsaugoti"));
21+
closeProgram(c.green("Duomenys faile sėkmingai išsaugoti"));
2122
} catch (err) {
22-
console.error(err);
23-
} finally {
24-
rl.close();
23+
closeProgram(c.red(err.message));
2524
}
2625
}
26+
27+
function closeProgram(msg = "") {
28+
console.clear(); //to clear password from history. Does it really clears?
29+
console.log(msg);
30+
rl.close();
31+
process.exit(0);
32+
}

src/task-3.js

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,40 @@ const dirPath = new URL("./../database/task-3", import.meta.url).pathname;
77

88
console.log(c.yellow.bold("LOGIN: "));
99
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-
};
10+
rl.on("SIGINT", closeProgram); //doesn't work on process.on('SIGINT') SIGINT is emitted on ctrl+c press
11+
1412
let username = await rl.question(c.bgBlue.bold("login:") + " ");
1513
let password = await rl.question(c.bgBlue.bold("password:") + " ");
1614
if (!validateLogin(username, password)) {
17-
rl.close();
18-
console.clear();
19-
console.log(c.red("Neteisingi prisijungimo duomenys"));
20-
process.exit();
15+
closeProgram(c.red("Neteisingi prisijungimo duomenys"));
2116
}
22-
chooseAction();
2317

24-
process.on("exit", () => {
25-
rl.close();
26-
console.clear(); //to clear password from history. Does it really clears?
27-
process.exit();
28-
});
18+
chooseAction();
2919

3020
function validateLogin(username, password) {
3121
if (username.trim() !== "admin" || password.trim() !== "1234") {
3222
return false;
3323
}
3424
return true;
3525
}
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-
}
4226

43-
async function runAction(actionFunc, callback) {
44-
await actionFunc();
45-
await callback();
46-
}
4727
async function chooseAction() {
28+
/// do not quit after action, just keep cycling between prompt for action and action until ctrl+c
4829
let action = "";
4930
while (!validateChoice(action)) {
5031
console.log(`Exit: ${c.bgBlue("Ctrl + C")}`);
5132
console.log(`Read: ${c.bgBlue("R")}`);
5233
console.log(`Write: ${c.bgBlue("W")}`);
5334
action = await rl.question(``);
5435
}
55-
action === "w" ? runAction(writeDataToFile, chooseAction) : runAction(readFileData, chooseAction);
36+
action === "w" ? await writeDataToFile() : await readFileData();
37+
await chooseAction();
38+
}
39+
function validateChoice(action) {
40+
action = action.trim().toLowerCase();
41+
if (action === "r" || action === "read") return "r";
42+
if (action === "w" || action === "write") return "w";
43+
return "";
5644
}
5745

5846
async function writeDataToFile() {
@@ -74,3 +62,11 @@ async function readFileData() {
7462
console.log(c.red(err.message));
7563
}
7664
}
65+
function closeProgram(msg = "") {
66+
// console.clear(); //to clear password and read history from logs. doesn't clear all console, only visible area
67+
// https://stackoverflow.com/questions/8813142/clear-terminal-window-in-node-js-readline-shell
68+
process.stdout.write("\u001b[H\u001b[2J\u001b[3J"); //clear terminal with the scrollback, ansi escape code sequence
69+
rl.close();
70+
console.log(msg);
71+
process.exit(0);
72+
}

src/task-bonus.js

Whitespace-only changes.

0 commit comments

Comments
 (0)