2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-10-16 19:39:33 +01:00
|
|
|
import * as path from "../path/mod.ts";
|
|
|
|
|
2018-11-07 23:19:08 -05:00
|
|
|
export function charCode(s: string): number {
|
|
|
|
return s.charCodeAt(0);
|
|
|
|
}
|
2018-12-17 22:40:42 -05:00
|
|
|
|
2019-06-19 12:22:01 +08:00
|
|
|
/** Create or open a temporal file at specified directory with prefix and
|
|
|
|
* postfix
|
|
|
|
* */
|
2019-02-11 08:49:48 +09:00
|
|
|
export async function tempFile(
|
|
|
|
dir: string,
|
|
|
|
opts: {
|
|
|
|
prefix?: string;
|
|
|
|
postfix?: string;
|
|
|
|
} = { prefix: "", postfix: "" }
|
2020-06-12 20:23:38 +01:00
|
|
|
): Promise<{ file: Deno.File; filepath: string }> {
|
2019-02-11 08:49:48 +09:00
|
|
|
const r = Math.floor(Math.random() * 1000000);
|
|
|
|
const filepath = path.resolve(
|
|
|
|
`${dir}/${opts.prefix || ""}${r}${opts.postfix || ""}`
|
|
|
|
);
|
2020-06-12 20:23:38 +01:00
|
|
|
await Deno.mkdir(path.dirname(filepath), { recursive: true });
|
|
|
|
const file = await Deno.open(filepath, {
|
2020-04-25 00:45:55 +02:00
|
|
|
create: true,
|
|
|
|
read: true,
|
|
|
|
write: true,
|
|
|
|
append: true,
|
|
|
|
});
|
2019-02-11 08:49:48 +09:00
|
|
|
return { file, filepath };
|
2018-12-17 22:40:42 -05:00
|
|
|
}
|