|
1 |
| -import { generateRandomUserDataString as newEntry } from "./generateRandomUserDataString.js"; |
2 | 1 | import c from "chalk";
|
3 | 2 | import { writeFile, mkdir, readFile } from "node:fs/promises";
|
4 | 3 | import readline from "node:readline/promises";
|
| 4 | +import { validate } from "./validations.js"; |
| 5 | + |
5 | 6 | const fileName = "registration.json";
|
6 | 7 | const dirPath = new URL("./../database/task-bonus", import.meta.url).pathname;
|
7 |
| - |
8 |
| -let rl = readline.createInterface({ input: process.stdin, output: process.stdout }); |
| 8 | +const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); |
| 9 | +/// close program by clearing console on ctrl+c |
9 | 10 | rl.on("SIGINT", closeProgram); //doesn't work on process.on('SIGINT') SIGINT is emitted on ctrl+c press
|
10 | 11 |
|
11 |
| -async function mainFunc() { |
12 |
| - console.log(c.dim("Exit: Ctrl + C")); |
13 |
| - console.log(c.yellow.bold("Enter your details: ")); |
14 |
| - let details = await getUserDetails(); |
15 |
| - let currentData = JSON.parse(await readFileData()); |
16 |
| - console.log(typeof currentData, currentData); |
17 |
| - currentData.push(details); |
18 |
| - console.log(currentData); |
19 |
| - await writeDataToFile(JSON.stringify(currentData)); |
20 |
| - closeProgram(c.green("Duomenys faile sėkmingai išsaugoti")); |
21 |
| -} |
| 12 | +/// Intro messages |
| 13 | +printIntro(); |
| 14 | + |
| 15 | +/// Get registration inputs |
| 16 | +let details = await getUserDetails(); |
| 17 | + |
| 18 | +/// Read adn parse current file data |
| 19 | +let currentData = JSON.parse(await readFileData()); |
| 20 | + |
| 21 | +/// Append new data to array |
| 22 | +currentData.push(details); |
| 23 | + |
| 24 | +/// Save new updated array to file as json |
| 25 | +await writeDataToFile(JSON.stringify(currentData)); |
| 26 | + |
| 27 | +/// Log Success message |
| 28 | +closeProgram(c.green("Duomenys faile sėkmingai išsaugoti")); |
22 | 29 |
|
23 | 30 | async function getUserDetails() {
|
24 | 31 | let userDetails = {
|
25 |
| - name: await getValidInput.name(), |
26 |
| - lastName: await getValidInput.lastName(), |
27 |
| - password: await getValidInput.password(), |
28 |
| - email: await getValidInput.email(), |
29 |
| - birthday: await getValidInput.birthday(), |
| 32 | + name: await getValidInput("Your name: ", [validate.isMaxLength(50)]), |
| 33 | + lastName: await getValidInput("Your last name ", [validate.isMaxLength(50)]), |
| 34 | + password: await getValidInput("Your password: ", [validate.isMinLength(9), validate.isMaxLength(50)]), |
| 35 | + email: await getValidInput("Your email address: ", [validate.isValidEmail]), |
| 36 | + birthday: await getValidInput("Your birthday YYYY-MM-DD: ", [validate.isValidBirthdayFormat]), |
30 | 37 | };
|
31 | 38 | return userDetails;
|
32 | 39 | }
|
33 | 40 |
|
34 |
| -const getValidInput = { |
35 |
| - name: async function () { |
36 |
| - let name = ""; |
37 |
| - let errorMsg = ""; |
38 |
| - while (!name) { |
39 |
| - if (errorMsg) console.log(c.red(errorMsg)); |
40 |
| - name = (await rl.question(c.bgBlue.bold("Your name:") + " ")).trim(); |
41 |
| - if (!name) { |
42 |
| - errorMsg = "Name can't be empty"; |
43 |
| - } |
44 |
| - } |
45 |
| - return name; |
46 |
| - }, |
47 |
| - lastName: async function () { |
48 |
| - let lastName = ""; |
49 |
| - let errorMsg = ""; |
50 |
| - while (!lastName) { |
51 |
| - if (errorMsg) console.log(c.red(errorMsg)); |
52 |
| - lastName = (await rl.question(c.bgBlue.bold("Your last name:") + " ")).trim(); |
53 |
| - if (!lastName) { |
54 |
| - errorMsg = "Last name can't be empty"; |
55 |
| - } |
| 41 | +async function getValidInput(questionStr, validateArr = []) { |
| 42 | + validateArr = [validate.isNotEmpty, ...validateArr]; //add default check for empty values |
| 43 | + let input = ""; |
| 44 | + let errorMsg = ""; |
| 45 | + while (!input) { |
| 46 | + if (errorMsg) console.log(c.red(errorMsg)); |
| 47 | + let rawInput = (await rl.question(c.bgBlue.bold(questionStr))).trim(); |
| 48 | + try { |
| 49 | + validateArr.forEach((validateFunc) => { |
| 50 | + input = ""; |
| 51 | + input = validateFunc(rawInput); |
| 52 | + }); |
| 53 | + } catch (validationError) { |
| 54 | + errorMsg = validationError.message; |
56 | 55 | }
|
57 |
| - return lastName; |
58 |
| - }, |
59 |
| - password: async function () { |
60 |
| - let password = ""; |
61 |
| - let errorMsg = ""; |
62 |
| - while (!password) { |
63 |
| - if (errorMsg) console.log(c.red(errorMsg)); |
64 |
| - password = (await rl.question(c.bgBlue.bold("Your password:") + " ")).trim(); |
65 |
| - if (!password) { |
66 |
| - errorMsg = "Password can't be empty"; |
67 |
| - } |
68 |
| - } |
69 |
| - return password; |
70 |
| - }, |
71 |
| - email: async function () { |
72 |
| - let email = ""; |
73 |
| - let errorMsg = ""; |
74 |
| - while (!email) { |
75 |
| - if (errorMsg) console.log(c.red(errorMsg)); |
76 |
| - email = (await rl.question(c.bgBlue.bold("Your email address:") + " ")).trim(); |
77 |
| - if (!email) { |
78 |
| - errorMsg = "Email can't be empty"; |
79 |
| - } |
80 |
| - } |
81 |
| - return email; |
82 |
| - }, |
83 |
| - birthday: async function () { |
84 |
| - let birthday = ""; |
85 |
| - let errorMsg = ""; |
86 |
| - while (!birthday) { |
87 |
| - if (errorMsg) console.log(c.red(errorMsg)); |
88 |
| - birthday = (await rl.question(c.bgBlue.bold("Your birthday:") + " ")).trim(); |
89 |
| - if (!birthday) { |
90 |
| - errorMsg = "Birthday can't be empty"; |
91 |
| - } |
92 |
| - } |
93 |
| - return birthday; |
94 |
| - }, |
95 |
| -}; |
96 |
| -mainFunc(); |
| 56 | + } |
| 57 | + return input; |
| 58 | +} |
97 | 59 |
|
98 | 60 | async function writeDataToFile(data) {
|
99 | 61 | try {
|
100 | 62 | await mkdir(dirPath, { recursive: true });
|
101 |
| - await writeFile(`${dirPath}/${fileName}`, data); ///kelias ne nuo sito failo, bet nuo project rooto??... |
102 |
| - // console.log(c.green("Duomenys faile sėkmingai išsaugoti")); |
| 63 | + await writeFile(`${dirPath}/${fileName}`, data); |
103 | 64 | } catch (err) {
|
104 |
| - console.error(err); |
| 65 | + console.error(c.red(err.message)); |
105 | 66 | }
|
106 | 67 | }
|
107 | 68 |
|
108 | 69 | async function readFileData() {
|
109 | 70 | try {
|
110 |
| - // await mkdir(dirPath, { recursive: true }); |
111 | 71 | return await readFile(`${dirPath}/${fileName}`);
|
112 | 72 | } catch (err) {
|
113 |
| - console.log("chould add empty"); |
114 | 73 | return "[]"; //if file empty or not created
|
115 | 74 | }
|
116 | 75 | }
|
| 76 | + |
| 77 | +function printIntro() { |
| 78 | + console.clear(); |
| 79 | + console.log(c.yellow.bold(` REGISTRATION`)); |
| 80 | + console.log(c.dim("Exit: Ctrl + C")); |
| 81 | + console.log(c.yellow.bold("Enter your details: ")); |
| 82 | +} |
| 83 | + |
117 | 84 | function closeProgram(msg = "") {
|
118 | 85 | // console.clear(); //to clear password and read history from logs. doesn't clear all console, only visible area
|
119 | 86 | // https://stackoverflow.com/questions/8813142/clear-terminal-window-in-node-js-readline-shell
|
|
0 commit comments