From 7e6003db16a3b05481ba47e1d320414e00226a55 Mon Sep 17 00:00:00 2001 From: axetroy Date: Thu, 14 Mar 2019 01:34:08 +0800 Subject: [PATCH 1/5] feat: add writeJson/writeJsonSync for fs modules --- fs/write_json.ts | 79 ++++++++++++++ fs/write_json_test.ts | 244 ++++++++++++++++++++++++++++++++++++++++++ test.ts | 1 + 3 files changed, 324 insertions(+) create mode 100644 fs/write_json.ts create mode 100644 fs/write_json_test.ts diff --git a/fs/write_json.ts b/fs/write_json.ts new file mode 100644 index 000000000000..7de37deb6bc4 --- /dev/null +++ b/fs/write_json.ts @@ -0,0 +1,79 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +import * as path from "./path/mod.ts"; + +interface WriteJsonOption { + spaces?: number | string; + replacer?: (number | string)[] | null | Replacer; +} + +type Replacer = (key: string, value: any) => any; + +/** + * Writes an object to a JSON file. + * @export + * @param {string} filePath + * @param {*} object + * @param {WriteJsonOption} [options] + * @returns {Promise} + */ +export async function writeJson( + filePath: string, + object: any, + options?: WriteJsonOption +): Promise { + filePath = path.resolve(filePath); + + let contentRaw: string = ""; + + try { + if (options) { + contentRaw = JSON.stringify( + object, + (options.replacer as string[]) || null, + options.spaces + ); + } else { + contentRaw = JSON.stringify(object); + } + } catch (err) { + err.message = `${filePath}: ${err.message}`; + throw err; + } + + await Deno.writeFile(filePath, new TextEncoder().encode(contentRaw)); +} + +/** + * Writes an object to a JSON file. + * @export + * @param {string} filePath + * @param {*} object + * @param {WriteJsonOption} [options] + * @returns {void} + */ +export function writeJsonSync( + filePath: string, + object: any, + options?: WriteJsonOption +): void { + filePath = path.resolve(filePath); + + let contentRaw: string = ""; + + try { + if (options) { + contentRaw = JSON.stringify( + object, + (options.replacer as string[]) || null, + options.spaces + ); + } else { + contentRaw = JSON.stringify(object); + } + } catch (err) { + err.message = `${filePath}: ${err.message}`; + throw err; + } + + Deno.writeFileSync(filePath, new TextEncoder().encode(contentRaw)); +} diff --git a/fs/write_json_test.ts b/fs/write_json_test.ts new file mode 100644 index 000000000000..acc36600108a --- /dev/null +++ b/fs/write_json_test.ts @@ -0,0 +1,244 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +import { test } from "../testing/mod.ts"; +import { + assertEquals, + assertThrowsAsync, + assertThrows +} from "../testing/asserts.ts"; +import { writeJson, writeJsonSync } from "./write_json.ts"; +import * as path from "./path/mod.ts"; + +const testdataDir = path.resolve("fs", "testdata"); + +test(async function writeJsonIfNotExists() { + const notExistsJsonFile = path.join(testdataDir, "file_not_exists.json"); + + await assertThrowsAsync( + async () => { + await writeJson(notExistsJsonFile, { a: "1" }); + throw new Error("should write success"); + }, + Error, + "should write success" + ); + + const content = await Deno.readFile(notExistsJsonFile); + + await Deno.remove(notExistsJsonFile); + + assertEquals(new TextDecoder().decode(content), `{"a":"1"}`); +}); + +test(async function writeJsonIfExists() { + const existsJsonFile = path.join(testdataDir, "file_write_exists.json"); + + await Deno.writeFile(existsJsonFile, new Uint8Array()); + + await assertThrowsAsync( + async () => { + await writeJson(existsJsonFile, { a: "1" }); + throw new Error("should write success"); + }, + Error, + "should write success" + ); + + const content = await Deno.readFile(existsJsonFile); + + await Deno.remove(existsJsonFile); + + assertEquals(new TextDecoder().decode(content), `{"a":"1"}`); +}); + +test(async function writeJsonIfExistsAnInvalidJson() { + const existsInvalidJsonFile = path.join( + testdataDir, + "file_write_invalid.json" + ); + + const invalidJsonContent = new TextEncoder().encode("[123}"); + await Deno.writeFile(existsInvalidJsonFile, invalidJsonContent); + + await assertThrowsAsync( + async () => { + await writeJson(existsInvalidJsonFile, { a: "1" }); + throw new Error("should write success"); + }, + Error, + "should write success" + ); + + const content = await Deno.readFile(existsInvalidJsonFile); + + await Deno.remove(existsInvalidJsonFile); + + assertEquals(new TextDecoder().decode(content), `{"a":"1"}`); +}); + +test(async function writeJsonWithSpaces() { + const existsJsonFile = path.join(testdataDir, "file_write_spaces.json"); + + const invalidJsonContent = new TextEncoder().encode(); + await Deno.writeFile(existsJsonFile, invalidJsonContent); + + await assertThrowsAsync( + async () => { + await writeJson(existsJsonFile, { a: "1" }, { spaces: 2 }); + throw new Error("should write success"); + }, + Error, + "should write success" + ); + + const content = await Deno.readFile(existsJsonFile); + + await Deno.remove(existsJsonFile); + + assertEquals(new TextDecoder().decode(content), `{\n "a": "1"\n}`); +}); + +test(async function writeJsonWithReplacer() { + const existsJsonFile = path.join(testdataDir, "file_write_replacer.json"); + + const invalidJsonContent = new TextEncoder().encode(); + await Deno.writeFile(existsJsonFile, invalidJsonContent); + + await assertThrowsAsync( + async () => { + await writeJson( + existsJsonFile, + { a: "1", b: "2", c: "3" }, + { + replacer: ["a"] + } + ); + throw new Error("should write success"); + }, + Error, + "should write success" + ); + + const content = await Deno.readFile(existsJsonFile); + + await Deno.remove(existsJsonFile); + + assertEquals(new TextDecoder().decode(content), `{"a":"1"}`); +}); + +test(function writeJsonSyncIfNotExists() { + const notExistsJsonFile = path.join(testdataDir, "file_not_exists_sync.json"); + + assertThrows( + () => { + writeJsonSync(notExistsJsonFile, { a: "1" }); + throw new Error("should write success"); + }, + Error, + "should write success" + ); + + const content = Deno.readFileSync(notExistsJsonFile); + + Deno.removeSync(notExistsJsonFile); + + assertEquals(new TextDecoder().decode(content), `{"a":"1"}`); +}); + +test(function writeJsonSyncIfExists() { + const existsJsonFile = path.join(testdataDir, "file_write_exists_sync.json"); + + Deno.writeFileSync(existsJsonFile, new Uint8Array()); + + assertThrows( + () => { + writeJsonSync(existsJsonFile, { a: "1" }); + throw new Error("should write success"); + }, + Error, + "should write success" + ); + + const content = Deno.readFileSync(existsJsonFile); + + Deno.removeSync(existsJsonFile); + + assertEquals(new TextDecoder().decode(content), `{"a":"1"}`); +}); + +test(function writeJsonSyncIfExistsAnInvalidJson() { + const existsInvalidJsonFile = path.join( + testdataDir, + "file_write_invalid_sync.json" + ); + + const invalidJsonContent = new TextEncoder().encode("[123}"); + Deno.writeFileSync(existsInvalidJsonFile, invalidJsonContent); + + assertThrows( + () => { + writeJsonSync(existsInvalidJsonFile, { a: "1" }); + throw new Error("should write success"); + }, + Error, + "should write success" + ); + + const content = Deno.readFileSync(existsInvalidJsonFile); + + Deno.removeSync(existsInvalidJsonFile); + + assertEquals(new TextDecoder().decode(content), `{"a":"1"}`); +}); + +test(function writeJsonWithSpaces() { + const existsJsonFile = path.join(testdataDir, "file_write_spaces_sync.json"); + + const invalidJsonContent = new TextEncoder().encode(); + Deno.writeFileSync(existsJsonFile, invalidJsonContent); + + assertThrows( + () => { + writeJsonSync(existsJsonFile, { a: "1" }, { spaces: 2 }); + throw new Error("should write success"); + }, + Error, + "should write success" + ); + + const content = Deno.readFileSync(existsJsonFile); + + Deno.removeSync(existsJsonFile); + + assertEquals(new TextDecoder().decode(content), `{\n "a": "1"\n}`); +}); + +test(function writeJsonWithReplacer() { + const existsJsonFile = path.join( + testdataDir, + "file_write_replacer_sync.json" + ); + + const invalidJsonContent = new TextEncoder().encode(); + Deno.writeFileSync(existsJsonFile, invalidJsonContent); + + assertThrows( + () => { + writeJsonSync( + existsJsonFile, + { a: "1", b: "2", c: "3" }, + { + replacer: ["a"] + } + ); + throw new Error("should write success"); + }, + Error, + "should write success" + ); + + const content = Deno.readFileSync(existsJsonFile); + + Deno.removeSync(existsJsonFile); + + assertEquals(new TextDecoder().decode(content), `{"a":"1"}`); +}); diff --git a/test.ts b/test.ts index 01a68cd76c45..9034631a6ae9 100755 --- a/test.ts +++ b/test.ts @@ -19,6 +19,7 @@ import "./fs/ensure_dir_test.ts"; import "./fs/ensure_file_test.ts"; import "./fs/move_test.ts"; import "./fs/read_json_test.ts"; +import "./fs/write_json_test.ts"; import "./io/test.ts"; import "./http/server_test.ts"; import "./http/file_server_test.ts"; From ef73f2e3e157e5bbd09f1002b206d5d476b2911b Mon Sep 17 00:00:00 2001 From: axetroy Date: Thu, 14 Mar 2019 10:18:52 +0800 Subject: [PATCH 2/5] styles: follow the style guide --- fs/write_json.ts | 58 +++++++++++++++--------------------------------- 1 file changed, 18 insertions(+), 40 deletions(-) diff --git a/fs/write_json.ts b/fs/write_json.ts index 7de37deb6bc4..356b61fb50e3 100644 --- a/fs/write_json.ts +++ b/fs/write_json.ts @@ -1,40 +1,29 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. import * as path from "./path/mod.ts"; -interface WriteJsonOption { +type Replacer = (key: string, value: any) => any; + +export interface WriteJsonOption { spaces?: number | string; - replacer?: (number | string)[] | null | Replacer; + replacer?: Array | Replacer; } -type Replacer = (key: string, value: any) => any; - -/** - * Writes an object to a JSON file. - * @export - * @param {string} filePath - * @param {*} object - * @param {WriteJsonOption} [options] - * @returns {Promise} - */ +/* Writes an object to a JSON file. */ export async function writeJson( filePath: string, object: any, - options?: WriteJsonOption + options: WriteJsonOption = {} ): Promise { filePath = path.resolve(filePath); let contentRaw: string = ""; try { - if (options) { - contentRaw = JSON.stringify( - object, - (options.replacer as string[]) || null, - options.spaces - ); - } else { - contentRaw = JSON.stringify(object); - } + contentRaw = JSON.stringify( + object, + options.replacer as string[], + options.spaces + ); } catch (err) { err.message = `${filePath}: ${err.message}`; throw err; @@ -43,33 +32,22 @@ export async function writeJson( await Deno.writeFile(filePath, new TextEncoder().encode(contentRaw)); } -/** - * Writes an object to a JSON file. - * @export - * @param {string} filePath - * @param {*} object - * @param {WriteJsonOption} [options] - * @returns {void} - */ +/* Writes an object to a JSON file. */ export function writeJsonSync( filePath: string, object: any, - options?: WriteJsonOption + options: WriteJsonOption = {} ): void { filePath = path.resolve(filePath); let contentRaw: string = ""; try { - if (options) { - contentRaw = JSON.stringify( - object, - (options.replacer as string[]) || null, - options.spaces - ); - } else { - contentRaw = JSON.stringify(object); - } + contentRaw = JSON.stringify( + object, + options.replacer as string[], + options.spaces + ); } catch (err) { err.message = `${filePath}: ${err.message}`; throw err; From bb51bce49b7da88a70b4599e0a5e422972334d58 Mon Sep 17 00:00:00 2001 From: Kitson Kelly Date: Thu, 14 Mar 2019 10:21:43 +0800 Subject: [PATCH 3/5] Rename WriteJsonOption to WriteJsonOptions Co-Authored-By: axetroy --- fs/write_json.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/write_json.ts b/fs/write_json.ts index 356b61fb50e3..bfefe7cd039d 100644 --- a/fs/write_json.ts +++ b/fs/write_json.ts @@ -3,7 +3,7 @@ import * as path from "./path/mod.ts"; type Replacer = (key: string, value: any) => any; -export interface WriteJsonOption { +export interface WriteJsonOptions { spaces?: number | string; replacer?: Array | Replacer; } From 9523765ebe92ae76277f3ecac5982fcd1fa18dcf Mon Sep 17 00:00:00 2001 From: axetroy Date: Thu, 14 Mar 2019 10:22:35 +0800 Subject: [PATCH 4/5] refactor: reanme WriteJsonOption to WriteJsonOptions --- fs/write_json.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fs/write_json.ts b/fs/write_json.ts index bfefe7cd039d..7ac878b32b73 100644 --- a/fs/write_json.ts +++ b/fs/write_json.ts @@ -12,7 +12,7 @@ export interface WriteJsonOptions { export async function writeJson( filePath: string, object: any, - options: WriteJsonOption = {} + options: WriteJsonOptions = {} ): Promise { filePath = path.resolve(filePath); @@ -36,7 +36,7 @@ export async function writeJson( export function writeJsonSync( filePath: string, object: any, - options: WriteJsonOption = {} + options: WriteJsonOptions = {} ): void { filePath = path.resolve(filePath); From 61f3da7a9b53e7cb6e2bade1235fc4f7d8a33f43 Mon Sep 17 00:00:00 2001 From: axetroy Date: Thu, 14 Mar 2019 10:51:19 +0800 Subject: [PATCH 5/5] fix: eslint error --- fs/write_json.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fs/write_json.ts b/fs/write_json.ts index 7ac878b32b73..f94489095edc 100644 --- a/fs/write_json.ts +++ b/fs/write_json.ts @@ -1,6 +1,6 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +/* eslint-disable @typescript-eslint/no-explicit-any */ import * as path from "./path/mod.ts"; - type Replacer = (key: string, value: any) => any; export interface WriteJsonOptions { @@ -16,7 +16,7 @@ export async function writeJson( ): Promise { filePath = path.resolve(filePath); - let contentRaw: string = ""; + let contentRaw = ""; try { contentRaw = JSON.stringify( @@ -40,7 +40,7 @@ export function writeJsonSync( ): void { filePath = path.resolve(filePath); - let contentRaw: string = ""; + let contentRaw = ""; try { contentRaw = JSON.stringify(