summaryrefslogtreecommitdiff
path: root/js/rename.ts
blob: d92538f451bfdf893b9902303aa66bf1d9b7a1fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import * as msg 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, msg.Any, flatbuffers.Offset] {
  const builder = new flatbuffers.Builder();
  const oldpath_ = builder.createString(oldpath);
  const newpath_ = builder.createString(newpath);
  msg.Rename.startRename(builder);
  msg.Rename.addOldpath(builder, oldpath_);
  msg.Rename.addNewpath(builder, newpath_);
  const inner = msg.Rename.endRename(builder);
  return [builder, msg.Any.Rename, inner];
}