2020-07-05 15:48:51 +02:00
|
|
|
import path from 'path'
|
|
|
|
import * as fsWithCallbacks from 'fs'
|
|
|
|
const fs = fsWithCallbacks.promises
|
|
|
|
|
2021-02-18 19:40:44 +01:00
|
|
|
async function deleteAllFilesExceptOne(
|
|
|
|
directoryPath: string,
|
|
|
|
fileNameToNotDelete: string
|
|
|
|
): Promise<void> {
|
|
|
|
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)
|
2020-07-05 15:48:51 +02:00
|
|
|
}
|
2021-02-18 19:40:44 +01:00
|
|
|
}
|
2020-07-05 15:48:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default deleteAllFilesExceptOne
|