| 9 | * @param {string} filepath - The file or directory to remove. |
| 10 | */ |
| 11 | export async function rmRecursive(fs, filepath) { |
| 12 | const entries = await fs.readdir(filepath) |
| 13 | if (entries == null) { |
| 14 | await fs.rm(filepath) |
| 15 | } else if (entries.length) { |
| 16 | await Promise.all( |
| 17 | entries.map(entry => { |
| 18 | const subpath = join(filepath, entry) |
| 19 | return fs.lstat(subpath).then(stat => { |
| 20 | if (!stat) return |
| 21 | return stat.isDirectory() ? rmRecursive(fs, subpath) : fs.rm(subpath) |
| 22 | }) |
| 23 | }) |
| 24 | ).then(() => fs.rmdir(filepath)) |
| 25 | } else { |
| 26 | await fs.rmdir(filepath) |
| 27 | } |
| 28 | } |