diff options
author | Mani Maghsoudlou <manidlou@gmail.com> | 2018-09-12 08:44:58 -0700 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2018-09-12 11:44:58 -0400 |
commit | 88d42f0b189a1daa3fb44eb24ecad77823557d9f (patch) | |
tree | ac4d166271d9708472b16150f5f6dcc712095fcb /js/rename.ts | |
parent | 26081a32dfaf34fdc8b6cf53222c15f3d4e4f30d (diff) |
Implement deno.rename() (#731)
Diffstat (limited to 'js/rename.ts')
-rw-r--r-- | js/rename.ts | 42 |
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]; +} |