From da0c29d6c396c45567df1bc38d0c23bcd6eaebce Mon Sep 17 00:00:00 2001 From: FabianLars Date: Wed, 16 Aug 2023 15:12:07 +0200 Subject: [PATCH 1/5] feat(api): Add `append` option to writeFile apis. --- .changes/fs-append-file.md | 6 ++++++ core/tauri/src/endpoints/file_system.rs | 21 +++++++++++++++++++-- tooling/api/src/fs.ts | 6 ++++++ 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 .changes/fs-append-file.md diff --git a/.changes/fs-append-file.md b/.changes/fs-append-file.md new file mode 100644 index 000000000000..8866892028fa --- /dev/null +++ b/.changes/fs-append-file.md @@ -0,0 +1,6 @@ +--- +'@tauri-apps/api': 'patch:enhance' +'tauri': 'patch:enhance' +--- + +Add `append` option to `FsOptions`, used in `writeTextFile` and `writeBinaryFile`, to be able to append to existing files instead of overwriting it. diff --git a/core/tauri/src/endpoints/file_system.rs b/core/tauri/src/endpoints/file_system.rs index 0acea50db38a..ae2cfdfbd069 100644 --- a/core/tauri/src/endpoints/file_system.rs +++ b/core/tauri/src/endpoints/file_system.rs @@ -23,7 +23,10 @@ use serde::{ }; use tauri_macros::{command_enum, module_command_handler, CommandModule}; -use std::fmt::{Debug, Formatter}; +use std::{ + fmt::{Debug, Formatter}, + fs::OpenOptions, +}; use std::{ fs, fs::File, @@ -49,6 +52,8 @@ pub struct FileOperationOptions { /// The base directory of the operation. /// The directory path of the BaseDirectory will be the prefix of the defined file path. pub dir: Option, + /// Whether writeFile should append to or truncate the file + pub append: Option, } /// The API descriptor. @@ -166,6 +171,12 @@ impl Cmd { contents: Vec, options: Option, ) -> super::Result<()> { + let append = options + .as_ref() + .map(|opt| opt.append) + .flatten() + .unwrap_or_default(); + let resolved_path = resolve_path( &context.config, &context.package_info, @@ -173,7 +184,12 @@ impl Cmd { path, options.and_then(|o| o.dir), )?; - File::create(&resolved_path) + + OpenOptions::new() + .append(append) + .write(true) + .create(true) + .open(&resolved_path) .with_context(|| format!("path: {}", resolved_path.display())) .map_err(Into::into) .and_then(|mut f| f.write_all(&contents).map_err(|err| err.into())) @@ -409,6 +425,7 @@ mod tests { fn arbitrary(g: &mut Gen) -> Self { Self { dir: Option::arbitrary(g), + append: Option::arbitrary(g), } } } diff --git a/tooling/api/src/fs.ts b/tooling/api/src/fs.ts index 9d47b8a19a46..93ba653d79a2 100644 --- a/tooling/api/src/fs.ts +++ b/tooling/api/src/fs.ts @@ -110,6 +110,12 @@ export enum BaseDirectory { */ interface FsOptions { dir?: BaseDirectory + /** + * Whether the content should overwrite the content of the file or append to it. + * + * @since 1.5.0 + */ + append?: boolean // note that adding fields here needs a change in the writeBinaryFile check } From 697cab7989991d78484380b78417515985509de2 Mon Sep 17 00:00:00 2001 From: Fabian-Lars Date: Wed, 16 Aug 2023 15:15:27 +0200 Subject: [PATCH 2/5] wording --- core/tauri/src/endpoints/file_system.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/tauri/src/endpoints/file_system.rs b/core/tauri/src/endpoints/file_system.rs index ae2cfdfbd069..773f55845b97 100644 --- a/core/tauri/src/endpoints/file_system.rs +++ b/core/tauri/src/endpoints/file_system.rs @@ -52,7 +52,7 @@ pub struct FileOperationOptions { /// The base directory of the operation. /// The directory path of the BaseDirectory will be the prefix of the defined file path. pub dir: Option, - /// Whether writeFile should append to or truncate the file + /// Whether writeFile should append to a file or truncate it. pub append: Option, } From 4afb7680e19c916a1373b1cafc2148c2262ab2da Mon Sep 17 00:00:00 2001 From: FabianLars Date: Wed, 16 Aug 2023 15:30:50 +0200 Subject: [PATCH 3/5] fmt --- tooling/api/src/fs.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tooling/api/src/fs.ts b/tooling/api/src/fs.ts index 93ba653d79a2..956073e01860 100644 --- a/tooling/api/src/fs.ts +++ b/tooling/api/src/fs.ts @@ -111,10 +111,10 @@ export enum BaseDirectory { interface FsOptions { dir?: BaseDirectory /** - * Whether the content should overwrite the content of the file or append to it. - * - * @since 1.5.0 - */ + * Whether the content should overwrite the content of the file or append to it. + * + * @since 1.5.0 + */ append?: boolean // note that adding fields here needs a change in the writeBinaryFile check } From 3663dc67abc552ee3bcf7be60aca248d86f47025 Mon Sep 17 00:00:00 2001 From: Fabian-Lars Date: Wed, 16 Aug 2023 15:56:35 +0200 Subject: [PATCH 4/5] Update .changes/fs-append-file.md Co-authored-by: Amr Bashir --- .changes/fs-append-file.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changes/fs-append-file.md b/.changes/fs-append-file.md index 8866892028fa..a55e0dcbbeb8 100644 --- a/.changes/fs-append-file.md +++ b/.changes/fs-append-file.md @@ -3,4 +3,4 @@ 'tauri': 'patch:enhance' --- -Add `append` option to `FsOptions`, used in `writeTextFile` and `writeBinaryFile`, to be able to append to existing files instead of overwriting it. +Add `append` option to `FsOptions` in the `fs` JS module, used in `writeTextFile` and `writeBinaryFile`, to be able to append to existing files instead of overwriting it. From 06b50e4056ca86b6b65ddede4cc4883cc5530e2f Mon Sep 17 00:00:00 2001 From: FabianLars Date: Wed, 16 Aug 2023 15:59:13 +0200 Subject: [PATCH 5/5] clippeeeyyyy --- core/tauri/src/endpoints/file_system.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/core/tauri/src/endpoints/file_system.rs b/core/tauri/src/endpoints/file_system.rs index 773f55845b97..48e62f9bc564 100644 --- a/core/tauri/src/endpoints/file_system.rs +++ b/core/tauri/src/endpoints/file_system.rs @@ -173,8 +173,7 @@ impl Cmd { ) -> super::Result<()> { let append = options .as_ref() - .map(|opt| opt.append) - .flatten() + .and_then(|opt| opt.append) .unwrap_or_default(); let resolved_path = resolve_path(