2018-09-12 22:04:45 -04:00
|
|
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
2018-10-03 21:18:23 -04:00
|
|
|
import * as msg from "gen/msg_generated";
|
2018-10-17 13:04:28 -04:00
|
|
|
import * as flatbuffers from "./flatbuffers";
|
2018-09-12 22:04:45 -04:00
|
|
|
import * as dispatch from "./dispatch";
|
|
|
|
import { assert } from "./util";
|
|
|
|
|
|
|
|
export interface MakeTempDirOptions {
|
|
|
|
dir?: string;
|
|
|
|
prefix?: string;
|
|
|
|
suffix?: string;
|
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** makeTempDirSync is the synchronous version of `makeTempDir`.
|
2018-09-12 22:04:45 -04:00
|
|
|
*
|
2018-10-14 16:29:50 -04:00
|
|
|
* import { makeTempDirSync } from "deno";
|
|
|
|
* const tempDirName0 = makeTempDirSync();
|
|
|
|
* const tempDirName1 = makeTempDirSync({ prefix: 'my_temp' });
|
2018-09-12 22:04:45 -04:00
|
|
|
*/
|
|
|
|
export function makeTempDirSync(options: MakeTempDirOptions = {}): string {
|
|
|
|
return res(dispatch.sendSync(...req(options)));
|
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** makeTempDir creates a new temporary directory in the directory `dir`, its
|
2018-09-12 22:04:45 -04:00
|
|
|
* 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.
|
|
|
|
*
|
2018-10-14 16:29:50 -04:00
|
|
|
* import { makeTempDir } from "deno";
|
|
|
|
* const tempDirName0 = await makeTempDir();
|
|
|
|
* const tempDirName1 = await makeTempDir({ prefix: 'my_temp' });
|
2018-09-12 22:04:45 -04:00
|
|
|
*/
|
|
|
|
export async function makeTempDir(
|
|
|
|
options: MakeTempDirOptions = {}
|
|
|
|
): Promise<string> {
|
|
|
|
return res(await dispatch.sendAsync(...req(options)));
|
|
|
|
}
|
|
|
|
|
|
|
|
function req({
|
|
|
|
dir,
|
|
|
|
prefix,
|
|
|
|
suffix
|
2018-10-03 21:18:23 -04:00
|
|
|
}: MakeTempDirOptions): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] {
|
2018-10-17 13:04:28 -04:00
|
|
|
const builder = flatbuffers.createBuilder();
|
2018-09-12 22:04:45 -04:00
|
|
|
const fbDir = dir == null ? -1 : builder.createString(dir);
|
|
|
|
const fbPrefix = prefix == null ? -1 : builder.createString(prefix);
|
|
|
|
const fbSuffix = suffix == null ? -1 : builder.createString(suffix);
|
2018-10-03 21:18:23 -04:00
|
|
|
msg.MakeTempDir.startMakeTempDir(builder);
|
2018-09-12 22:04:45 -04:00
|
|
|
if (dir != null) {
|
2018-10-03 21:18:23 -04:00
|
|
|
msg.MakeTempDir.addDir(builder, fbDir);
|
2018-09-12 22:04:45 -04:00
|
|
|
}
|
|
|
|
if (prefix != null) {
|
2018-10-03 21:18:23 -04:00
|
|
|
msg.MakeTempDir.addPrefix(builder, fbPrefix);
|
2018-09-12 22:04:45 -04:00
|
|
|
}
|
|
|
|
if (suffix != null) {
|
2018-10-03 21:18:23 -04:00
|
|
|
msg.MakeTempDir.addSuffix(builder, fbSuffix);
|
2018-09-12 22:04:45 -04:00
|
|
|
}
|
2018-10-03 21:18:23 -04:00
|
|
|
const inner = msg.MakeTempDir.endMakeTempDir(builder);
|
|
|
|
return [builder, msg.Any.MakeTempDir, inner];
|
2018-09-12 22:04:45 -04:00
|
|
|
}
|
|
|
|
|
2018-10-03 21:18:23 -04:00
|
|
|
function res(baseRes: null | msg.Base): string {
|
2018-09-12 22:04:45 -04:00
|
|
|
assert(baseRes != null);
|
2018-10-03 21:18:23 -04:00
|
|
|
assert(msg.Any.MakeTempDirRes === baseRes!.innerType());
|
|
|
|
const res = new msg.MakeTempDirRes();
|
2018-10-03 21:12:23 -04:00
|
|
|
assert(baseRes!.inner(res) != null);
|
2018-09-12 22:04:45 -04:00
|
|
|
const path = res.path();
|
|
|
|
assert(path != null);
|
|
|
|
return path!;
|
|
|
|
}
|