| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import fs from 'node:fs'; |
| b69ab31 | | | 9 | import path from 'node:path'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | /** |
| b69ab31 | | | 12 | * fs.promises.rm() was introduced in Node v14.14.0, so to in order to run in |
| b69ab31 | | | 13 | * Node v10, we must provide our own implementation. |
| b69ab31 | | | 14 | * |
| b69ab31 | | | 15 | * This functions like `rm -rf <file>`. |
| b69ab31 | | | 16 | */ |
| b69ab31 | | | 17 | export default async function rmtree(file: string): Promise<void> { |
| b69ab31 | | | 18 | let stat; |
| b69ab31 | | | 19 | try { |
| b69ab31 | | | 20 | stat = await fs.promises.lstat(file); |
| b69ab31 | | | 21 | } catch (error) { |
| b69ab31 | | | 22 | if ((error as NodeJS.ErrnoException).code === 'ENOENT') { |
| b69ab31 | | | 23 | // If the file does not exist, nothing to do! |
| b69ab31 | | | 24 | return; |
| b69ab31 | | | 25 | } else { |
| b69ab31 | | | 26 | throw error; |
| b69ab31 | | | 27 | } |
| b69ab31 | | | 28 | } |
| b69ab31 | | | 29 | |
| b69ab31 | | | 30 | if (stat.isDirectory()) { |
| b69ab31 | | | 31 | const stack = [file]; |
| b69ab31 | | | 32 | while (stack.length !== 0) { |
| b69ab31 | | | 33 | // eslint-disable-next-line no-await-in-loop |
| b69ab31 | | | 34 | await rmtreeIterative(stack); |
| b69ab31 | | | 35 | } |
| b69ab31 | | | 36 | } else { |
| b69ab31 | | | 37 | await fs.promises.unlink(file); |
| b69ab31 | | | 38 | } |
| b69ab31 | | | 39 | } |
| b69ab31 | | | 40 | |
| b69ab31 | | | 41 | /** |
| b69ab31 | | | 42 | * fs.promises.rm() was introduced in Node v14.14.0, so to in order to run in |
| b69ab31 | | | 43 | * Node v10, we must provide our own implementation. |
| b69ab31 | | | 44 | * |
| b69ab31 | | | 45 | * @param stack a list of folders to remove recursively. Folders at the end of |
| b69ab31 | | | 46 | * the array will be removed before preceding folders. |
| b69ab31 | | | 47 | */ |
| b69ab31 | | | 48 | async function rmtreeIterative(stack: Array<string>): Promise<void> { |
| b69ab31 | | | 49 | // This is effectively a "peek" on the stack. |
| b69ab31 | | | 50 | const folder = stack[stack.length - 1]; |
| b69ab31 | | | 51 | if (folder == null) { |
| b69ab31 | | | 52 | throw new Error(`invariant violation: empty stack`); |
| b69ab31 | | | 53 | } |
| b69ab31 | | | 54 | |
| b69ab31 | | | 55 | // We rely on the caller to ensure `folder` is a path to a directory rather |
| b69ab31 | | | 56 | // than stat each argument that was passed in. |
| b69ab31 | | | 57 | const files = await fs.promises.readdir(folder); |
| b69ab31 | | | 58 | |
| b69ab31 | | | 59 | const stackLength = stack.length; |
| b69ab31 | | | 60 | await Promise.all( |
| b69ab31 | | | 61 | files.map(async (file: string) => { |
| b69ab31 | | | 62 | const fullPath = path.join(folder, file); |
| b69ab31 | | | 63 | const stat = await fs.promises.lstat(fullPath); |
| b69ab31 | | | 64 | if (stat.isDirectory()) { |
| b69ab31 | | | 65 | stack.push(fullPath); |
| b69ab31 | | | 66 | } else { |
| b69ab31 | | | 67 | await fs.promises.unlink(fullPath); |
| b69ab31 | | | 68 | } |
| b69ab31 | | | 69 | }), |
| b69ab31 | | | 70 | ); |
| b69ab31 | | | 71 | |
| b69ab31 | | | 72 | // If nothing was pushed onto the stack, then we can assume this folder is |
| b69ab31 | | | 73 | // now empty and rmdir() will succeed. |
| b69ab31 | | | 74 | if (stack.length === stackLength) { |
| b69ab31 | | | 75 | await fs.promises.rmdir(folder); |
| b69ab31 | | | 76 | stack.pop(); |
| b69ab31 | | | 77 | } |
| b69ab31 | | | 78 | } |