diff --git a/fs/README.md b/fs/README.md index b80cef7b84a742..93c95de03ed4df 100644 --- a/fs/README.md +++ b/fs/README.md @@ -134,7 +134,7 @@ for (const fileInfo of walk()) { } ``` -### writejson +### writeJson Writes an object to a JSON file. @@ -147,6 +147,34 @@ Writes an object to a JSON file. import { writeJson, writeJsonSync } from "https://deno.land/std/fs/mod.ts"; writeJson("./target.dat", { foo: "bar" }, { spaces: 2 }); // returns a promise -writeJsonSync("./target.dat", { foo: "bar" }, { replacer: ["foo"] }); -// void +writeJsonSync("./target.dat", { foo: "bar" }, { replacer: ["foo"] }); // void +``` + +### readFileStr + +Read file and output it as a string. + +**ReadOptions** + +- encoding : The encoding to read file. lowercased. + +```ts +import { readFileStr, readFileStrSync } from "https://deno.land/std/fs/mod.ts"; + +readFileStr("./target.dat", { encoding: "utf8" }); // returns a promise +readFileStrSync("./target.dat", { encoding: "utf8" }); // void +``` + +### writeFileStr + +Write the string to file. + +```ts +import { + writeFileStr, + writeFileStrSync +} from "https://deno.land/std/fs/mod.ts"; + +writeFileStr("./target.dat", "file content"); // returns a promise +writeFileStrSync("./target.dat", "file content"); // void ``` diff --git a/fs/mod.ts b/fs/mod.ts index 28b5753b9a7a89..789b6bdf8f5b21 100644 --- a/fs/mod.ts +++ b/fs/mod.ts @@ -6,6 +6,8 @@ export * from "./exists.ts"; export * from "./glob.ts"; export * from "./globrex.ts"; export * from "./move.ts"; +export * from "./read_file_str.ts"; +export * from "./write_file_str.ts"; export * from "./read_json.ts"; export * from "./write_json.ts"; export * from "./walk.ts"; diff --git a/fs/test.ts b/fs/test.ts index 025c0efb253b0f..08c6f2cf883b3f 100644 --- a/fs/test.ts +++ b/fs/test.ts @@ -10,6 +10,7 @@ import "./ensure_dir_test.ts"; import "./ensure_file_test.ts"; import "./move_test.ts"; import "./read_json_test.ts"; -import "./read_file_str_test.ts"; import "./write_json_test.ts"; +import "./read_file_str_test.ts"; +import "./write_file_str_test.ts"; import "./utils_test.ts"; diff --git a/fs/write_file_str.ts b/fs/write_file_str.ts new file mode 100644 index 00000000000000..a4a4beb5b162ce --- /dev/null +++ b/fs/write_file_str.ts @@ -0,0 +1,28 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. + +/** + * Write the string to file synchronously. + * + * @param filename File to write + * @param content The content write to file + * @returns void + */ +export function writeFileStrSync(filename: string, content: string): void { + const encoder = new TextEncoder(); + Deno.writeFileSync(filename, encoder.encode(content)); +} + +/** + * Write the string to file. + * + * @param filename File to write + * @param content The content write to file + * @returns Promise + */ +export async function writeFileStr( + filename: string, + content: string +): Promise { + const encoder = new TextEncoder(); + await Deno.writeFile(filename, encoder.encode(content)); +} diff --git a/fs/write_file_str_test.ts b/fs/write_file_str_test.ts new file mode 100644 index 00000000000000..4039b26f6c7fe3 --- /dev/null +++ b/fs/write_file_str_test.ts @@ -0,0 +1,38 @@ +import { test } from "../testing/mod.ts"; +import { assertEquals } from "../testing/asserts.ts"; +import { writeFileStr, writeFileStrSync } from "./write_file_str.ts"; +import * as path from "./path/mod.ts"; + +const testdataDir = path.resolve("fs", "testdata"); + +test(function testReadFileSync() { + const jsonFile = path.join(testdataDir, "write_file_1.json"); + const content = "write_file_str_test"; + writeFileStrSync(jsonFile, content); + + // make sure file have been create. + Deno.statSync(jsonFile); + + const result = new TextDecoder().decode(Deno.readFileSync(jsonFile)); + + // remove test file + Deno.removeSync(jsonFile); + + assertEquals(content, result); +}); + +test(async function testReadFile() { + const jsonFile = path.join(testdataDir, "write_file_2.json"); + const content = "write_file_str_test"; + await writeFileStr(jsonFile, content); + + // make sure file have been create. + await Deno.stat(jsonFile); + + const result = new TextDecoder().decode(await Deno.readFile(jsonFile)); + + // remove test file + await Deno.remove(jsonFile); + + assertEquals(content, result); +});