|
| 1 | +import { generateRandomUserDataString as newEntry } from "./generateRandomUserDataString.js"; |
| 2 | +import c from "chalk"; |
| 3 | +import { writeFile, mkdir, readFile } from "node:fs/promises"; |
| 4 | +import readline from "node:readline/promises"; |
| 5 | +const fileName = "registration.json"; |
| 6 | +const dirPath = new URL("./../database/task-bonus", import.meta.url).pathname; |
| 7 | + |
| 8 | +let rl = readline.createInterface({ input: process.stdin, output: process.stdout }); |
| 9 | +rl.on("SIGINT", closeProgram); //doesn't work on process.on('SIGINT') SIGINT is emitted on ctrl+c press |
| 10 | + |
| 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 | +} |
| 22 | + |
| 23 | +async function getUserDetails() { |
| 24 | + 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(), |
| 30 | + }; |
| 31 | + return userDetails; |
| 32 | +} |
| 33 | + |
| 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 | + } |
| 56 | + } |
| 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(); |
| 97 | + |
| 98 | +async function writeDataToFile(data) { |
| 99 | + try { |
| 100 | + 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")); |
| 103 | + } catch (err) { |
| 104 | + console.error(err); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +async function readFileData() { |
| 109 | + try { |
| 110 | + // await mkdir(dirPath, { recursive: true }); |
| 111 | + return await readFile(`${dirPath}/${fileName}`); |
| 112 | + } catch (err) { |
| 113 | + console.log("chould add empty"); |
| 114 | + return "[]"; //if file empty or not created |
| 115 | + } |
| 116 | +} |
| 117 | +function closeProgram(msg = "") { |
| 118 | + // console.clear(); //to clear password and read history from logs. doesn't clear all console, only visible area |
| 119 | + // https://stackoverflow.com/questions/8813142/clear-terminal-window-in-node-js-readline-shell |
| 120 | + process.stdout.write("\u001b[H\u001b[2J\u001b[3J"); //clear terminal with the scrollback, ansi escape code sequence |
| 121 | + rl.close(); |
| 122 | + console.log(msg); |
| 123 | + process.exit(0); |
| 124 | +} |
0 commit comments