All files / fs / unstable_remove.ts

33.33% Branches 1/3
30.43% Lines 14/46
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
 
 
 
x9
x9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x9
x9
x9
 
x53
x53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x9
x9
x9
 
x46
x46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x46




































I
















































I















// Copyright 2018-2025 the Deno authors. MIT license.

import type { RemoveOptions } from "./unstable_types.ts";
import { getNodeFs, isDeno } from "./_utils.ts";
import { mapError } from "./_map_error.ts";

/**
 * Removes the named file or directory.
 *
 * Throws error if permission denied, path not found, or path is a non-empty directory and
 * the recursive option isn't set to true.
 *
 * Requires `allow-write` permission.
 *
 * @example Usage
 * ```ts
 * import { assertFalse } from "@std/assert";
 * import { exists } from "@std/fs/exists";
 * import { remove } from "@std/fs/unstable-remove";
 * import { makeTempDir } from "@std/fs/unstable-make-temp-dir";
 *
 * const tempDir = await makeTempDir();
 * await remove(tempDir);
 * assertFalse(await exists(tempDir));
 * ```
 *
 * @tags allow-write
 *
 * @param path The path of file or directory.
 * @param options Options when reading a file. See {@linkcode RemoveOptions}.
 */
export async function remove(
  path: string | URL,
  options?: RemoveOptions,
) {
  if (isDeno) {
    await Deno.remove(path, options);
  } else {
    const { recursive = false } = options ?? {};
    try {
      await getNodeFs().promises.rm(path, { recursive: recursive });
    } catch (error) {
      if ((error as Error & { code: string }).code === "ERR_FS_EISDIR") {
        try {
          await getNodeFs().promises.rmdir(path);
        } catch (error) {
          throw mapError(error);
        }
        return;
      }
      throw mapError(error);
    }
  }
}

/**
 * Synchronously removes the named file or directory.
 *
 * Throws error if permission denied, path not found, or path is a non-empty directory and
 * the recursive option isn't set to true.
 *
 * Requires `allow-write` permission.
 *
 * @example Usage
 * ```ts
 * import { assertFalse } from "@std/assert";
 * import { existsSync } from "@std/fs/exists";
 * import { removeSync } from "@std/fs/unstable-remove";
 * import { makeTempDirSync } from "@std/fs/unstable-make-temp-dir";
 *
 * const tempDir = makeTempDirSync();
 * removeSync(tempDir);
 * assertFalse(existsSync(tempDir));
 * ```
 *
 * @tags allow-write
 *
 * @param path The path of file or directory.
 * @param options Options when reading a file. See {@linkcode RemoveOptions}.
 */
export function removeSync(
  path: string | URL,
  options?: RemoveOptions,
) {
  if (isDeno) {
    Deno.removeSync(path, options);
  } else {
    const { recursive = false } = options ?? {};
    try {
      getNodeFs().rmSync(path, { recursive: recursive });
    } catch (error) {
      if ((error as Error & { code: string }).code === "ERR_FS_EISDIR") {
        try {
          getNodeFs().rmdirSync(path);
        } catch (error) {
          throw mapError(error);
        }
        return;
      }
      throw mapError(error);
    }
  }
}