summaryrefslogtreecommitdiff
path: root/std/node/_fs/_fs_writeFile.ts
diff options
context:
space:
mode:
authorMarcos Casagrande <marcoscvp90@gmail.com>2020-05-15 15:50:27 +0200
committerGitHub <noreply@github.com>2020-05-15 09:50:27 -0400
commitce57a1824d3c89d19460efb315b273a43d18373e (patch)
treec8b6ba52ec4834cd4398224cb3fe73c6b3e7b0f2 /std/node/_fs/_fs_writeFile.ts
parent62a7fcbdc40479b7fdae98eeaedab1efc7b95d4a (diff)
feat(std/node): fs.writeFileSync polyfill (#5414)
Diffstat (limited to 'std/node/_fs/_fs_writeFile.ts')
-rw-r--r--std/node/_fs/_fs_writeFile.ts44
1 files changed, 44 insertions, 0 deletions
diff --git a/std/node/_fs/_fs_writeFile.ts b/std/node/_fs/_fs_writeFile.ts
index c9f7fe125..b46620d9b 100644
--- a/std/node/_fs/_fs_writeFile.ts
+++ b/std/node/_fs/_fs_writeFile.ts
@@ -63,3 +63,47 @@ export function writeFile(
}
})();
}
+
+export function writeFileSync(
+ pathOrRid: string | number,
+ data: string | Uint8Array,
+ options?: string | WriteFileOptions
+): void {
+ const flag: string | undefined = isFileOptions(options)
+ ? options.flag
+ : undefined;
+
+ const mode: number | undefined = isFileOptions(options)
+ ? options.mode
+ : undefined;
+
+ const encoding = getEncoding(options) || "utf8";
+ const openOptions = getOpenOptions(flag || "w");
+
+ if (typeof data === "string" && encoding === "utf8")
+ data = new TextEncoder().encode(data) as Uint8Array;
+
+ 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;
+ }
+}