mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 07:14:47 -05:00
feat(cli): Add WriteFileOptions to writeTextFile & writeTextFileSync (#6280)
This commit is contained in:
parent
82aabb657a
commit
231899695d
3 changed files with 129 additions and 20 deletions
9
cli/js/lib.deno.ns.d.ts
vendored
9
cli/js/lib.deno.ns.d.ts
vendored
|
@ -1524,7 +1524,11 @@ declare namespace Deno {
|
|||
*
|
||||
* Requires `allow-write` permission, and `allow-read` if `options.create` is `false`.
|
||||
*/
|
||||
export function writeTextFileSync(path: string | URL, data: string): void;
|
||||
export function writeTextFileSync(
|
||||
path: string | URL,
|
||||
data: string,
|
||||
options?: WriteFileOptions
|
||||
): void;
|
||||
|
||||
/** Asynchronously write string `data` to the given `path`, by default creating a new file if needed,
|
||||
* else overwriting.
|
||||
|
@ -1537,7 +1541,8 @@ declare namespace Deno {
|
|||
*/
|
||||
export function writeTextFile(
|
||||
path: string | URL,
|
||||
data: string
|
||||
data: string,
|
||||
options?: WriteFileOptions
|
||||
): Promise<void>;
|
||||
|
||||
/** Synchronously truncates or extends the specified file, to reach the
|
||||
|
|
|
@ -1,23 +1,20 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
import { writeFileSync, writeFile, WriteFileOptions } from "./write_file.ts";
|
||||
|
||||
import { open, openSync } from "./files.ts";
|
||||
import { writeAll, writeAllSync } from "./buffer.ts";
|
||||
|
||||
export function writeTextFileSync(path: string | URL, data: string): void {
|
||||
const file = openSync(path, { write: true, create: true, truncate: true });
|
||||
const encoder = new TextEncoder();
|
||||
const contents = encoder.encode(data);
|
||||
writeAllSync(file, contents);
|
||||
file.close();
|
||||
}
|
||||
|
||||
export async function writeTextFile(
|
||||
export function writeTextFileSync(
|
||||
path: string | URL,
|
||||
data: string
|
||||
): Promise<void> {
|
||||
const file = await open(path, { write: true, create: true, truncate: true });
|
||||
data: string,
|
||||
options: WriteFileOptions = {}
|
||||
): void {
|
||||
const encoder = new TextEncoder();
|
||||
const contents = encoder.encode(data);
|
||||
await writeAll(file, contents);
|
||||
file.close();
|
||||
return writeFileSync(path, encoder.encode(data), options);
|
||||
}
|
||||
|
||||
export function writeTextFile(
|
||||
path: string | URL,
|
||||
data: string,
|
||||
options: WriteFileOptions = {}
|
||||
): Promise<void> {
|
||||
const encoder = new TextEncoder();
|
||||
return writeFile(path, encoder.encode(data), options);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import {
|
||||
unitTest,
|
||||
assert,
|
||||
assertEquals,
|
||||
assertThrows,
|
||||
assertThrowsAsync,
|
||||
|
@ -46,6 +47,59 @@ unitTest({ perms: { write: false } }, function writeTextFileSyncPerm(): void {
|
|||
}, Deno.errors.PermissionDenied);
|
||||
});
|
||||
|
||||
unitTest(
|
||||
{ perms: { read: true, write: true } },
|
||||
function writeTextFileSyncUpdateMode(): void {
|
||||
if (Deno.build.os !== "windows") {
|
||||
const data = "Hello";
|
||||
const filename = Deno.makeTempDirSync() + "/test.txt";
|
||||
Deno.writeTextFileSync(filename, data, { mode: 0o755 });
|
||||
assertEquals(Deno.statSync(filename).mode! & 0o777, 0o755);
|
||||
Deno.writeTextFileSync(filename, data, { mode: 0o666 });
|
||||
assertEquals(Deno.statSync(filename).mode! & 0o777, 0o666);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
unitTest(
|
||||
{ perms: { read: true, write: true } },
|
||||
function writeTextFileSyncCreate(): void {
|
||||
const data = "Hello";
|
||||
const filename = Deno.makeTempDirSync() + "/test.txt";
|
||||
let caughtError = false;
|
||||
// if create turned off, the file won't be created
|
||||
try {
|
||||
Deno.writeTextFileSync(filename, data, { create: false });
|
||||
} catch (e) {
|
||||
caughtError = true;
|
||||
assert(e instanceof Deno.errors.NotFound);
|
||||
}
|
||||
assert(caughtError);
|
||||
|
||||
// Turn on create, should have no error
|
||||
Deno.writeTextFileSync(filename, data, { create: true });
|
||||
Deno.writeTextFileSync(filename, data, { create: false });
|
||||
assertEquals("Hello", Deno.readTextFileSync(filename));
|
||||
}
|
||||
);
|
||||
|
||||
unitTest(
|
||||
{ perms: { read: true, write: true } },
|
||||
function writeTextFileSyncAppend(): void {
|
||||
const data = "Hello";
|
||||
const filename = Deno.makeTempDirSync() + "/test.txt";
|
||||
Deno.writeTextFileSync(filename, data);
|
||||
Deno.writeTextFileSync(filename, data, { append: true });
|
||||
assertEquals("HelloHello", Deno.readTextFileSync(filename));
|
||||
// Now attempt overwrite
|
||||
Deno.writeTextFileSync(filename, data, { append: false });
|
||||
assertEquals("Hello", Deno.readTextFileSync(filename));
|
||||
// append not set should also overwrite
|
||||
Deno.writeTextFileSync(filename, data);
|
||||
assertEquals("Hello", Deno.readTextFileSync(filename));
|
||||
}
|
||||
);
|
||||
|
||||
unitTest(
|
||||
{ perms: { read: true, write: true } },
|
||||
async function writeTextFileSuccess(): Promise<void> {
|
||||
|
@ -92,3 +146,56 @@ unitTest(
|
|||
}, Deno.errors.PermissionDenied);
|
||||
}
|
||||
);
|
||||
|
||||
unitTest(
|
||||
{ perms: { read: true, write: true } },
|
||||
async function writeTextFileUpdateMode(): Promise<void> {
|
||||
if (Deno.build.os !== "windows") {
|
||||
const data = "Hello";
|
||||
const filename = Deno.makeTempDirSync() + "/test.txt";
|
||||
await Deno.writeTextFile(filename, data, { mode: 0o755 });
|
||||
assertEquals(Deno.statSync(filename).mode! & 0o777, 0o755);
|
||||
await Deno.writeTextFile(filename, data, { mode: 0o666 });
|
||||
assertEquals(Deno.statSync(filename).mode! & 0o777, 0o666);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
unitTest(
|
||||
{ perms: { read: true, write: true } },
|
||||
async function writeTextFileCreate(): Promise<void> {
|
||||
const data = "Hello";
|
||||
const filename = Deno.makeTempDirSync() + "/test.txt";
|
||||
let caughtError = false;
|
||||
// if create turned off, the file won't be created
|
||||
try {
|
||||
await Deno.writeTextFile(filename, data, { create: false });
|
||||
} catch (e) {
|
||||
caughtError = true;
|
||||
assert(e instanceof Deno.errors.NotFound);
|
||||
}
|
||||
assert(caughtError);
|
||||
|
||||
// Turn on create, should have no error
|
||||
await Deno.writeTextFile(filename, data, { create: true });
|
||||
await Deno.writeTextFile(filename, data, { create: false });
|
||||
assertEquals("Hello", Deno.readTextFileSync(filename));
|
||||
}
|
||||
);
|
||||
|
||||
unitTest(
|
||||
{ perms: { read: true, write: true } },
|
||||
async function writeTextFileAppend(): Promise<void> {
|
||||
const data = "Hello";
|
||||
const filename = Deno.makeTempDirSync() + "/test.txt";
|
||||
await Deno.writeTextFile(filename, data);
|
||||
await Deno.writeTextFile(filename, data, { append: true });
|
||||
assertEquals("HelloHello", Deno.readTextFileSync(filename));
|
||||
// Now attempt overwrite
|
||||
await Deno.writeTextFile(filename, data, { append: false });
|
||||
assertEquals("Hello", Deno.readTextFileSync(filename));
|
||||
// append not set should also overwrite
|
||||
await Deno.writeTextFile(filename, data);
|
||||
assertEquals("Hello", Deno.readTextFileSync(filename));
|
||||
}
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue