fix(json): Add newline at the end of json files (denoland/deno#6885)

This commit is contained in:
Marcin Puc 2020-07-26 21:51:33 +02:00 committed by denobot
parent c0b2775e96
commit 7d4b49623d
3 changed files with 52 additions and 43 deletions

View File

@ -133,18 +133,24 @@ const foo = readJsonSync("./foo.json");
Writes an object to a JSON file. Writes an object to a JSON file.
**WriteJsonOptions** #### WriteJsonOptions
- replacer : An array of strings and numbers that acts as a approved list for - replacer : An array of strings and numbers that acts as a approved list for
selecting the object properties that will be stringified. selecting the object properties that will be stringified.
- space : Adds indentation, white space, and line break characters to the - space : Adds indentation, white space, and line break characters to the
return-value JSON text to make it easier to read. return-value JSON text to make it easier to read.
You can also specify options from `Deno.WriteFileOptions` to configure how the
file is written.
```ts ```ts
import { writeJson, writeJsonSync } from "https://deno.land/std/fs/mod.ts"; import { writeJson, writeJsonSync } from "https://deno.land/std/fs/mod.ts";
writeJson("./target.dat", { foo: "bar" }, { spaces: 2 }); // returns a promise 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
// appends to the file instead of rewriting
writeJsonSync("./target.dat", { foo: "bar" }, { append: true });
``` ```
### walk ### walk

View File

@ -2,9 +2,28 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
type Replacer = (key: string, value: any) => any; type Replacer = (key: string, value: any) => any;
export interface WriteJsonOptions { export interface WriteJsonOptions extends Deno.WriteFileOptions {
spaces?: number | string;
replacer?: Array<number | string> | Replacer; replacer?: Array<number | string> | Replacer;
spaces?: number | string;
}
function serialize(
filePath: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
object: any,
options: WriteJsonOptions,
): string {
try {
const jsonString = JSON.stringify(
object,
options.replacer as string[],
options.spaces,
);
return `${jsonString}\n`;
} catch (err) {
err.message = `${filePath}: ${err.message}`;
throw err;
}
} }
/* Writes an object to a JSON file. */ /* Writes an object to a JSON file. */
@ -14,20 +33,12 @@ export async function writeJson(
object: any, object: any,
options: WriteJsonOptions = {}, options: WriteJsonOptions = {},
): Promise<void> { ): Promise<void> {
let contentRaw = ""; const jsonString = serialize(filePath, object, options);
await Deno.writeTextFile(filePath, jsonString, {
try { append: options.append,
contentRaw = JSON.stringify( create: options.create,
object, mode: options.mode,
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. */ /* Writes an object to a JSON file. */
@ -37,18 +48,10 @@ export function writeJsonSync(
object: any, object: any,
options: WriteJsonOptions = {}, options: WriteJsonOptions = {},
): void { ): void {
let contentRaw = ""; const jsonString = serialize(filePath, object, options);
Deno.writeTextFileSync(filePath, jsonString, {
try { append: options.append,
contentRaw = JSON.stringify( create: options.create,
object, mode: options.mode,
options.replacer as string[], });
options.spaces,
);
} catch (err) {
err.message = `${filePath}: ${err.message}`;
throw err;
}
Deno.writeFileSync(filePath, new TextEncoder().encode(contentRaw));
} }

View File

@ -25,7 +25,7 @@ Deno.test("writeJsonIfNotExists", async function (): Promise<void> {
await Deno.remove(notExistsJsonFile); await Deno.remove(notExistsJsonFile);
assertEquals(new TextDecoder().decode(content), `{"a":"1"}`); assertEquals(new TextDecoder().decode(content), `{"a":"1"}\n`);
}); });
Deno.test("writeJsonIfExists", async function (): Promise<void> { Deno.test("writeJsonIfExists", async function (): Promise<void> {
@ -46,7 +46,7 @@ Deno.test("writeJsonIfExists", async function (): Promise<void> {
await Deno.remove(existsJsonFile); await Deno.remove(existsJsonFile);
assertEquals(new TextDecoder().decode(content), `{"a":"1"}`); assertEquals(new TextDecoder().decode(content), `{"a":"1"}\n`);
}); });
Deno.test("writeJsonIfExistsAnInvalidJson", async function (): Promise<void> { Deno.test("writeJsonIfExistsAnInvalidJson", async function (): Promise<void> {
@ -71,7 +71,7 @@ Deno.test("writeJsonIfExistsAnInvalidJson", async function (): Promise<void> {
await Deno.remove(existsInvalidJsonFile); await Deno.remove(existsInvalidJsonFile);
assertEquals(new TextDecoder().decode(content), `{"a":"1"}`); assertEquals(new TextDecoder().decode(content), `{"a":"1"}\n`);
}); });
Deno.test("writeJsonWithSpaces", async function (): Promise<void> { Deno.test("writeJsonWithSpaces", async function (): Promise<void> {
@ -93,7 +93,7 @@ Deno.test("writeJsonWithSpaces", async function (): Promise<void> {
await Deno.remove(existsJsonFile); await Deno.remove(existsJsonFile);
assertEquals(new TextDecoder().decode(content), `{\n "a": "1"\n}`); assertEquals(new TextDecoder().decode(content), `{\n "a": "1"\n}\n`);
}); });
Deno.test("writeJsonWithReplacer", async function (): Promise<void> { Deno.test("writeJsonWithReplacer", async function (): Promise<void> {
@ -121,7 +121,7 @@ Deno.test("writeJsonWithReplacer", async function (): Promise<void> {
await Deno.remove(existsJsonFile); await Deno.remove(existsJsonFile);
assertEquals(new TextDecoder().decode(content), `{"a":"1"}`); assertEquals(new TextDecoder().decode(content), `{"a":"1"}\n`);
}); });
Deno.test("writeJsonSyncIfNotExists", function (): void { Deno.test("writeJsonSyncIfNotExists", function (): void {
@ -140,7 +140,7 @@ Deno.test("writeJsonSyncIfNotExists", function (): void {
Deno.removeSync(notExistsJsonFile); Deno.removeSync(notExistsJsonFile);
assertEquals(new TextDecoder().decode(content), `{"a":"1"}`); assertEquals(new TextDecoder().decode(content), `{"a":"1"}\n`);
}); });
Deno.test("writeJsonSyncIfExists", function (): void { Deno.test("writeJsonSyncIfExists", function (): void {
@ -161,7 +161,7 @@ Deno.test("writeJsonSyncIfExists", function (): void {
Deno.removeSync(existsJsonFile); Deno.removeSync(existsJsonFile);
assertEquals(new TextDecoder().decode(content), `{"a":"1"}`); assertEquals(new TextDecoder().decode(content), `{"a":"1"}\n`);
}); });
Deno.test("writeJsonSyncIfExistsAnInvalidJson", function (): void { Deno.test("writeJsonSyncIfExistsAnInvalidJson", function (): void {
@ -186,10 +186,10 @@ Deno.test("writeJsonSyncIfExistsAnInvalidJson", function (): void {
Deno.removeSync(existsInvalidJsonFile); Deno.removeSync(existsInvalidJsonFile);
assertEquals(new TextDecoder().decode(content), `{"a":"1"}`); assertEquals(new TextDecoder().decode(content), `{"a":"1"}\n`);
}); });
Deno.test("writeJsonWithSpaces", function (): void { Deno.test("writeJsonSyncWithSpaces", function (): void {
const existsJsonFile = path.join(testdataDir, "file_write_spaces_sync.json"); const existsJsonFile = path.join(testdataDir, "file_write_spaces_sync.json");
const invalidJsonContent = new TextEncoder().encode(); const invalidJsonContent = new TextEncoder().encode();
@ -208,10 +208,10 @@ Deno.test("writeJsonWithSpaces", function (): void {
Deno.removeSync(existsJsonFile); Deno.removeSync(existsJsonFile);
assertEquals(new TextDecoder().decode(content), `{\n "a": "1"\n}`); assertEquals(new TextDecoder().decode(content), `{\n "a": "1"\n}\n`);
}); });
Deno.test("writeJsonWithReplacer", function (): void { Deno.test("writeJsonSyncWithReplacer", function (): void {
const existsJsonFile = path.join( const existsJsonFile = path.join(
testdataDir, testdataDir,
"file_write_replacer_sync.json", "file_write_replacer_sync.json",
@ -239,5 +239,5 @@ Deno.test("writeJsonWithReplacer", function (): void {
Deno.removeSync(existsJsonFile); Deno.removeSync(existsJsonFile);
assertEquals(new TextDecoder().decode(content), `{"a":"1"}`); assertEquals(new TextDecoder().decode(content), `{"a":"1"}\n`);
}); });