2019-03-05 14:01:42 +01:00
|
|
|
import fs from 'fs'
|
|
|
|
import { join } from 'path'
|
|
|
|
import { promisify } from 'util'
|
|
|
|
|
|
|
|
const readdir = promisify(fs.readdir)
|
|
|
|
const stat = promisify(fs.stat)
|
|
|
|
const rmdir = promisify(fs.rmdir)
|
|
|
|
const unlink = promisify(fs.unlink)
|
|
|
|
const sleep = promisify(setTimeout)
|
|
|
|
|
|
|
|
const unlinkFile = async (p: string, t = 1): Promise<void> => {
|
|
|
|
try {
|
|
|
|
await unlink(p)
|
|
|
|
} catch (e) {
|
2019-04-11 23:09:12 +02:00
|
|
|
if (
|
|
|
|
(e.code === 'EBUSY' ||
|
|
|
|
e.code === 'ENOTEMPTY' ||
|
|
|
|
e.code === 'EPERM' ||
|
|
|
|
e.code === 'EMFILE') &&
|
|
|
|
t < 3
|
|
|
|
) {
|
2019-03-05 14:01:42 +01:00
|
|
|
await sleep(t * 100)
|
|
|
|
return unlinkFile(p, t++)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.code === 'ENOENT') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
throw e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recursively delete directory contents
|
|
|
|
* @param {string} dir Directory to delete the contents of
|
2019-11-19 17:40:13 +01:00
|
|
|
* @param {RegExp} [exclude] Exclude based on relative file path
|
2019-04-16 15:56:34 +02:00
|
|
|
* @param {string} [previousPath] Ensures that parameter dir exists, this is not passed recursively
|
2019-03-05 14:01:42 +01:00
|
|
|
* @returns Promise void
|
|
|
|
*/
|
2019-04-11 23:09:12 +02:00
|
|
|
export async function recursiveDelete(
|
|
|
|
dir: string,
|
2019-11-19 17:40:13 +01:00
|
|
|
exclude?: RegExp,
|
|
|
|
previousPath: string = ''
|
2019-04-11 23:09:12 +02:00
|
|
|
): Promise<void> {
|
2019-03-05 14:01:42 +01:00
|
|
|
let result
|
|
|
|
try {
|
|
|
|
result = await readdir(dir)
|
|
|
|
} catch (e) {
|
2019-11-19 17:40:13 +01:00
|
|
|
if (e.code === 'ENOENT') {
|
2019-04-11 23:09:12 +02:00
|
|
|
return
|
|
|
|
}
|
2019-03-05 14:01:42 +01:00
|
|
|
throw e
|
|
|
|
}
|
|
|
|
|
2019-04-11 23:09:12 +02:00
|
|
|
await Promise.all(
|
|
|
|
result.map(async (part: string) => {
|
|
|
|
const absolutePath = join(dir, part)
|
2019-05-29 13:57:26 +02:00
|
|
|
const pathStat = await stat(absolutePath).catch(e => {
|
2019-04-11 23:09:12 +02:00
|
|
|
if (e.code !== 'ENOENT') throw e
|
|
|
|
})
|
|
|
|
if (!pathStat) {
|
|
|
|
return
|
|
|
|
}
|
2019-03-05 14:01:42 +01:00
|
|
|
|
2019-11-19 17:40:13 +01:00
|
|
|
const pp = join(previousPath, part)
|
|
|
|
if (pathStat.isDirectory() && (!exclude || !exclude.test(pp))) {
|
|
|
|
await recursiveDelete(absolutePath, exclude, pp)
|
|
|
|
return rmdir(absolutePath)
|
2019-04-11 23:09:12 +02:00
|
|
|
}
|
|
|
|
|
2019-11-19 17:40:13 +01:00
|
|
|
if (!exclude || !exclude.test(pp)) {
|
2019-04-11 23:09:12 +02:00
|
|
|
return unlinkFile(absolutePath)
|
|
|
|
}
|
2019-05-29 13:57:26 +02:00
|
|
|
})
|
2019-04-11 23:09:12 +02:00
|
|
|
)
|
2019-03-05 14:01:42 +01:00
|
|
|
}
|