1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-07-18 02:20:12 +02:00
programming-challenges/scripts/utils/deleteAllFilesExceptOne.ts

19 lines
658 B
TypeScript
Raw Normal View History

2020-07-05 15:48:51 +02:00
import path from 'path'
import * as fsWithCallbacks from 'fs'
const fs = fsWithCallbacks.promises
async function deleteAllFilesExceptOne (directoryPath: string, fileNameToNotDelete: string) {
const fileNames = await fs.readdir(path.resolve(directoryPath))
for (const name of fileNames) {
const fileNamePath = path.resolve(directoryPath, name)
const stats = await fs.stat(fileNamePath)
if (stats.isDirectory()) {
await fs.rmdir(fileNamePath, { recursive: true })
} else if (name !== fileNameToNotDelete) {
await fs.unlink(fileNamePath)
}
}
}
export default deleteAllFilesExceptOne