Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): Add append option to writeFile apis #7636

Merged
merged 5 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/fs-append-file.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@tauri-apps/api': 'patch:enhance'
'tauri': 'patch:enhance'
---

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.
20 changes: 18 additions & 2 deletions core/tauri/src/endpoints/file_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<BaseDirectory>,
/// Whether writeFile should append to a file or truncate it.
pub append: Option<bool>,
}

/// The API descriptor.
Expand Down Expand Up @@ -166,14 +171,24 @@ impl Cmd {
contents: Vec<u8>,
options: Option<FileOperationOptions>,
) -> super::Result<()> {
let append = options
.as_ref()
.and_then(|opt| opt.append)
.unwrap_or_default();

let resolved_path = resolve_path(
&context.config,
&context.package_info,
&context.window,
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()))
Expand Down Expand Up @@ -409,6 +424,7 @@ mod tests {
fn arbitrary(g: &mut Gen) -> Self {
Self {
dir: Option::arbitrary(g),
append: Option::arbitrary(g),
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions tooling/api/src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member Author

@FabianLars FabianLars Aug 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't understand what this means

}

Expand Down
Loading