mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 09:31:22 -05:00
BREAKING(std/fs): remove writeJson and writeJsonSync (#7256)
This commit is contained in:
parent
d4b6b25def
commit
334ed0e2f4
4 changed files with 0 additions and 283 deletions
|
@ -118,30 +118,6 @@ copySync("./foo", "./existingFolder", { overwrite: true });
|
|||
// Will overwrite existingFolder
|
||||
```
|
||||
|
||||
### writeJson
|
||||
|
||||
Writes an object to a JSON file.
|
||||
|
||||
#### WriteJsonOptions
|
||||
|
||||
- replacer : An array of strings and numbers that acts as a approved list for
|
||||
selecting the object properties that will be stringified.
|
||||
- space : Adds indentation, white space, and line break characters to the
|
||||
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
|
||||
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
|
||||
|
||||
// appends to the file instead of rewriting
|
||||
writeJsonSync("./target.dat", { foo: "bar" }, { append: true });
|
||||
```
|
||||
|
||||
### walk
|
||||
|
||||
Iterate all files in a directory recursively.
|
||||
|
|
|
@ -8,6 +8,5 @@ export * from "./exists.ts";
|
|||
export * from "./expand_glob.ts";
|
||||
export * from "./move.ts";
|
||||
export * from "./copy.ts";
|
||||
export * from "./write_json.ts";
|
||||
export * from "./walk.ts";
|
||||
export * from "./eol.ts";
|
||||
|
|
|
@ -1,57 +0,0 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
type Replacer = (key: string, value: any) => any;
|
||||
|
||||
export interface WriteJsonOptions extends Deno.WriteFileOptions {
|
||||
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. */
|
||||
export async function writeJson(
|
||||
filePath: string,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
object: any,
|
||||
options: WriteJsonOptions = {},
|
||||
): Promise<void> {
|
||||
const jsonString = serialize(filePath, object, options);
|
||||
await Deno.writeTextFile(filePath, jsonString, {
|
||||
append: options.append,
|
||||
create: options.create,
|
||||
mode: options.mode,
|
||||
});
|
||||
}
|
||||
|
||||
/* Writes an object to a JSON file. */
|
||||
export function writeJsonSync(
|
||||
filePath: string,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
object: any,
|
||||
options: WriteJsonOptions = {},
|
||||
): void {
|
||||
const jsonString = serialize(filePath, object, options);
|
||||
Deno.writeTextFileSync(filePath, jsonString, {
|
||||
append: options.append,
|
||||
create: options.create,
|
||||
mode: options.mode,
|
||||
});
|
||||
}
|
|
@ -1,201 +0,0 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
import * as path from "../path/mod.ts";
|
||||
import {
|
||||
assertEquals,
|
||||
assertThrows,
|
||||
assertThrowsAsync,
|
||||
} from "../testing/asserts.ts";
|
||||
import {
|
||||
exists,
|
||||
existsSync,
|
||||
} from "./exists.ts";
|
||||
import { writeJson, writeJsonSync } from "./write_json.ts";
|
||||
|
||||
const testdataDir = path.resolve("fs", "testdata");
|
||||
|
||||
Deno.test("writeJson not exists", async function (): Promise<void> {
|
||||
const notExistsJsonFile = path.join(testdataDir, "writeJson_not_exists.json");
|
||||
|
||||
await writeJson(notExistsJsonFile, { a: "1" });
|
||||
|
||||
const content = await Deno.readTextFile(notExistsJsonFile);
|
||||
|
||||
await Deno.remove(notExistsJsonFile);
|
||||
|
||||
assertEquals(content, `{"a":"1"}\n`);
|
||||
});
|
||||
|
||||
Deno.test("writeJson if not exists", async function (): Promise<void> {
|
||||
const notExistsJsonFile = path.join(
|
||||
testdataDir,
|
||||
"writeJson_file_not_exists.json",
|
||||
);
|
||||
|
||||
try {
|
||||
assertThrowsAsync(
|
||||
async function (): Promise<void> {
|
||||
await writeJson(notExistsJsonFile, { a: "1" }, { create: false });
|
||||
},
|
||||
Deno.errors.NotFound,
|
||||
);
|
||||
} finally {
|
||||
if (await exists(notExistsJsonFile)) await Deno.remove(notExistsJsonFile);
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("writeJson exists", async function (): Promise<void> {
|
||||
const existsJsonFile = path.join(testdataDir, "writeJson_exists.json");
|
||||
await Deno.writeFile(existsJsonFile, new Uint8Array());
|
||||
|
||||
try {
|
||||
await writeJson(existsJsonFile, { a: "1" });
|
||||
const content = await Deno.readTextFile(existsJsonFile);
|
||||
assertEquals(content, `{"a":"1"}\n`);
|
||||
} finally {
|
||||
await Deno.remove(existsJsonFile);
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("writeJson spaces", async function (): Promise<void> {
|
||||
const existsJsonFile = path.join(testdataDir, "writeJson_spaces.json");
|
||||
await Deno.writeFile(existsJsonFile, new Uint8Array());
|
||||
|
||||
try {
|
||||
await writeJson(existsJsonFile, { a: "1" }, { spaces: 2 });
|
||||
const content = await Deno.readTextFile(existsJsonFile);
|
||||
assertEquals(content, `{\n "a": "1"\n}\n`);
|
||||
} finally {
|
||||
await Deno.remove(existsJsonFile);
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("writeJson replacer", async function (): Promise<void> {
|
||||
const existsJsonFile = path.join(testdataDir, "writeJson_replacer.json");
|
||||
await Deno.writeFile(existsJsonFile, new Uint8Array());
|
||||
|
||||
try {
|
||||
await writeJson(
|
||||
existsJsonFile,
|
||||
{ a: "1", b: "2", c: "3" },
|
||||
{ replacer: ["a"] },
|
||||
);
|
||||
|
||||
const content = await Deno.readTextFile(existsJsonFile);
|
||||
assertEquals(content, `{"a":"1"}\n`);
|
||||
} finally {
|
||||
await Deno.remove(existsJsonFile);
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("writeJson append", async function (): Promise<void> {
|
||||
const existsJsonFile = path.join(testdataDir, "writeJson_append.json");
|
||||
await Deno.writeFile(existsJsonFile, new Uint8Array());
|
||||
|
||||
try {
|
||||
await writeJson(existsJsonFile, { a: "1" }, { append: true });
|
||||
await writeJson(existsJsonFile, { b: "2" }, { append: true });
|
||||
|
||||
const content = await Deno.readTextFile(existsJsonFile);
|
||||
assertEquals(content, `{"a":"1"}\n{"b":"2"}\n`);
|
||||
} finally {
|
||||
await Deno.remove(existsJsonFile);
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("writeJsonSync not exists", function (): void {
|
||||
const notExistsJsonFile = path.join(
|
||||
testdataDir,
|
||||
"writeJsonSync_not_exists.json",
|
||||
);
|
||||
|
||||
writeJsonSync(notExistsJsonFile, { a: "1" });
|
||||
|
||||
const content = Deno.readTextFileSync(notExistsJsonFile);
|
||||
|
||||
Deno.removeSync(notExistsJsonFile);
|
||||
|
||||
assertEquals(content, `{"a":"1"}\n`);
|
||||
});
|
||||
|
||||
Deno.test("writeJsonSync if not exists", function (): void {
|
||||
const notExistsJsonFile = path.join(
|
||||
testdataDir,
|
||||
"writeJsonSync_file_not_exists.json",
|
||||
);
|
||||
|
||||
try {
|
||||
assertThrows(
|
||||
function (): void {
|
||||
writeJsonSync(notExistsJsonFile, { a: "1" }, { create: false });
|
||||
},
|
||||
Deno.errors.NotFound,
|
||||
);
|
||||
} finally {
|
||||
if (existsSync(notExistsJsonFile)) Deno.removeSync(notExistsJsonFile);
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("writeJsonSync exists", function (): void {
|
||||
const existsJsonFile = path.join(testdataDir, "writeJsonSync_exists.json");
|
||||
Deno.writeFileSync(existsJsonFile, new Uint8Array());
|
||||
|
||||
try {
|
||||
writeJsonSync(existsJsonFile, { a: "1" });
|
||||
const content = Deno.readTextFileSync(existsJsonFile);
|
||||
assertEquals(content, `{"a":"1"}\n`);
|
||||
} finally {
|
||||
Deno.removeSync(existsJsonFile);
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("writeJsonSync spaces", function (): void {
|
||||
const existsJsonFile = path.join(testdataDir, "writeJsonSync_spaces.json");
|
||||
|
||||
Deno.writeFileSync(existsJsonFile, new Uint8Array());
|
||||
|
||||
try {
|
||||
writeJsonSync(existsJsonFile, { a: "1" }, { spaces: 2 });
|
||||
const content = Deno.readTextFileSync(existsJsonFile);
|
||||
assertEquals(content, `{\n "a": "1"\n}\n`);
|
||||
} finally {
|
||||
Deno.removeSync(existsJsonFile);
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("writeJsonSync replacer", function (): void {
|
||||
const existsJsonFile = path.join(
|
||||
testdataDir,
|
||||
"writeJsonSync_replacer.json",
|
||||
);
|
||||
|
||||
Deno.writeFileSync(existsJsonFile, new Uint8Array());
|
||||
|
||||
try {
|
||||
writeJsonSync(
|
||||
existsJsonFile,
|
||||
{ a: "1", b: "2", c: "3" },
|
||||
{ replacer: ["a"] },
|
||||
);
|
||||
|
||||
const content = Deno.readTextFileSync(existsJsonFile);
|
||||
assertEquals(content, `{"a":"1"}\n`);
|
||||
} finally {
|
||||
Deno.removeSync(existsJsonFile);
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("writeJsonSync append", function (): void {
|
||||
const existsJsonFile = path.join(testdataDir, "writeJsonSync_append.json");
|
||||
|
||||
Deno.writeFileSync(existsJsonFile, new Uint8Array());
|
||||
|
||||
try {
|
||||
writeJsonSync(existsJsonFile, { a: "1" }, { append: true });
|
||||
writeJsonSync(existsJsonFile, { b: "2" }, { append: true });
|
||||
|
||||
const content = Deno.readTextFileSync(existsJsonFile);
|
||||
assertEquals(content, `{"a":"1"}\n{"b":"2"}\n`);
|
||||
} finally {
|
||||
Deno.removeSync(existsJsonFile);
|
||||
}
|
||||
});
|
Loading…
Add table
Reference in a new issue