2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-09-02 17:07:11 -04:00
|
|
|
import { stat, statSync } from "./stat.ts";
|
|
|
|
import { open, openSync } from "./files.ts";
|
|
|
|
import { chmod, chmodSync } from "./chmod.ts";
|
|
|
|
import { writeAll, writeAllSync } from "./buffer.ts";
|
2019-03-09 12:30:38 -05:00
|
|
|
|
2020-03-02 10:19:42 -05:00
|
|
|
/** Options for writing to a file. */
|
2019-02-02 14:26:18 -05:00
|
|
|
export interface WriteFileOptions {
|
2020-03-02 10:19:42 -05:00
|
|
|
/** Defaults to `false`. If set to `true`, will append to a file instead of
|
|
|
|
* overwriting previous contents. */
|
2019-02-02 14:26:18 -05:00
|
|
|
append?: boolean;
|
2020-03-02 10:19:42 -05:00
|
|
|
/** Sets the option to allow creating a new file, if one doesn't already
|
|
|
|
* exist at the specified path (defaults to `true`). */
|
|
|
|
create?: boolean;
|
|
|
|
/** Permissions always applied to file. */
|
2020-03-07 22:29:12 -05:00
|
|
|
mode?: number;
|
2019-02-02 14:26:18 -05:00
|
|
|
}
|
|
|
|
|
2020-03-02 10:19:42 -05:00
|
|
|
/** Synchronously write data to the given path, by default creating a new
|
|
|
|
* file if needed, else overwriting.
|
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");
|
2019-02-12 10:08:56 -05:00
|
|
|
* Deno.writeFileSync("hello.txt", data);
|
2020-03-02 10:19:42 -05:00
|
|
|
*
|
|
|
|
* Requires `allow-write` permission, and `allow-read` if create is `false`.
|
2018-09-11 12:00:57 -04:00
|
|
|
*/
|
|
|
|
export function writeFileSync(
|
2020-03-06 11:29:23 -05:00
|
|
|
path: string,
|
2018-09-11 12:00:57 -04:00
|
|
|
data: Uint8Array,
|
2019-02-02 14:26:18 -05:00
|
|
|
options: WriteFileOptions = {}
|
2018-09-11 12:00:57 -04:00
|
|
|
): void {
|
2019-03-27 23:29:36 -04:00
|
|
|
if (options.create !== undefined) {
|
|
|
|
const create = !!options.create;
|
|
|
|
if (!create) {
|
|
|
|
// verify that file exists
|
2020-03-06 11:29:23 -05:00
|
|
|
statSync(path);
|
2019-03-27 23:29:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const openMode = !!options.append ? "a" : "w";
|
2020-03-06 11:29:23 -05:00
|
|
|
const file = openSync(path, openMode);
|
2019-03-27 23:29:36 -04:00
|
|
|
|
2020-03-07 22:29:12 -05:00
|
|
|
if (options.mode !== undefined && options.mode !== null) {
|
|
|
|
chmodSync(path, options.mode);
|
2019-03-27 23:29:36 -04:00
|
|
|
}
|
|
|
|
|
2019-07-23 11:16:39 -04:00
|
|
|
writeAllSync(file, data);
|
2019-03-27 23:29:36 -04:00
|
|
|
file.close();
|
2018-09-11 12:00:57 -04:00
|
|
|
}
|
|
|
|
|
2020-03-02 10:19:42 -05:00
|
|
|
/** Write data to the given path, by default creating a new file if needed,
|
|
|
|
* else overwriting.
|
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");
|
2019-02-12 10:08:56 -05:00
|
|
|
* await Deno.writeFile("hello.txt", data);
|
2020-03-02 10:19:42 -05:00
|
|
|
*
|
|
|
|
* Requires `allow-write` permission, and `allow-read` if create is `false`.
|
2018-09-11 12:00:57 -04:00
|
|
|
*/
|
|
|
|
export async function writeFile(
|
2020-03-06 11:29:23 -05:00
|
|
|
path: string,
|
2018-09-11 12:00:57 -04:00
|
|
|
data: Uint8Array,
|
2019-02-02 14:26:18 -05:00
|
|
|
options: WriteFileOptions = {}
|
2018-09-11 12:00:57 -04:00
|
|
|
): Promise<void> {
|
2019-03-27 23:29:36 -04:00
|
|
|
if (options.create !== undefined) {
|
|
|
|
const create = !!options.create;
|
|
|
|
if (!create) {
|
|
|
|
// verify that file exists
|
2020-03-06 11:29:23 -05:00
|
|
|
await stat(path);
|
2019-03-27 23:29:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const openMode = !!options.append ? "a" : "w";
|
2020-03-06 11:29:23 -05:00
|
|
|
const file = await open(path, openMode);
|
2019-03-27 23:29:36 -04:00
|
|
|
|
2020-03-07 22:29:12 -05:00
|
|
|
if (options.mode !== undefined && options.mode !== null) {
|
|
|
|
await chmod(path, options.mode);
|
2019-03-27 23:29:36 -04:00
|
|
|
}
|
|
|
|
|
2019-07-23 11:16:39 -04:00
|
|
|
await writeAll(file, data);
|
2019-03-27 23:29:36 -04:00
|
|
|
file.close();
|
2018-09-11 12:00:57 -04:00
|
|
|
}
|