(newData: Data[], filePath: string = "data.json")
| 48 | |
| 49 | |
| 50 | export const saveDataToFile = (newData: Data[], filePath: string = "data.json") => { |
| 51 | try { |
| 52 | let existingData: Data[] = []; |
| 53 | |
| 54 | // Check if the file exists |
| 55 | if (fs.existsSync(filePath)) { |
| 56 | // If the file exists, read its content |
| 57 | const fileContent = fs.readFileSync(filePath, 'utf-8'); |
| 58 | existingData = JSON.parse(fileContent); |
| 59 | } |
| 60 | |
| 61 | // Add the new data to the existing array |
| 62 | existingData.push(...newData); |
| 63 | |
| 64 | // Write the updated data back to the file |
| 65 | fs.writeFileSync(filePath, JSON.stringify(existingData, null, 2)); |
| 66 | |
| 67 | } catch (error) { |
| 68 | try { |
| 69 | if (fs.existsSync(filePath)) { |
| 70 | fs.unlinkSync(filePath); |
| 71 | console.log(`File ${filePath} deleted and create new file.`); |
| 72 | } |
| 73 | fs.writeFileSync(filePath, JSON.stringify(newData, null, 2)); |
| 74 | console.log("File is saved successfully.") |
| 75 | } catch (error) { |
| 76 | console.log('Error saving data to JSON file:', error); |
| 77 | } |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | export const sleep = async (ms: number) => { |
| 82 | await new Promise((resolve) => setTimeout(resolve, ms)) |
no outgoing calls
no test coverage detected