2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-03-09 19:22:15 -04:00
|
|
|
import { stat, statSync } from "./ops/fs/stat.ts";
|
2019-09-02 17:07:11 -04:00
|
|
|
import { open, openSync } from "./files.ts";
|
2020-03-09 19:22:15 -04:00
|
|
|
import { chmod, chmodSync } from "./ops/fs/chmod.ts";
|
2019-09-02 17:07:11 -04:00
|
|
|
import { writeAll, writeAllSync } from "./buffer.ts";
|
2020-03-20 16:03:04 -04:00
|
|
|
import { build } from "./build.ts";
|
2019-03-09 12:30:38 -05:00
|
|
|
|
2019-02-02 14:26:18 -05:00
|
|
|
export interface WriteFileOptions {
|
|
|
|
append?: boolean;
|
2020-03-02 10:19:42 -05:00
|
|
|
create?: boolean;
|
2020-03-07 22:29:12 -05:00
|
|
|
mode?: number;
|
2019-02-02 14:26:18 -05:00
|
|
|
}
|
|
|
|
|
2018-09-11 12:00:57 -04:00
|
|
|
export function writeFileSync(
|
2020-06-11 12:36:20 -04:00
|
|
|
path: string | URL,
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 18:45:55 -04:00
|
|
|
const openOptions = !!options.append
|
|
|
|
? { write: true, create: true, append: true }
|
|
|
|
: { write: true, create: true, truncate: true };
|
|
|
|
const file = openSync(path, openOptions);
|
2019-03-27 23:29:36 -04:00
|
|
|
|
2020-03-20 16:03:04 -04:00
|
|
|
if (
|
|
|
|
options.mode !== undefined &&
|
|
|
|
options.mode !== null &&
|
2020-04-28 12:35:23 -04:00
|
|
|
build.os !== "windows"
|
2020-03-20 16:03:04 -04:00
|
|
|
) {
|
2020-03-07 22:29:12 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
export async function writeFile(
|
2020-06-11 12:36:20 -04:00
|
|
|
path: string | URL,
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 18:45:55 -04:00
|
|
|
const openOptions = !!options.append
|
|
|
|
? { write: true, create: true, append: true }
|
|
|
|
: { write: true, create: true, truncate: true };
|
|
|
|
const file = await open(path, openOptions);
|
2019-03-27 23:29:36 -04:00
|
|
|
|
2020-03-20 16:03:04 -04:00
|
|
|
if (
|
|
|
|
options.mode !== undefined &&
|
|
|
|
options.mode !== null &&
|
2020-04-28 12:35:23 -04:00
|
|
|
build.os !== "windows"
|
2020-03-20 16:03:04 -04:00
|
|
|
) {
|
2020-03-07 22:29:12 -05:00
|
|
|
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
|
|
|
}
|