-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfileHandler.js
More file actions
38 lines (29 loc) · 1.02 KB
/
Copy pathfileHandler.js
File metadata and controls
38 lines (29 loc) · 1.02 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
import fs from "fs";
import path from "path";
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Store files inside a `.data` folder
const dataDir = path.join(__dirname, '.data');
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir);
}
const taskFile = path.join(dataDir, "tasks.json");
const completedFile = path.join(dataDir, "compeletedTasks.json");
const readJSON = (filePath) => {
if (!fs.existsSync(filePath))
return [];
const content = fs.readFileSync(filePath, "utf-8");
try {
return JSON.parse(content);
} catch (error) {
return [];
}
}
const writeJSON = (filePath, data) => {
fs.writeFileSync(filePath, JSON.stringify(data, null, 2));
}
export const loadTasks = () => readJSON(taskFile);
export const loadCompletedTasks = () => readJSON(completedFile);
export const saveTasks = (data) => writeJSON(taskFile, data);
export const saveCompletedTasks = (data) => writeJSON(completedFile, data);