forked from EBEREGIT/Nodejs_CLI_app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddData.js
63 lines (58 loc) · 1.32 KB
/
addData.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import inquirer from "inquirer";
import fs from "fs";
import { v4 as uuidv4 } from "uuid";
import queryDB from "./queryDB.js";
export default async function addData(info) {
try {
const answers = await inquirer.prompt([
{
type: "input",
name: "name",
message: "What's your name?",
},
{
type: "number",
name: "phone",
message: "What's your phone?",
},
{
type: "list",
name: "age",
message: "Are you an adult?",
choices: [
{ name: "Y", value: "Adult" },
{ name: "N", value: "Minor" },
],
},
]);
const data = {
id: uuidv4(),
name: answers.name,
phone: answers.phone,
age: answers.age,
};
info.push(data);
if (fs.existsSync("db.json")) {
createDetails(info);
} else {
fs.appendFile("db.json", "[]", (err) => {
if (err) {
console.log("Could not create db.json", err);
return;
}
createDetails(info);
});
}
} catch (error) {
console.log("Something went wrong!", error);
}
}
async function createDetails(info) {
await fs.writeFile("db.json", JSON.stringify(info), function (err) {
if (err) {
console.log(err);
}
console.log("saved!");
});
}
queryDB(addData);