All files / fs / unstable_rename.ts

33.33% Branches 1/3
46.15% Lines 12/26
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
 
 
x1
x1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1
x1
x1
 
x10
x10
 
 
 
 
 
 
 
x10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1
x11
x11
 
 
 
 
 
 
 
x11



































I



































I






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

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

/**
 * Renames (moves) `oldpath` to `newpath`. Paths may be files or directories.
 * If `newpath` already exists and is not a directory, `rename()` replaces it.
 * OS-specific restrictions may apply when `oldpath` and `newpath` are in
 * different directories.
 *
 * On Unix-like OSes, this operation does not follow symlinks at either path.
 *
 * It varies between platforms when the operation throws errors, and if so
 * what they are. It's always an error to rename anything to a non-empty
 * directory.
 *
 * Requires `allow-read` and `allow-write` permissions.
 *
 * @example Usage
 * ```ts ignore
 * import { rename } from "@std/fs/unstable-rename";
 * await rename("old/path", "new/path");
 * ```
 *
 * @tags allow-read, allow-write
 *
 * @param oldpath The current name/path of the file/directory.
 * @param newpath The updated name/path of the file/directory.
 */
export async function rename(
  oldpath: string | URL,
  newpath: string | URL,
): Promise<void> {
  if (isDeno) {
    await Deno.rename(oldpath, newpath);
  } else {
    try {
      await getNodeFs().promises.rename(oldpath, newpath);
    } catch (error) {
      throw mapError(error);
    }
  }
}

/**
 * Synchronously renames (moves) `oldpath` to `newpath`. Paths may be files or
 * directories. If `newpath` already exists and is not a directory,
 * `renameSync()` replaces it. OS-specific restrictions may apply when
 * `oldpath` and `newpath` are in different directories.
 *
 * On Unix-like OSes, this operation does not follow symlinks at either path.
 *
 * It varies between platforms when the operation throws errors, and if so what
 * they are. It's always an error to rename anything to a non-empty directory.
 *
 * Requires `allow-read` and `allow-write` permissions.
 *
 * @example Usage
 * ```ts ignore
 * import { renameSync } from "@std/fs/unstable-rename";
 * renameSync("old/path", "new/path");
 * ```
 *
 * @tags allow-read, allow-write
 *
 * @param oldpath The current name/path of the file/directory.
 * @param newpath The updated name/path of the file/directory.
 */
export function renameSync(oldpath: string | URL, newpath: string | URL): void {
  if (isDeno) {
    Deno.renameSync(oldpath, newpath);
  } else {
    try {
      getNodeFs().renameSync(oldpath, newpath);
    } catch (error) {
      throw mapError(error);
    }
  }
}