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
|
|
|
|
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
|
|
|
*
|
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);
|
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-03-27 23:29:36 -04:00
|
|
|
if (options.create !== undefined) {
|
|
|
|
const create = !!options.create;
|
|
|
|
if (!create) {
|
|
|
|
// verify that file exists
|
|
|
|
statSync(filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const openMode = !!options.append ? "a" : "w";
|
|
|
|
const file = openSync(filename, openMode);
|
|
|
|
|
|
|
|
if (options.perm !== undefined && options.perm !== null) {
|
|
|
|
chmodSync(filename, options.perm);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
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);
|
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-03-27 23:29:36 -04:00
|
|
|
if (options.create !== undefined) {
|
|
|
|
const create = !!options.create;
|
|
|
|
if (!create) {
|
|
|
|
// verify that file exists
|
|
|
|
await stat(filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const openMode = !!options.append ? "a" : "w";
|
|
|
|
const file = await open(filename, openMode);
|
|
|
|
|
|
|
|
if (options.perm !== undefined && options.perm !== null) {
|
|
|
|
await chmod(filename, options.perm);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|