1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-12-08 00:45:29 +01:00
programming-challenges/scripts/utils/deleteAllFilesExceptOne.ts
2020-07-05 15:48:51 +02:00

19 lines
658 B
TypeScript

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