summaryrefslogtreecommitdiff
path: root/js/rename.ts
diff options
context:
space:
mode:
authorMani Maghsoudlou <manidlou@gmail.com>2018-09-12 08:44:58 -0700
committerRyan Dahl <ry@tinyclouds.org>2018-09-12 11:44:58 -0400
commit88d42f0b189a1daa3fb44eb24ecad77823557d9f (patch)
treeac4d166271d9708472b16150f5f6dcc712095fcb /js/rename.ts
parent26081a32dfaf34fdc8b6cf53222c15f3d4e4f30d (diff)
Implement deno.rename() (#731)
Diffstat (limited to 'js/rename.ts')
-rw-r--r--js/rename.ts42
1 files changed, 42 insertions, 0 deletions
diff --git a/js/rename.ts b/js/rename.ts
new file mode 100644
index 000000000..241e27490
--- /dev/null
+++ b/js/rename.ts
@@ -0,0 +1,42 @@
+// Copyright 2018 the Deno authors. All rights reserved. MIT license.
+import * as fbs from "gen/msg_generated";
+import { flatbuffers } from "flatbuffers";
+import * as dispatch from "./dispatch";
+
+/**
+ * Synchronously renames (moves) oldpath to newpath. 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.
+ *
+ * import { renameSync } from "deno";
+ * renameSync("old/path", "new/path");
+ */
+export function renameSync(oldpath: string, newpath: string): void {
+ dispatch.sendSync(...req(oldpath, newpath));
+}
+
+/**
+ * Renames (moves) oldpath to newpath. 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.
+ *
+ * import { rename } from "deno";
+ * await rename("old/path", "new/path");
+ */
+export async function rename(oldpath: string, newpath: string): Promise<void> {
+ await dispatch.sendAsync(...req(oldpath, newpath));
+}
+
+function req(
+ oldpath: string,
+ newpath: string
+): [flatbuffers.Builder, fbs.Any, flatbuffers.Offset] {
+ const builder = new flatbuffers.Builder();
+ const oldpath_ = builder.createString(oldpath);
+ const newpath_ = builder.createString(newpath);
+ fbs.Rename.startRename(builder);
+ fbs.Rename.addOldpath(builder, oldpath_);
+ fbs.Rename.addNewpath(builder, newpath_);
+ const msg = fbs.Rename.endRename(builder);
+ return [builder, fbs.Any.Rename, msg];
+}