2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 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-11 12:00:57 -04:00
|
|
|
import * as dispatch from "./dispatch";
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Write a new file, with given filename and data synchronously.
|
2018-09-11 12:00:57 -04:00
|
|
|
*
|
2018-10-14 16:29:50 -04:00
|
|
|
* import { writeFileSync } from "deno";
|
2018-09-11 12:00:57 -04:00
|
|
|
*
|
2019-01-30 10:44:40 -05:00
|
|
|
* const encoder = new TextEncoder();
|
2018-10-14 16:29:50 -04:00
|
|
|
* const data = encoder.encode("Hello world\n");
|
|
|
|
* writeFileSync("hello.txt", data);
|
2018-09-11 12:00:57 -04:00
|
|
|
*/
|
|
|
|
export function writeFileSync(
|
|
|
|
filename: string,
|
|
|
|
data: Uint8Array,
|
|
|
|
perm = 0o666
|
|
|
|
): void {
|
|
|
|
dispatch.sendSync(...req(filename, data, perm));
|
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Write a new file, with given filename and data.
|
2018-09-11 12:00:57 -04:00
|
|
|
*
|
2018-10-14 16:29:50 -04:00
|
|
|
* import { writeFile } from "deno";
|
2018-09-11 12:00:57 -04:00
|
|
|
*
|
2019-01-30 10:44:40 -05:00
|
|
|
* const encoder = new TextEncoder();
|
2018-10-14 16:29:50 -04:00
|
|
|
* const data = encoder.encode("Hello world\n");
|
|
|
|
* await writeFile("hello.txt", data);
|
2018-09-11 12:00:57 -04:00
|
|
|
*/
|
|
|
|
export async function writeFile(
|
|
|
|
filename: string,
|
|
|
|
data: Uint8Array,
|
|
|
|
perm = 0o666
|
|
|
|
): Promise<void> {
|
|
|
|
await dispatch.sendAsync(...req(filename, data, perm));
|
|
|
|
}
|
|
|
|
|
|
|
|
function req(
|
|
|
|
filename: string,
|
|
|
|
data: Uint8Array,
|
|
|
|
perm: number
|
2018-10-03 21:18:23 -04:00
|
|
|
): [flatbuffers.Builder, msg.Any, flatbuffers.Offset, Uint8Array] {
|
2018-10-17 13:04:28 -04:00
|
|
|
const builder = flatbuffers.createBuilder();
|
2018-09-11 12:00:57 -04:00
|
|
|
const filename_ = builder.createString(filename);
|
2018-10-03 21:18:23 -04:00
|
|
|
msg.WriteFile.startWriteFile(builder);
|
|
|
|
msg.WriteFile.addFilename(builder, filename_);
|
|
|
|
msg.WriteFile.addPerm(builder, perm);
|
|
|
|
const inner = msg.WriteFile.endWriteFile(builder);
|
|
|
|
return [builder, msg.Any.WriteFile, inner, data];
|
2018-09-11 12:00:57 -04:00
|
|
|
}
|