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
|
|
|
/**
|
|
|
|
* Error thrown in {@linkcode move} or {@linkcode moveSync} when the
|
|
|
|
* destination is a subdirectory of the source.
|
|
|
|
*/
|
2023-04-05 11:19:24 +00:00
|
|
|
export class SubdirectoryMoveError extends Error {
|
2023-12-05 08:52:56 +00:00
|
|
|
/** Constructs a new instance. */
|
2023-04-05 11:19:24 +00:00
|
|
|
constructor(src: string | URL, dest: string | URL) {
|
|
|
|
super(
|
|
|
|
`Cannot move '${src}' to a subdirectory of itself, '${dest}'.`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Moves a file or directory.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```ts
|
|
|
|
* import { move } from "https://deno.land/std@$STD_VERSION/fs/mod.ts";
|
|
|
|
*
|
|
|
|
* move("./foo", "./bar"); // returns a promise
|
|
|
|
* ```
|
|
|
|
*/
|
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,
|
2020-07-14 19:24:17 +00:00
|
|
|
{ overwrite = false }: MoveOptions = {},
|
2023-12-05 08:52:56 +00:00
|
|
|
): Promise<void> {
|
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))
|
|
|
|
) {
|
|
|
|
throw new SubdirectoryMoveError(src, 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
|
|
|
/**
|
|
|
|
* Moves a file or directory synchronously.
|
2023-12-05 08:52:56 +00:00
|
|
|
*
|
2022-11-25 11:40:23 +00:00
|
|
|
* @example
|
|
|
|
* ```ts
|
|
|
|
* import { moveSync } from "https://deno.land/std@$STD_VERSION/fs/mod.ts";
|
|
|
|
*
|
|
|
|
* moveSync("./foo", "./bar"); // void
|
|
|
|
* ```
|
|
|
|
*/
|
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,
|
2020-07-14 19:24:17 +00:00
|
|
|
{ overwrite = false }: MoveOptions = {},
|
2023-12-05 08:52:56 +00:00
|
|
|
): void {
|
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))
|
|
|
|
) {
|
|
|
|
throw new SubdirectoryMoveError(src, 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
|
|
|
}
|