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";
|
|
|
|
|
2019-02-02 14:26:18 -05:00
|
|
|
/** Options for writing to a file.
|
|
|
|
* `perm` would change the file's permission if set.
|
|
|
|
* `create` decides if the file should be created if not exists (default: true)
|
|
|
|
* `append` decides if the file should be appended (default: false)
|
|
|
|
*/
|
|
|
|
export interface WriteFileOptions {
|
|
|
|
perm?: number;
|
|
|
|
create?: boolean;
|
|
|
|
append?: boolean;
|
|
|
|
}
|
|
|
|
|
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,
|
2019-02-02 14:26:18 -05:00
|
|
|
options: WriteFileOptions = {}
|
2018-09-11 12:00:57 -04:00
|
|
|
): void {
|
2019-02-02 14:26:18 -05:00
|
|
|
dispatch.sendSync(...req(filename, data, options));
|
2018-09-11 12:00:57 -04:00
|
|
|
}
|
|
|
|
|
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,
|
2019-02-02 14:26:18 -05:00
|
|
|
options: WriteFileOptions = {}
|
2018-09-11 12:00:57 -04:00
|
|
|
): Promise<void> {
|
2019-02-02 14:26:18 -05:00
|
|
|
await dispatch.sendAsync(...req(filename, data, options));
|
2018-09-11 12:00:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function req(
|
|
|
|
filename: string,
|
|
|
|
data: Uint8Array,
|
2019-02-02 14:26:18 -05:00
|
|
|
options: WriteFileOptions
|
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_);
|
2019-02-02 14:26:18 -05:00
|
|
|
// Perm is not updated by default
|
|
|
|
if (options.perm !== undefined && options.perm !== null) {
|
|
|
|
msg.WriteFile.addUpdatePerm(builder, true);
|
|
|
|
msg.WriteFile.addPerm(builder, options.perm!);
|
|
|
|
} else {
|
|
|
|
msg.WriteFile.addUpdatePerm(builder, false);
|
|
|
|
msg.WriteFile.addPerm(builder, 0o666);
|
|
|
|
}
|
|
|
|
// Create is turned on by default
|
|
|
|
if (options.create !== undefined) {
|
|
|
|
msg.WriteFile.addIsCreate(builder, !!options.create);
|
|
|
|
} else {
|
|
|
|
msg.WriteFile.addIsCreate(builder, true);
|
|
|
|
}
|
|
|
|
msg.WriteFile.addIsAppend(builder, !!options.append);
|
2018-10-03 21:18:23 -04:00
|
|
|
const inner = msg.WriteFile.endWriteFile(builder);
|
|
|
|
return [builder, msg.Any.WriteFile, inner, data];
|
2018-09-11 12:00:57 -04:00
|
|
|
}
|