-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.js
56 lines (49 loc) · 1.43 KB
/
storage.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
const fs = require('fs');
const path = require('path');
const os = require('os');
function getDocumentsPath() {
switch (process.platform) {
case 'win32':
return path.join(os.homedir(), 'Documents'); // Windows path
case 'darwin':
return path.join(os.homedir(), 'Documents'); // macOS path
case 'linux':
return path.join(os.homedir(), 'Documents'); // Linux path (if needed)
default:
throw new Error('Unsupported platform');
}
}
const documentsPath = getDocumentsPath();
const filePath = path.join(documentsPath, 'AmbientBuddy.json');
function saveJsonData(data) {
try {
const jsonData = JSON.stringify(data, null, 2); // Pretty-print JSON
fs.writeFileSync(filePath, jsonData, 'utf8');
console.log('Data successfully saved to', filePath);
} catch (error) {
console.error('Error saving JSON:', error);
}
}
function loadJsonData() {
try {
if (!fs.existsSync(filePath)) {
console.log('File does not exist:', filePath);
return null;
}
const jsonData = fs.readFileSync(filePath, 'utf8');
return JSON.parse(jsonData);
} catch (error) {
console.error('Error reading JSON:', error);
return null;
}
}
const myData = {
name: "John Doe",
age: 30,
job: "Software Engineer"
};
module.exports = {
myData,
saveJsonData,
loadJsonData
};