diff options
author | Marcos Casagrande <marcoscvp90@gmail.com> | 2020-05-05 00:59:37 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-04 18:59:37 -0400 |
commit | f0aea98c85e18b297593ed6483b620945483fa37 (patch) | |
tree | 4176b47a2162e2713169a1d7858fdb6aec3a4ed5 /std/node/_fs/_fs_appendFile.ts | |
parent | 5f67a202ff59f25ea183c261f664a6db06407e17 (diff) |
feat(std/node): fs.writefile / fs.promises.writeFile (#5054)
Diffstat (limited to 'std/node/_fs/_fs_appendFile.ts')
-rw-r--r-- | std/node/_fs/_fs_appendFile.ts | 92 |
1 files changed, 10 insertions, 82 deletions
diff --git a/std/node/_fs/_fs_appendFile.ts b/std/node/_fs/_fs_appendFile.ts index b116589a0..c0f347dbf 100644 --- a/std/node/_fs/_fs_appendFile.ts +++ b/std/node/_fs/_fs_appendFile.ts @@ -1,5 +1,10 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { FileOptions, isFileOptions, CallbackWithError } from "./_fs_common.ts"; +import { + WriteFileOptions, + isFileOptions, + CallbackWithError, + getOpenOptions, +} from "./_fs_common.ts"; import { notImplemented } from "../_utils.ts"; import { fromFileUrl } from "../path.ts"; @@ -10,13 +15,13 @@ import { fromFileUrl } from "../path.ts"; export function appendFile( pathOrRid: string | number | URL, data: string, - optionsOrCallback: string | FileOptions | CallbackWithError, + optionsOrCallback: string | WriteFileOptions | CallbackWithError, callback?: CallbackWithError ): void { pathOrRid = pathOrRid instanceof URL ? fromFileUrl(pathOrRid) : pathOrRid; const callbackFn: CallbackWithError | undefined = optionsOrCallback instanceof Function ? optionsOrCallback : callback; - const options: string | FileOptions | undefined = + const options: string | WriteFileOptions | undefined = optionsOrCallback instanceof Function ? undefined : optionsOrCallback; if (!callbackFn) { throw new Error("No callback function supplied"); @@ -74,7 +79,7 @@ function closeRidIfNecessary(isPathString: boolean, rid: number): void { export function appendFileSync( pathOrRid: string | number | URL, data: string, - options?: string | FileOptions + options?: string | WriteFileOptions ): void { let rid = -1; @@ -110,7 +115,7 @@ export function appendFileSync( } function validateEncoding( - encodingOption: string | FileOptions | undefined + encodingOption: string | WriteFileOptions | undefined ): void { if (!encodingOption) return; @@ -122,80 +127,3 @@ function validateEncoding( 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": { - // 'a': Open file for appending. The file is created if it does not exist. - openOptions = { create: true, append: true }; - break; - } - case "ax": { - // 'ax': Like 'a' but fails if the path exists. - openOptions = { createNew: true, write: true, append: true }; - break; - } - case "a+": { - // 'a+': Open file for reading and appending. The file is created if it does not exist. - openOptions = { read: true, create: true, append: true }; - break; - } - case "ax+": { - // 'ax+': Like 'a+' but fails if the path exists. - openOptions = { read: true, createNew: true, append: true }; - break; - } - case "r": { - // 'r': Open file for reading. An exception occurs if the file does not exist. - openOptions = { read: true }; - break; - } - case "r+": { - // 'r+': Open file for reading and writing. An exception occurs if the file does not exist. - openOptions = { read: true, write: true }; - break; - } - case "w": { - // 'w': Open file for writing. The file is created (if it does not exist) or truncated (if it exists). - openOptions = { create: true, write: true, truncate: true }; - break; - } - case "wx": { - // 'wx': Like 'w' but fails if the path exists. - openOptions = { createNew: true, write: true }; - break; - } - case "w+": { - // 'w+': Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists). - openOptions = { create: true, write: true, truncate: true, read: true }; - break; - } - case "wx+": { - // 'wx+': Like 'w+' but fails if the path exists. - openOptions = { createNew: true, write: true, read: true }; - break; - } - case "as": { - // 'as': Open file for appending in synchronous mode. The file is created if it does not exist. - openOptions = { create: true, append: true }; - } - case "as+": { - // 'as+': Open file for reading and appending in synchronous mode. The file is created if it does not exist. - openOptions = { create: true, read: true, append: true }; - } - case "rs+": { - // 'rs+': Open file for reading and writing in synchronous mode. Instructs the operating system to bypass the local file system cache. - openOptions = { create: true, read: true, write: true }; - } - default: { - throw new Error(`Unrecognized file system flag: ${flag}`); - } - } - - return openOptions; -} |