-
Notifications
You must be signed in to change notification settings - Fork 629
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add writeJson/writeJsonSync for fs modules #271
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7e6003d
feat: add writeJson/writeJsonSync for fs modules
axetroy ef73f2e
styles: follow the style guide
axetroy bb51bce
Rename WriteJsonOption to WriteJsonOptions
kitsonk 9523765
refactor: reanme WriteJsonOption to WriteJsonOptions
axetroy 61f3da7
fix: eslint error
axetroy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// 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 { | ||
spaces?: number | string; | ||
replacer?: Array<number | string> | Replacer; | ||
} | ||
|
||
/* Writes an object to a JSON file. */ | ||
export async function writeJson( | ||
filePath: string, | ||
object: any, | ||
options: WriteJsonOptions = {} | ||
): Promise<void> { | ||
filePath = path.resolve(filePath); | ||
|
||
let contentRaw = ""; | ||
|
||
try { | ||
contentRaw = JSON.stringify( | ||
object, | ||
options.replacer as string[], | ||
options.spaces | ||
); | ||
} 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 function writeJsonSync( | ||
filePath: string, | ||
object: any, | ||
options: WriteJsonOptions = {} | ||
): void { | ||
filePath = path.resolve(filePath); | ||
|
||
let contentRaw = ""; | ||
|
||
try { | ||
contentRaw = JSON.stringify( | ||
object, | ||
options.replacer as string[], | ||
options.spaces | ||
); | ||
} catch (err) { | ||
err.message = `${filePath}: ${err.message}`; | ||
throw err; | ||
} | ||
|
||
Deno.writeFileSync(filePath, new TextEncoder().encode(contentRaw)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we ignore any warning?
I didn't find the answer in the style guide.
But it seems that the other modules have not ignored it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've just fixed the date module. But we may not have any warning. I'll check on the flag module to fix it.