diff options
| author | Yoshiya Hinosawa <stibium121@gmail.com> | 2018-09-13 11:04:45 +0900 |
|---|---|---|
| committer | Bert Belder <bertbelder@gmail.com> | 2018-09-12 22:33:11 -0700 |
| commit | 0ca7301a2d6405101569e073153536aa99a084c9 (patch) | |
| tree | 96cbbf3b5e34a4f28d970dd7d9eaf07c82267b09 /js/make_temp_dir.ts | |
| parent | 28812b833761c101d7d39a774f5b2619688765cf (diff) | |
feat: add deno.makeTempDir
Diffstat (limited to 'js/make_temp_dir.ts')
| -rw-r--r-- | js/make_temp_dir.ts | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/js/make_temp_dir.ts b/js/make_temp_dir.ts new file mode 100644 index 000000000..b252be381 --- /dev/null +++ b/js/make_temp_dir.ts @@ -0,0 +1,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!; +} |
