summaryrefslogtreecommitdiff
path: root/std/fs/move.ts
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2019-10-09 17:10:09 -0400
committerRyan Dahl <ry@tinyclouds.org>2019-10-09 17:10:09 -0400
commit151ce0266eb4de2c8fc600c81c192a5f791b6169 (patch)
tree7cb04016a1c7ee88adde83814548d7a9409dcde3 /std/fs/move.ts
parenta355f7c807686918734416d91b79c26c21effba9 (diff)
Move everything into std subdir
Diffstat (limited to 'std/fs/move.ts')
-rw-r--r--std/fs/move.ts59
1 files changed, 59 insertions, 0 deletions
diff --git a/std/fs/move.ts b/std/fs/move.ts
new file mode 100644
index 000000000..190f88609
--- /dev/null
+++ b/std/fs/move.ts
@@ -0,0 +1,59 @@
+// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
+import { exists, existsSync } from "./exists.ts";
+import { isSubdir } from "./utils.ts";
+
+interface MoveOptions {
+ overwrite?: boolean;
+}
+
+/** Moves a file or directory */
+export async function move(
+ src: string,
+ dest: string,
+ options?: MoveOptions
+): Promise<void> {
+ const srcStat = await Deno.stat(src);
+
+ if (srcStat.isDirectory() && isSubdir(src, dest)) {
+ throw new Error(
+ `Cannot move '${src}' to a subdirectory of itself, '${dest}'.`
+ );
+ }
+
+ if (options && options.overwrite) {
+ await Deno.remove(dest, { recursive: true });
+ await Deno.rename(src, dest);
+ } else {
+ if (await exists(dest)) {
+ throw new Error("dest already exists.");
+ }
+ await Deno.rename(src, dest);
+ }
+
+ return;
+}
+
+/** Moves a file or directory */
+export function moveSync(
+ src: string,
+ dest: string,
+ options?: MoveOptions
+): void {
+ const srcStat = Deno.statSync(src);
+
+ if (srcStat.isDirectory() && isSubdir(src, dest)) {
+ throw new Error(
+ `Cannot move '${src}' to a subdirectory of itself, '${dest}'.`
+ );
+ }
+
+ if (options && options.overwrite) {
+ Deno.removeSync(dest, { recursive: true });
+ Deno.renameSync(src, dest);
+ } else {
+ if (existsSync(dest)) {
+ throw new Error("dest already exists.");
+ }
+ Deno.renameSync(src, dest);
+ }
+}