(filename: string = "data.json")
| 93 | |
| 94 | // Function to read JSON file |
| 95 | export function readJson(filename: string = "data.json"): Data[] { |
| 96 | if (!fs.existsSync(filename)) { |
| 97 | // If the file does not exist, create an empty array |
| 98 | fs.writeFileSync(filename, '[]', 'utf-8'); |
| 99 | } |
| 100 | const data = fs.readFileSync(filename, 'utf-8'); |
| 101 | return JSON.parse(data) as Data[]; |
| 102 | } |
| 103 | |
| 104 | // Function to write JSON file |
| 105 | export function writeJson( data: Data[], filename: string = "data.json",): void { |