summaryrefslogtreecommitdiff
path: root/js/remove.ts
blob: d9f69399daf80c5b4311fd9b8f52069741b56c60 (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
43
44
45
46
47
48
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import * as msg from "gen/msg_generated";
import * as flatbuffers from "./flatbuffers";
import * as dispatch from "./dispatch";

export interface RemoveOption {
  recursive?: boolean;
}

/** Removes the named file or directory synchronously. Would throw
 * error if permission denied, not found, or directory not empty if `recursive`
 * set to false.
 * `recursive` is set to false by default.
 *
 *       import { removeSync } from "deno";
 *       removeSync("/path/to/dir/or/file", {recursive: false});
 */
export function removeSync(path: string, options: RemoveOption = {}): void {
  dispatch.sendSync(...req(path, options));
}

/** Removes the named file or directory. Would throw error if
 * permission denied, not found, or directory not empty if `recursive` set
 * to false.
 * `recursive` is set to false by default.
 *
 *       import { remove } from "deno";
 *       await remove("/path/to/dir/or/file", {recursive: false});
 */
export async function remove(
  path: string,
  options: RemoveOption = {}
): Promise<void> {
  await dispatch.sendAsync(...req(path, options));
}

function req(
  path: string,
  options: RemoveOption
): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] {
  const builder = flatbuffers.createBuilder();
  const path_ = builder.createString(path);
  msg.Remove.startRemove(builder);
  msg.Remove.addPath(builder, path_);
  msg.Remove.addRecursive(builder, !!options.recursive);
  const inner = msg.Remove.endRemove(builder);
  return [builder, msg.Any.Remove, inner];
}