2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-12-21 07:39:51 +00:00
|
|
|
import { isSubdir } from "./_is_subdir.ts";
|
|
|
|
import { isSamePath } from "./_is_same_path.ts";
|
2019-03-12 09:11:30 +00:00
|
|
|
|
2022-08-31 07:04:53 +00:00
|
|
|
const EXISTS_ERROR = new Deno.errors.AlreadyExists("dest already exists.");
|
2022-08-24 04:28:54 +00:00
|
|
|
|
2023-12-05 08:52:56 +00:00
|
|
|
/** Options for {@linkcode move} and {@linkcode moveSync}. */
|
|
|
|
export interface MoveOptions {
|
|
|
|
/**
|
|
|
|
* Whether the destination file should be overwritten if it already exists.
|
|
|
|
*
|
|
|
|
* @default {false}
|
|
|
|
*/
|
2019-03-12 09:11:30 +00:00
|
|
|
overwrite?: boolean;
|
|
|
|
}
|
|
|
|
|
2022-11-25 11:40:23 +00:00
|
|
|
/**
|
2024-06-07 03:54:50 +00:00
|
|
|
* Asynchronously moves a file or directory (along with its contents).
|
|
|
|
*
|
|
|
|
* Requires `--allow-read` and `--allow-write` permissions.
|
|
|
|
*
|
|
|
|
* @see {@link https://docs.deno.com/runtime/manual/basics/permissions#file-system-access}
|
|
|
|
* for more information on Deno's permissions system.
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
2024-03-27 06:28:06 +00:00
|
|
|
* @param src The source file or directory as a string or URL.
|
|
|
|
* @param dest The destination file or directory as a string or URL.
|
|
|
|
* @param options Options for the move operation.
|
2024-07-25 05:53:22 +00:00
|
|
|
* @throws {Deno.errors.AlreadyExists} If `dest` already exists and
|
|
|
|
* `options.overwrite` is `false`.
|
|
|
|
* @throws {Deno.errors.NotSupported} If `src` is a sub-directory of `dest`.
|
2024-06-07 03:54:50 +00:00
|
|
|
*
|
2024-03-27 06:28:06 +00:00
|
|
|
* @returns A void promise that resolves once the operation completes.
|
|
|
|
*
|
|
|
|
* @example Basic usage
|
2024-09-19 23:29:31 +00:00
|
|
|
* ```ts ignore
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { move } from "@std/fs/move";
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
2024-03-27 06:28:06 +00:00
|
|
|
* await move("./foo", "./bar");
|
2022-11-25 11:40:23 +00:00
|
|
|
* ```
|
2024-03-27 06:28:06 +00:00
|
|
|
*
|
|
|
|
* This will move the file or directory at `./foo` to `./bar` without
|
|
|
|
* overwriting.
|
|
|
|
*
|
|
|
|
* @example Overwriting
|
2024-09-19 23:29:31 +00:00
|
|
|
* ```ts ignore
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { move } from "@std/fs/move";
|
2024-03-27 06:28:06 +00:00
|
|
|
*
|
|
|
|
* await move("./foo", "./bar", { overwrite: true });
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* This will move the file or directory at `./foo` to `./bar`, overwriting
|
|
|
|
* `./bar` if it already exists.
|
2022-11-25 11:40:23 +00:00
|
|
|
*/
|
2019-03-12 09:11:30 +00:00
|
|
|
export async function move(
|
2022-08-30 03:58:48 +00:00
|
|
|
src: string | URL,
|
|
|
|
dest: string | URL,
|
2024-07-19 04:10:29 +00:00
|
|
|
options?: MoveOptions,
|
2023-12-05 08:52:56 +00:00
|
|
|
): Promise<void> {
|
2024-07-19 04:10:29 +00:00
|
|
|
const { overwrite = false } = options ?? {};
|
|
|
|
|
2019-03-12 09:11:30 +00:00
|
|
|
const srcStat = await Deno.stat(src);
|
|
|
|
|
2023-04-05 11:19:24 +00:00
|
|
|
if (
|
|
|
|
srcStat.isDirectory &&
|
|
|
|
(isSubdir(src, dest) || isSamePath(src, dest))
|
|
|
|
) {
|
2024-07-25 05:53:22 +00:00
|
|
|
throw new Deno.errors.NotSupported(
|
|
|
|
`Cannot move '${src}' to a subdirectory of itself, '${dest}'.`,
|
|
|
|
);
|
2019-03-12 09:11:30 +00:00
|
|
|
}
|
|
|
|
|
2020-03-10 16:08:58 +00:00
|
|
|
if (overwrite) {
|
2023-04-05 11:19:24 +00:00
|
|
|
if (isSamePath(src, dest)) return;
|
2022-08-24 04:28:54 +00:00
|
|
|
try {
|
2020-04-26 20:27:24 +00:00
|
|
|
await Deno.remove(dest, { recursive: true });
|
2022-08-24 04:28:54 +00:00
|
|
|
} catch (error) {
|
|
|
|
if (!(error instanceof Deno.errors.NotFound)) {
|
|
|
|
throw error;
|
|
|
|
}
|
2020-04-26 20:27:24 +00:00
|
|
|
}
|
2019-03-12 09:11:30 +00:00
|
|
|
} else {
|
2022-08-24 04:28:54 +00:00
|
|
|
try {
|
|
|
|
await Deno.lstat(dest);
|
|
|
|
return Promise.reject(EXISTS_ERROR);
|
|
|
|
} catch {
|
|
|
|
// Do nothing...
|
2019-03-12 09:11:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 17:21:33 +00:00
|
|
|
await Deno.rename(src, dest);
|
2019-03-12 09:11:30 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 11:40:23 +00:00
|
|
|
/**
|
2024-06-07 03:54:50 +00:00
|
|
|
* Synchronously moves a file or directory (along with its contents).
|
|
|
|
*
|
|
|
|
* Requires `--allow-read` and `--allow-write` permissions.
|
|
|
|
*
|
|
|
|
* @see {@link https://docs.deno.com/runtime/manual/basics/permissions#file-system-access}
|
|
|
|
* for more information on Deno's permissions system.
|
2023-12-05 08:52:56 +00:00
|
|
|
*
|
2024-03-27 06:28:06 +00:00
|
|
|
* @param src The source file or directory as a string or URL.
|
|
|
|
* @param dest The destination file or directory as a string or URL.
|
|
|
|
* @param options Options for the move operation.
|
2024-07-25 05:53:22 +00:00
|
|
|
* @throws {Deno.errors.AlreadyExists} If `dest` already exists and
|
|
|
|
* `options.overwrite` is `false`.
|
|
|
|
* @throws {Deno.errors.NotSupported} If `src` is a sub-directory of `dest`.
|
2024-06-07 03:54:50 +00:00
|
|
|
*
|
2024-03-27 06:28:06 +00:00
|
|
|
* @returns A void value that returns once the operation completes.
|
|
|
|
*
|
|
|
|
* @example Basic usage
|
2024-09-19 23:29:31 +00:00
|
|
|
* ```ts ignore
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { moveSync } from "@std/fs/move";
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
2024-03-27 06:28:06 +00:00
|
|
|
* moveSync("./foo", "./bar");
|
2022-11-25 11:40:23 +00:00
|
|
|
* ```
|
2024-03-27 06:28:06 +00:00
|
|
|
*
|
|
|
|
* This will move the file or directory at `./foo` to `./bar` without
|
|
|
|
* overwriting.
|
|
|
|
*
|
|
|
|
* @example Overwriting
|
2024-09-19 23:29:31 +00:00
|
|
|
* ```ts ignore
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { moveSync } from "@std/fs/move";
|
2024-03-27 06:28:06 +00:00
|
|
|
*
|
|
|
|
* moveSync("./foo", "./bar", { overwrite: true });
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* This will move the file or directory at `./foo` to `./bar`, overwriting
|
|
|
|
* `./bar` if it already exists.
|
2022-11-25 11:40:23 +00:00
|
|
|
*/
|
2019-03-12 09:11:30 +00:00
|
|
|
export function moveSync(
|
2022-08-30 03:58:48 +00:00
|
|
|
src: string | URL,
|
|
|
|
dest: string | URL,
|
2024-07-19 04:10:29 +00:00
|
|
|
options?: MoveOptions,
|
2023-12-05 08:52:56 +00:00
|
|
|
): void {
|
2024-07-19 04:10:29 +00:00
|
|
|
const { overwrite = false } = options ?? {};
|
|
|
|
|
2019-03-12 09:11:30 +00:00
|
|
|
const srcStat = Deno.statSync(src);
|
|
|
|
|
2023-04-05 11:19:24 +00:00
|
|
|
if (
|
|
|
|
srcStat.isDirectory &&
|
|
|
|
(isSubdir(src, dest) || isSamePath(src, dest))
|
|
|
|
) {
|
2024-07-25 05:53:22 +00:00
|
|
|
throw new Deno.errors.NotSupported(
|
|
|
|
`Cannot move '${src}' to a subdirectory of itself, '${dest}'.`,
|
|
|
|
);
|
2019-03-12 09:11:30 +00:00
|
|
|
}
|
|
|
|
|
2020-03-10 16:08:58 +00:00
|
|
|
if (overwrite) {
|
2023-04-05 11:19:24 +00:00
|
|
|
if (isSamePath(src, dest)) return;
|
2022-08-24 04:28:54 +00:00
|
|
|
try {
|
2020-04-26 20:27:24 +00:00
|
|
|
Deno.removeSync(dest, { recursive: true });
|
2022-08-24 04:28:54 +00:00
|
|
|
} catch (error) {
|
|
|
|
if (!(error instanceof Deno.errors.NotFound)) {
|
|
|
|
throw error;
|
|
|
|
}
|
2020-04-26 20:27:24 +00:00
|
|
|
}
|
2019-03-12 09:11:30 +00:00
|
|
|
} else {
|
2022-08-24 04:28:54 +00:00
|
|
|
try {
|
|
|
|
Deno.lstatSync(dest);
|
|
|
|
throw EXISTS_ERROR;
|
|
|
|
} catch (error) {
|
|
|
|
if (error === EXISTS_ERROR) {
|
|
|
|
throw error;
|
|
|
|
}
|
2019-03-12 09:11:30 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-09 17:21:33 +00:00
|
|
|
|
|
|
|
Deno.renameSync(src, dest);
|
2019-03-12 09:11:30 +00:00
|
|
|
}
|