-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileOperations.js
192 lines (164 loc) · 7.93 KB
/
fileOperations.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const fs = require("fs")
const logger = require("./logger")
const fileName = "notes.json"
class FileOperations {
checkFileStatus(showLogs = true) {
try {
logger.heading("🥁 Checking if files exist or not", showLogs)
if (!fs.existsSync(fileName)) {
logger.info("☑️ File doesnt exists! Creating one", showLogs)
fs.appendFileSync(fileName, "");
}
else {
logger.info("☑️ File exists!", showLogs)
}
} catch (error) {
loggger.error(`❌ Error in append operation: ${error}`)
}
}
write(operationDetail, showLogs = true) {
try {
if (Object.keys(operationDetail).includes("title") && Object.keys(operationDetail).includes("body")) {
logger.heading(`🥁 Performing write operation for --> ${operationDetail.title}`, showLogs)
logger.info(`🕵️♂️ Checking if Title:${operationDetail.title} exists or not`, showLogs)
let allList = this.list(false)
let data = null
allList == null || allList == "" || allList == undefined ? allList = {} : allList = allList
if (Object.keys(allList).length != 0) {
data = this.show(operationDetail, false)
if (data) {
logger.info(`🙅 It already exists`, showLogs)
}
}
if (!data) {
logger.info(`🏃 Making fresh entry`, showLogs)
allList[operationDetail.title] = ({ title: operationDetail.title, body: operationDetail.body })
fs.writeFileSync(fileName, JSON.stringify(allList), (error) => {
if (error)
throw error
})
}
}
else
!operationDetail.title && operationDetail.body ? logger.warn("❌ Error in write operation: Title Not found") : !operationDetail.body && operationDetail.title ? logger.warn("❌ Error in write operation: Body not found") : logger.warn("❌ Error in write operation: No title and body found")
logger.info("🎉 Completed write operation (with or without error)")
}
catch (error) {
logger.error(`❌ Error in write operation: ${error}`)
}
}
list(showLogs = true) {
try {
logger.heading("🥁 Performing list operation!", showLogs)
const data = JSON.parse(fs.readFileSync(fileName))
if (data != null || data != "") {
let tableData = []
Object.keys(data).map((item, index) => {
tableData.push(data[item])
})
if (showLogs) console.table(tableData, ["title", "body"])
return data;
}
logger.info("🎉 Completed list operation (with or without error)")
}
catch (error) {
if (error == "SyntaxError: Unexpected end of JSON input")
logger.warn("❎ List operation: List is empty! Nothing to show!", false)
else
logger.error(`❌ Error in list operation: ${error}`)
}
}
show(operationDetail, showLogs = true) {
try {
if (Object.keys(operationDetail).includes("title")) {
logger.heading(`🥁 Performing show operation for --> ${operationDetail.title}`, showLogs)
logger.info(`🔎 Finding details for Title:${operationDetail.title}`, showLogs)
const data = this.list(false)
if (Object.keys(data).includes(operationDetail.title)) {
logger.info("🎉 Found the information", showLogs)
if (showLogs) console.table(data[operationDetail.title])
return data[operationDetail.title]
}
else {
logger.warn("❌ The item doesnt exist", showLogs)
return false
}
}
else
logger.warn("❌ Error in show operation: Title Not found", showLogs)
logger.info("🎉 Completed show operation (with or without error)")
}
catch (error) {
logger.error(`❌ Error in show operation: ${error}`)
}
}
removeAll(showLogs = true) {
try {
logger.heading(`🥁 Performing remove all operation`, showLogs)
fs.unlinkSync(fileName);
logger.info("🎉 Completed remove-all operation (with or without error)")
}
catch (error) {
logger.error(`❌ Error in remove all operation:${error}`)
}
}
removeOne(operationDetail, showLogs = true) {
try {
const allList = this.list(false)
if (allList && Object.keys(operationDetail).includes("title")) {
logger.heading(`🥁 Performing remove operation for --> ${operationDetail.title}`, showLogs)
logger.info(`🕵️♂️ Checking if Title:${operationDetail.title} exists or not`, showLogs)
const data = this.show(operationDetail, false)
if (!data) {
logger.warn(`🙅 Element you are trying to delete doesnt exist`, showLogs)
}
if (data) {
logger.info(`🏃 Deleting the entry`, showLogs)
let allList = this.list(false)
delete (allList[data.title])
fs.writeFileSync(fileName, JSON.stringify(allList), (error) => {
if (error)
throw error
})
}
}
else
!allList ? logger.warn("❌ Error in remove operation:List is empty! Nothing to delete") : logger.warn("❌ Error in remove operation: Title Not found", showLogs)
logger.info("🎉 Completed remove-one operation (with or without error)")
}
catch (error) {
logger.error(`❌ Error in remove operation: ${error}`)
}
}
update(operationDetail, showLogs = true) {
try {
const allList = this.list(false)
if (allList && Object.keys(operationDetail).includes("title") && (Object.keys(operationDetail).includes("new-title") || Object.keys(operationDetail).includes("new-body"))) {
logger.heading(`🥁 Performing update operation for --> ${operationDetail.title}`, showLogs)
logger.info(`♻️ Updating for Title:${operationDetail.title}`, showLogs)
let data = this.show(operationDetail, false)
this.removeOne(operationDetail, false)
if (Object.keys(operationDetail).includes("new-title"))
data.title = operationDetail["new-title"]
if (Object.keys(operationDetail).includes("new-body"))
data.body = operationDetail["new-body"]
let allList = this.list(false)
allList[data.title] = data;
fs.writeFileSync(fileName, JSON.stringify(allList), (error) => {
if (error)
throw error
})
}
else
!allList ? logger.warn("❌ Error in update operation: List is empty! Nothing to update") : !operationDetail.title ? logger.error("❌ Error in update operation: Title Not found") : !operationDetail["new-title"] && !operationDetail["new-body"] ? logger.error("❌ Error in update operation: New body or title not found") : null
logger.info("🎉 Completed update operation (with or without error)")
}
catch (error) {
logger.error(`❌ Error in update operation: ${error}`)
}
}
errorOperation(operation) {
logger.error(`❌ Error: Invalid Operation --> ${operation}`)
}
}
module.exports = new FileOperations()