File size: 497 Bytes
9a42933 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import { existsSync, promises as fs } from "node:fs"
export const deleteFileIfExists = async (filePath: string) => {
const safePath = filePath.trim()
// just a sanity check
if (safePath.includes("*") ||safePath === "/" || safePath === "~" || safePath === ".") {
throw new Error(`lol, no.`)
}
if (existsSync(filePath)) {
try {
await fs.unlink(safePath)
return true
} catch (err) {
console.log(`failed to delete file ${safePath}`)
}
}
return false
} |