2020-05-04 18:59:37 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-11-26 07:50:08 -05:00
|
|
|
import { Encodings, notImplemented } from "../_utils.ts";
|
2020-05-22 22:11:10 -04:00
|
|
|
import { fromFileUrl } from "../path.ts";
|
2020-06-27 16:52:49 -04:00
|
|
|
import { Buffer } from "../buffer.ts";
|
2020-05-04 18:59:37 -04:00
|
|
|
import {
|
|
|
|
CallbackWithError,
|
2020-06-06 15:56:49 -04:00
|
|
|
checkEncoding,
|
2020-09-27 06:22:32 -04:00
|
|
|
getEncoding,
|
2020-05-04 18:59:37 -04:00
|
|
|
getOpenOptions,
|
2020-09-27 06:22:32 -04:00
|
|
|
isFileOptions,
|
|
|
|
WriteFileOptions,
|
2020-05-04 18:59:37 -04:00
|
|
|
} from "./_fs_common.ts";
|
|
|
|
|
|
|
|
export function writeFile(
|
2020-05-22 22:11:10 -04:00
|
|
|
pathOrRid: string | number | URL,
|
2020-05-04 18:59:37 -04:00
|
|
|
data: string | Uint8Array,
|
2020-06-23 22:32:43 -04:00
|
|
|
optOrCallback: Encodings | CallbackWithError | WriteFileOptions | undefined,
|
2020-07-14 15:24:17 -04:00
|
|
|
callback?: CallbackWithError,
|
2020-05-04 18:59:37 -04:00
|
|
|
): void {
|
|
|
|
const callbackFn: CallbackWithError | undefined =
|
|
|
|
optOrCallback instanceof Function ? optOrCallback : callback;
|
2020-06-23 22:32:43 -04:00
|
|
|
const options: Encodings | WriteFileOptions | undefined =
|
2020-05-04 18:59:37 -04:00
|
|
|
optOrCallback instanceof Function ? undefined : optOrCallback;
|
|
|
|
|
|
|
|
if (!callbackFn) {
|
|
|
|
throw new TypeError("Callback must be a function.");
|
|
|
|
}
|
|
|
|
|
2020-05-22 22:11:10 -04:00
|
|
|
pathOrRid = pathOrRid instanceof URL ? fromFileUrl(pathOrRid) : pathOrRid;
|
|
|
|
|
2020-05-04 18:59:37 -04:00
|
|
|
const flag: string | undefined = isFileOptions(options)
|
|
|
|
? options.flag
|
|
|
|
: undefined;
|
|
|
|
|
|
|
|
const mode: number | undefined = isFileOptions(options)
|
|
|
|
? options.mode
|
|
|
|
: undefined;
|
|
|
|
|
2020-06-06 15:56:49 -04:00
|
|
|
const encoding = checkEncoding(getEncoding(options)) || "utf8";
|
2020-05-04 18:59:37 -04:00
|
|
|
const openOptions = getOpenOptions(flag || "w");
|
|
|
|
|
2020-06-27 16:52:49 -04:00
|
|
|
if (typeof data === "string") data = Buffer.from(data, encoding);
|
2020-05-04 18:59:37 -04:00
|
|
|
|
|
|
|
const isRid = typeof pathOrRid === "number";
|
|
|
|
let file;
|
|
|
|
|
|
|
|
let error: Error | null = null;
|
|
|
|
(async (): Promise<void> => {
|
|
|
|
try {
|
|
|
|
file = isRid
|
|
|
|
? new Deno.File(pathOrRid as number)
|
|
|
|
: await Deno.open(pathOrRid as string, openOptions);
|
|
|
|
|
|
|
|
if (!isRid && mode) {
|
|
|
|
if (Deno.build.os === "windows") notImplemented(`"mode" on Windows`);
|
|
|
|
await Deno.chmod(pathOrRid as string, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
await Deno.writeAll(file, data as Uint8Array);
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
} finally {
|
|
|
|
// Make sure to close resource
|
|
|
|
if (!isRid && file) file.close();
|
|
|
|
callbackFn(error);
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}
|
2020-05-15 09:50:27 -04:00
|
|
|
|
|
|
|
export function writeFileSync(
|
2020-05-22 22:11:10 -04:00
|
|
|
pathOrRid: string | number | URL,
|
2020-05-15 09:50:27 -04:00
|
|
|
data: string | Uint8Array,
|
2020-07-14 15:24:17 -04:00
|
|
|
options?: Encodings | WriteFileOptions,
|
2020-05-15 09:50:27 -04:00
|
|
|
): void {
|
2020-05-22 22:11:10 -04:00
|
|
|
pathOrRid = pathOrRid instanceof URL ? fromFileUrl(pathOrRid) : pathOrRid;
|
|
|
|
|
2020-05-15 09:50:27 -04:00
|
|
|
const flag: string | undefined = isFileOptions(options)
|
|
|
|
? options.flag
|
|
|
|
: undefined;
|
|
|
|
|
|
|
|
const mode: number | undefined = isFileOptions(options)
|
|
|
|
? options.mode
|
|
|
|
: undefined;
|
|
|
|
|
2020-06-06 15:56:49 -04:00
|
|
|
const encoding = checkEncoding(getEncoding(options)) || "utf8";
|
2020-05-15 09:50:27 -04:00
|
|
|
const openOptions = getOpenOptions(flag || "w");
|
|
|
|
|
2020-06-27 16:52:49 -04:00
|
|
|
if (typeof data === "string") data = Buffer.from(data, encoding);
|
2020-05-15 09:50:27 -04:00
|
|
|
|
|
|
|
const isRid = typeof pathOrRid === "number";
|
|
|
|
let file;
|
|
|
|
|
|
|
|
let error: Error | null = null;
|
|
|
|
try {
|
|
|
|
file = isRid
|
|
|
|
? new Deno.File(pathOrRid as number)
|
|
|
|
: Deno.openSync(pathOrRid as string, openOptions);
|
|
|
|
|
|
|
|
if (!isRid && mode) {
|
|
|
|
if (Deno.build.os === "windows") notImplemented(`"mode" on Windows`);
|
|
|
|
Deno.chmodSync(pathOrRid as string, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
Deno.writeAllSync(file, data as Uint8Array);
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
} finally {
|
|
|
|
// Make sure to close resource
|
|
|
|
if (!isRid && file) file.close();
|
|
|
|
|
|
|
|
if (error) throw error;
|
|
|
|
}
|
|
|
|
}
|