mirror of
https://github.com/denoland/deno.git
synced 2024-11-24 15:19:26 -05:00
feat: support createNew
in Deno.writeFile
(#17023)
This commit is contained in:
parent
a2ba573e77
commit
5d9bb8b4b0
4 changed files with 43 additions and 4 deletions
|
@ -96,6 +96,20 @@ Deno.test(
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Deno.test(
|
||||||
|
{ permissions: { read: true, write: true } },
|
||||||
|
function writeFileSyncCreateNew() {
|
||||||
|
const enc = new TextEncoder();
|
||||||
|
const data = enc.encode("Hello");
|
||||||
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
||||||
|
Deno.writeFileSync(filename, data, { createNew: true });
|
||||||
|
|
||||||
|
assertThrows(() => {
|
||||||
|
Deno.writeFileSync(filename, data, { createNew: true });
|
||||||
|
}, Deno.errors.AlreadyExists);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
Deno.test(
|
Deno.test(
|
||||||
{ permissions: { read: true, write: true } },
|
{ permissions: { read: true, write: true } },
|
||||||
function writeFileSyncAppend() {
|
function writeFileSyncAppend() {
|
||||||
|
@ -216,6 +230,19 @@ Deno.test(
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Deno.test(
|
||||||
|
{ permissions: { read: true, write: true } },
|
||||||
|
async function writeFileCreateNew() {
|
||||||
|
const enc = new TextEncoder();
|
||||||
|
const data = enc.encode("Hello");
|
||||||
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
||||||
|
await Deno.writeFile(filename, data, { createNew: true });
|
||||||
|
await assertRejects(async () => {
|
||||||
|
await Deno.writeFile(filename, data, { createNew: true });
|
||||||
|
}, Deno.errors.AlreadyExists);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
Deno.test(
|
Deno.test(
|
||||||
{ permissions: { read: true, write: true } },
|
{ permissions: { read: true, write: true } },
|
||||||
async function writeFileAppend() {
|
async function writeFileAppend() {
|
||||||
|
|
4
cli/tsc/dts/lib.deno.ns.d.ts
vendored
4
cli/tsc/dts/lib.deno.ns.d.ts
vendored
|
@ -3296,6 +3296,10 @@ declare namespace Deno {
|
||||||
/** Sets the option to allow creating a new file, if one doesn't already
|
/** Sets the option to allow creating a new file, if one doesn't already
|
||||||
* exist at the specified path (defaults to `true`). */
|
* exist at the specified path (defaults to `true`). */
|
||||||
create?: boolean;
|
create?: boolean;
|
||||||
|
/** Defaults to `false`. If set to `true`, no file, directory, or symlink is
|
||||||
|
* allowed to exist at the target location. When createNew is set to `true`,
|
||||||
|
* `create` is ignored. */
|
||||||
|
createNew?: boolean;
|
||||||
/** Permissions always applied to file. */
|
/** Permissions always applied to file. */
|
||||||
mode?: number;
|
mode?: number;
|
||||||
/** An abort signal to allow cancellation of the file write operation.
|
/** An abort signal to allow cancellation of the file write operation.
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
options.mode,
|
options.mode,
|
||||||
options.append ?? false,
|
options.append ?? false,
|
||||||
options.create ?? true,
|
options.create ?? true,
|
||||||
|
options.createNew ?? false,
|
||||||
data,
|
data,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -41,6 +42,7 @@
|
||||||
options.mode,
|
options.mode,
|
||||||
options.append ?? false,
|
options.append ?? false,
|
||||||
options.create ?? true,
|
options.create ?? true,
|
||||||
|
options.createNew ?? false,
|
||||||
data,
|
data,
|
||||||
cancelRid,
|
cancelRid,
|
||||||
);
|
);
|
||||||
|
|
|
@ -222,14 +222,18 @@ async fn op_open_async(
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn write_open_options(create: bool, append: bool) -> OpenOptions {
|
fn write_open_options(
|
||||||
|
create: bool,
|
||||||
|
append: bool,
|
||||||
|
create_new: bool,
|
||||||
|
) -> OpenOptions {
|
||||||
OpenOptions {
|
OpenOptions {
|
||||||
read: false,
|
read: false,
|
||||||
write: true,
|
write: true,
|
||||||
create,
|
create,
|
||||||
truncate: !append,
|
truncate: !append,
|
||||||
append,
|
append,
|
||||||
create_new: false,
|
create_new,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -240,13 +244,14 @@ fn op_write_file_sync(
|
||||||
mode: Option<u32>,
|
mode: Option<u32>,
|
||||||
append: bool,
|
append: bool,
|
||||||
create: bool,
|
create: bool,
|
||||||
|
create_new: bool,
|
||||||
data: ZeroCopyBuf,
|
data: ZeroCopyBuf,
|
||||||
) -> Result<(), AnyError> {
|
) -> Result<(), AnyError> {
|
||||||
let (path, open_options) = open_helper(
|
let (path, open_options) = open_helper(
|
||||||
state,
|
state,
|
||||||
&path,
|
&path,
|
||||||
mode,
|
mode,
|
||||||
Some(&write_open_options(create, append)),
|
Some(&write_open_options(create, append, create_new)),
|
||||||
"Deno.writeFileSync()",
|
"Deno.writeFileSync()",
|
||||||
)?;
|
)?;
|
||||||
write_file(&path, open_options, mode, data)
|
write_file(&path, open_options, mode, data)
|
||||||
|
@ -259,6 +264,7 @@ async fn op_write_file_async(
|
||||||
mode: Option<u32>,
|
mode: Option<u32>,
|
||||||
append: bool,
|
append: bool,
|
||||||
create: bool,
|
create: bool,
|
||||||
|
create_new: bool,
|
||||||
data: ZeroCopyBuf,
|
data: ZeroCopyBuf,
|
||||||
cancel_rid: Option<ResourceId>,
|
cancel_rid: Option<ResourceId>,
|
||||||
) -> Result<(), AnyError> {
|
) -> Result<(), AnyError> {
|
||||||
|
@ -274,7 +280,7 @@ async fn op_write_file_async(
|
||||||
&mut state.borrow_mut(),
|
&mut state.borrow_mut(),
|
||||||
&path,
|
&path,
|
||||||
mode,
|
mode,
|
||||||
Some(&write_open_options(create, append)),
|
Some(&write_open_options(create, append, create_new)),
|
||||||
"Deno.writeFile()",
|
"Deno.writeFile()",
|
||||||
)?;
|
)?;
|
||||||
let write_future = tokio::task::spawn_blocking(move || {
|
let write_future = tokio::task::spawn_blocking(move || {
|
||||||
|
|
Loading…
Reference in a new issue