2020-05-04 18:59:37 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
import { 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 {
|
2020-06-23 22:32:43 -04:00
|
|
|
Encodings,
|
2020-05-04 18:59:37 -04:00
|
|
|
WriteFileOptions,
|
|
|
|
CallbackWithError,
|
|
|
|
isFileOptions,
|
|
|
|
getEncoding,
|
2020-06-06 15:56:49 -04:00
|
|
|
checkEncoding,
|
2020-05-04 18:59:37 -04:00
|
|
|
getOpenOptions,
|
|
|
|
} 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-05-04 18:59:37 -04:00
|
|
|
callback?: CallbackWithError
|
|
|
|
): 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-06-23 22:32:43 -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;
|
|
|
|
}
|
|
|
|
}
|