import { FileOptions, isFileOptions } from "./_fs_common.ts";import { notImplemented } from "../_utils.ts";
export async function appendFile( pathOrRid: string | number, data: string, optionsOrCallback: string | FileOptions | Function, callback?: Function): Promise<void> { const callbackFn: Function | undefined = optionsOrCallback instanceof Function ? optionsOrCallback : callback; const options: string | FileOptions | undefined = optionsOrCallback instanceof Function ? undefined : optionsOrCallback; if (!callbackFn) { throw new Error("No callback function supplied"); }
validateEncoding(options);
let rid = -1; try { if (typeof pathOrRid === "number") { rid = pathOrRid; } else { const mode: number | undefined = isFileOptions(options) ? options.mode : undefined; const flag: string | undefined = isFileOptions(options) ? options.flag : undefined;
if (mode) { notImplemented("Deno does not yet support setting mode on create"); }
const file = await Deno.open(pathOrRid, getOpenOptions(flag)); rid = file.rid; }
const buffer: Uint8Array = new TextEncoder().encode(data);
await Deno.write(rid, buffer); callbackFn(); } catch (err) { callbackFn(err); } finally { if (typeof pathOrRid === "string" && rid != -1) { Deno.close(rid); } }}
export function appendFileSync( pathOrRid: string | number, data: string, options?: string | FileOptions): void { let rid = -1;
validateEncoding(options);
try { if (typeof pathOrRid === "number") { rid = pathOrRid; } else { const mode: number | undefined = isFileOptions(options) ? options.mode : undefined; const flag: string | undefined = isFileOptions(options) ? options.flag : undefined;
if (mode) { notImplemented("Deno does not yet support setting mode on create"); }
const file = Deno.openSync(pathOrRid, getOpenOptions(flag)); rid = file.rid; }
const buffer: Uint8Array = new TextEncoder().encode(data);
Deno.writeSync(rid, buffer); } finally { if (typeof pathOrRid === "string" && rid != -1) { Deno.close(rid); } }}
function validateEncoding( encodingOption: string | FileOptions | undefined): void { if (!encodingOption) return;
if (typeof encodingOption === "string") { if (encodingOption !== "utf8") { throw new Error("Only 'utf8' encoding is currently supported"); } } else if (encodingOption.encoding && encodingOption.encoding !== "utf8") { throw new Error("Only 'utf8' encoding is currently supported"); }}
function getOpenOptions(flag: string | undefined): Deno.OpenOptions { if (!flag) { return { create: true, append: true }; }
let openOptions: Deno.OpenOptions; switch (flag) { case "a": { openOptions = { create: true, append: true }; break; } case "ax": { openOptions = { createNew: true, write: true, append: true }; break; } case "a+": { openOptions = { read: true, create: true, append: true }; break; } case "ax+": { openOptions = { read: true, createNew: true, append: true }; break; } case "r": { openOptions = { read: true }; break; } case "r+": { openOptions = { read: true, write: true }; break; } case "w": { openOptions = { create: true, write: true, truncate: true }; break; } case "wx": { openOptions = { createNew: true, write: true }; break; } case "w+": { openOptions = { create: true, write: true, truncate: true, read: true }; break; } case "wx+": { openOptions = { createNew: true, write: true, read: true }; break; } case "as": { openOptions = { create: true, append: true }; } case "as+": { openOptions = { create: true, read: true, append: true }; } case "rs+": { openOptions = { create: true, read: true, write: true }; } default: { throw new Error(`Unrecognized file system flag: ${flag}`); } }
return openOptions;}