my-abdokhalifa-cafe-manager 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +85 -0
  2. package/items.json +1 -0
  3. package/package.json +18 -0
package/index.js ADDED
@@ -0,0 +1,85 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { Command } from 'commander';
4
+ import fs from 'fs';
5
+
6
+ import inquirer from 'inquirer';
7
+
8
+ const program = new Command();
9
+
10
+ const filePath = "./items.json";
11
+
12
+ const questions = [
13
+ {type: 'input',
14
+ name: "itemTitle",
15
+ message: "what's the item title?",
16
+ },
17
+ {type: 'number',
18
+ name: "itemPrice",
19
+ message: "what's the item price?",
20
+ }
21
+ ];
22
+
23
+ program
24
+ .name('trying to understand node js')
25
+ .description('A simple CLI to understand Node.js')
26
+ .version('1.0.0');
27
+
28
+ program
29
+ .command("add")
30
+ .alias("a")
31
+ .description("Add a new item")
32
+ // .argument("itemTitle", "The title of the item to add")
33
+ // .option("--itemPrice <price>", "The price of the item to add")
34
+ .action((param, option) => {
35
+ // console.log("param, option", param, option);
36
+ inquirer
37
+ .prompt(questions)
38
+ .then((answers) => {
39
+ console.log("answers", answers);
40
+
41
+ if (!fs.existsSync(filePath)) {
42
+
43
+ fs.writeFile(filePath, JSON.stringify([answers]), "utf8", (err) => {
44
+ if (err) {
45
+ console.error("Error writing to file", err);
46
+ } else {
47
+ console.log("Item added successfully!");
48
+ }
49
+ });
50
+ } else {
51
+ fs.readFile(filePath, "utf8", (err, fileData) => {
52
+ if (err) {
53
+ console.error("Error reading file", err);
54
+ process.exit();
55
+ }
56
+ console.log("fileData: ", fileData);
57
+ const items = JSON.parse(fileData);
58
+ items.push(answers);
59
+ fs.writeFile(filePath, JSON.stringify(items), "utf8", (err) => {
60
+ if (err) {
61
+ console.error("Error writing to file", err);
62
+ } else {
63
+ console.log("Item added successfully!");
64
+ }
65
+ });
66
+ });
67
+ }
68
+ });
69
+ });
70
+
71
+ program
72
+ .command("list")
73
+ .alias("l")
74
+ .description("List all items")
75
+ .action(() => {
76
+ fs.readFile(filePath, "utf8", (err, fileData) => {
77
+ if (err) {
78
+ console.error("Error reading file", err);
79
+ process.exit();
80
+ }
81
+ console.table(JSON.parse(fileData));
82
+ });
83
+ });
84
+
85
+ program.parse(process.argv);
package/items.json ADDED
@@ -0,0 +1 @@
1
+ [{"itemTitle":"chocholate","itemPrice":500},{"itemTitle":"coffee","itemPrice":100},{"itemTitle":"tea","itemPrice":20}]
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "my-abdokhalifa-cafe-manager",
3
+ "version": "1.0.0",
4
+ "main": "index.js",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1"
7
+ },
8
+ "bin": "./index.js",
9
+ "keywords": [],
10
+ "author": "",
11
+ "license": "ISC",
12
+ "type": "module",
13
+ "description": "",
14
+ "dependencies": {
15
+ "commander": "^14.0.3",
16
+ "inquirer": "^13.4.1"
17
+ }
18
+ }