| 3 | import path from "path"; |
| 4 | |
| 5 | export default class FileHandler { |
| 6 | static async parseJsonFile(filePath) { |
| 7 | try { |
| 8 | let data = await JSON.parse(await readFile(filePath, "utf8")); |
| 9 | return data; |
| 10 | } catch (err) { |
| 11 | console.error(err); |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | static async saveJsonFile(filePath, fileArr) { |
| 16 | try { |
| 17 | // Ensure directory exists |
| 18 | await fs.promises.mkdir(path.dirname(filePath), { recursive: true }); |
| 19 | |
| 20 | let data = await JSON.stringify(fileArr); |
| 21 | await writeFile(filePath, data); |
| 22 | console.log(`Successfully saved file list to ${filePath}.`); |
| 23 | } catch (err) { |
| 24 | console.error(err); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | static fileExists(filePath) { |
| 29 | return fs.existsSync(filePath); |
| 30 | } |
| 31 | |
| 32 | static async fileTime(filePath) { |
| 33 | try { |
| 34 | return fs.statSync(filePath).mtimeMs; |
| 35 | } catch (err) { |
| 36 | console.error(err); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | static async readFile(filePath) { |
| 41 | try { |
| 42 | return await readFile(filePath, "utf8"); |
| 43 | } catch (err) { |
| 44 | console.error(err); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | static async writeFile(filePath, data) { |
| 49 | try { |
| 50 | // Ensure directory exists |
| 51 | await fs.promises.mkdir(path.dirname(filePath), { recursive: true }); |
| 52 | |
| 53 | await writeFile(filePath, data); |
| 54 | } catch (err) { |
| 55 | console.error(err); |
| 56 | } |
| 57 | } |
| 58 | } |
nothing calls this directly
no outgoing calls
no test coverage detected