File helper utility
npm install @scarafone/files-helper
/// Import
const {
closeFile,
deleteFile,
ensureDirectoryExistence,
createDirectory,
deleteDirectory,
readDirectory,
readFile,
writeFile
} = require("@scarafone/files-helper")
/// Execution
// Functions are synchronous
// General use case is for JSON formatted objects, or JS files.
const fileLocation = "./test/location/results.json"
// Write File
// If true, will ensure filepath exists before writing file
const file = writeFile(filePath: fileLocation, data: JSON.stringify({ "key": "value" }), shouldEnsurePathExists: true)
// Read File
// If true, it will return an empty file
file = readFile(filePath: fileLocation, options: {...options}, parseAsJSON: true ) // Default is true
// Delete File
// If true, it won't fail, but path will be created and deleted
const isSuccess = deleteFile(filePath: fileLocation, shouldEnsurePathExists: true)
`
Wrap in try/catch blocks in order to catch exceptions raised. Generally speaking though if shouldEnsurePathExists` is true the operation is not likely to fail in most cases.