-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonEditor.js
More file actions
201 lines (176 loc) · 6.29 KB
/
Copy pathjsonEditor.js
File metadata and controls
201 lines (176 loc) · 6.29 KB
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
193
194
195
196
197
198
199
200
201
const fs = require('fs');
const path = require('path');
function jsonEditor(jsonDIR, exitBoolean) {
const currentModuleDir = path.dirname(require.main.filename);
const absoluteJsonDIR = path.resolve(currentModuleDir, jsonDIR);
global.exitBoolean = exitBoolean;
global.configJSON = require(absoluteJsonDIR);
global.JSONfile = configJSON;
global.jsonDIR = absoluteJsonDIR;
console.log('jsonEditor Loaded JSON: "' + absoluteJsonDIR + '"');
return {
read: (property) => read(property),
remove: (property) => remove(property),
add: (property, value) => add(property, value),
edit: (property, newValue) => edit(property, newValue),
createNested: (property) => createNested(property),
emptyNested: (property) => emptyNested(property),
createArray: (property) => createArray(property),
emptyArray: (property) => emptyArray(property),
clearAll: () => clearAll(),
};
}
// ----------------------------------------- update json ----------------------------------------
function updateJSON(configJSON) {
fs.writeFileSync(jsonDIR, JSON.stringify(configJSON, null, 2));
}
// ---------------------------------------- exit -----------------------------------------------
function exit() {
if (exitBoolean === true) {
process.exit();
} else if (exitBoolean === false) {
// Do nothing
} else {
exitBoolean = false;
}
}
// ---------------------------------------- value -----------------------------------------------
function clearAll() {
configJSON = {};
updateJSON(configJSON);
}
function read(property) {
if (configJSON[property] === undefined) {
console.log('Error: Cannot read Value: {' + property + '}\n' + 'Reason: Property does not exist');
exit();
} else {
return configJSON[property];
}
}
function add(property, value) {
const propertyPath = property.split('/');
let currentObject = configJSON;
let currentProperty = '';
for (let i = 0; i < propertyPath.length; i++) {
currentProperty = propertyPath[i];
if (Array.isArray(currentObject)) {
if (i === propertyPath.length - 1) {
currentObject.push(value);
updateJSON(configJSON);
return;
} else {
console.log('Error: Cannot add Value: {' + property + ', ' + value + '}\n' + 'Reason: Property is an array, not an object');
exit();
}
} else if (currentObject[currentProperty] === undefined) {
if (i === propertyPath.length - 1) {
currentObject[currentProperty] = value;
updateJSON(configJSON);
return;
} else {
currentObject[currentProperty] = {};
}
} else if (i === propertyPath.length - 1) {
console.log('Error: Cannot add Value: {' + property + ', ' + value + '}\n' + 'Reason: Property already exists');
exit();
}
currentObject = currentObject[currentProperty];
}
}
function remove(property) {
const propertyPath = property.split('/');
let currentObject = configJSON;
let currentProperty = '';
for (let i = 0; i < propertyPath.length; i++) {
currentProperty = propertyPath[i];
if (currentObject[currentProperty] === undefined) {
console.log('Error: Cannot delete Value: {' + property + '}\n' + 'Reason: Property does not exist');
exit();
} else if (i === propertyPath.length - 1) {
if (Array.isArray(currentObject)) {
const index = parseInt(currentProperty, 10);
if (!isNaN(index) && index >= 1 && index <= currentObject.length) {
currentObject.splice(index - 1, 1);
updateJSON(configJSON);
return;
} else {
console.log('Error: Cannot delete Value: {' + property + '}\n' + 'Reason: Invalid array index');
exit();
}
} else {
delete currentObject[currentProperty];
updateJSON(configJSON);
return;
}
}
currentObject = currentObject[currentProperty];
}
}
function edit(property, newValue) {
const propertyPath = property.split('/');
let currentObject = configJSON;
let currentProperty = '';
for (let i = 0; i < propertyPath.length; i++) {
currentProperty = propertyPath[i];
if (currentObject[currentProperty] === undefined) {
console.log('Error: Cannot edit Value: {' + property + '}\n' + 'Reason: Property does not exist');
exit();
} else if (i === propertyPath.length - 1) {
if (Array.isArray(currentObject)) {
const index = parseInt(currentProperty, 10);
if (!isNaN(index) && index >= 1 && index <= currentObject.length) {
currentObject[index - 1] = newValue;
updateJSON(configJSON);
return;
} else {
console.log('Error: Cannot edit Value: {' + property + '}\n' + 'Reason: Invalid array index');
exit();
}
} else {
currentObject[currentProperty] = newValue;
updateJSON(configJSON);
return;
}
}
currentObject = currentObject[currentProperty];
}
}
// ------------------------------------- nested object ------------------------------------------
function createNested(property) {
if (configJSON[property] === undefined) {
configJSON[property] = {};
updateJSON(configJSON);
} else {
console.log('Error: Cannot create Nested Object: {' + property + '}\n' + 'Reason: Property already exists');
exit();
}
}
function emptyNested(property) {
if (configJSON[property] === undefined || typeof configJSON[property] !== 'object') {
console.log('Error: Cannot empty Nested Object: {' + property + '}\n' + 'Reason: Property does not exist or is not a nested object');
exit();
} else {
configJSON[property] = {};
updateJSON(configJSON);
}
}
// ------------------------------------- array ------------------------------------------
function createArray(property) {
if (configJSON[property] === undefined) {
configJSON[property] = [];
updateJSON(configJSON);
} else {
console.log('Error: Cannot create Array: {' + property + '}\n' + 'Reason: Property already exists');
exit();
}
}
function emptyArray(property) {
if (configJSON[property] === undefined || !Array.isArray(configJSON[property])) {
console.log('Error: Cannot empty Array: {' + property + '}\n' + 'Reason: Property does not exist or is not an array');
exit();
} else {
configJSON[property] = [];
updateJSON(configJSON);
}
}
module.exports = jsonEditor;