summaryrefslogtreecommitdiff
path: root/js/make_temp_dir.ts
blob: b252be3810d8df6e0c0f53f7597be0e854dd2aad (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// 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";
import { assert } from "./util";

export interface MakeTempDirOptions {
  dir?: string;
  prefix?: string;
  suffix?: string;
}

/**
 * makeTempDirSync is the synchronous version of `makeTempDir`.
 *
 *     import { makeTempDirSync } from "deno";
 *     const tempDirName0 = makeTempDirSync();
 *     const tempDirName1 = makeTempDirSync({ prefix: 'my_temp' });
 */
export function makeTempDirSync(options: MakeTempDirOptions = {}): string {
  return res(dispatch.sendSync(...req(options)));
}

/**
 * makeTempDir creates a new temporary directory in the directory `dir`, its
 * name beginning with `prefix` and ending with `suffix`.
 * It returns the full path to the newly created directory.
 * If `dir` is unspecified, tempDir uses the default directory for temporary
 * files. Multiple programs calling tempDir simultaneously will not choose the
 * same directory. It is the caller's responsibility to remove the directory
 * when no longer needed.
 *
 *     import { makeTempDir } from "deno";
 *     const tempDirName0 = await makeTempDir();
 *     const tempDirName1 = await makeTempDir({ prefix: 'my_temp' });
 */
export async function makeTempDir(
  options: MakeTempDirOptions = {}
): Promise<string> {
  return res(await dispatch.sendAsync(...req(options)));
}

function req({
  dir,
  prefix,
  suffix
}: MakeTempDirOptions): [flatbuffers.Builder, fbs.Any, flatbuffers.Offset] {
  const builder = new flatbuffers.Builder();
  const fbDir = dir == null ? -1 : builder.createString(dir);
  const fbPrefix = prefix == null ? -1 : builder.createString(prefix);
  const fbSuffix = suffix == null ? -1 : builder.createString(suffix);
  fbs.MakeTempDir.startMakeTempDir(builder);
  if (dir != null) {
    fbs.MakeTempDir.addDir(builder, fbDir);
  }
  if (prefix != null) {
    fbs.MakeTempDir.addPrefix(builder, fbPrefix);
  }
  if (suffix != null) {
    fbs.MakeTempDir.addSuffix(builder, fbSuffix);
  }
  const msg = fbs.MakeTempDir.endMakeTempDir(builder);
  return [builder, fbs.Any.MakeTempDir, msg];
}

function res(baseRes: null | fbs.Base): string {
  assert(baseRes != null);
  assert(fbs.Any.MakeTempDirRes === baseRes!.msgType());
  const res = new fbs.MakeTempDirRes();
  assert(baseRes!.msg(res) != null);
  const path = res.path();
  assert(path != null);
  return path!;
}