Skip to content

Commit

Permalink
feat(bindings/nodejs): add append support (#2322)
Browse files Browse the repository at this point in the history
Signed-off-by: suyanhanx <[email protected]>
  • Loading branch information
suyanhanx authored May 25, 2023
1 parent 4098e62 commit 9bdd422
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
16 changes: 16 additions & 0 deletions bindings/nodejs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,22 @@ export class Operator {
* ```
*/
writeSync(path: string, content: Buffer | string): void
/**
* Append bytes into path.
*
* ### Notes
*
* - It always appends content to the end of the file.
* - It will create file if the path not exists.
*
* ### Example
* ```javascript
* await op.append("path/to/file", Buffer.from("hello world"));
* // or
* await op.append("path/to/file", "hello world");
* ```
*/
append(path: string, content: Buffer | string): Promise<void>
/**
* Copy file according to given `from` and `to` path.
*
Expand Down
23 changes: 23 additions & 0 deletions bindings/nodejs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,29 @@ impl Operator {
self.0.blocking().write(&path, c).map_err(format_napi_error)
}

/// Append bytes into path.
///
/// ### Notes
///
/// - It always appends content to the end of the file.
/// - It will create file if the path not exists.
///
/// ### Example
/// ```javascript
/// await op.append("path/to/file", Buffer.from("hello world"));
/// // or
/// await op.append("path/to/file", "hello world");
/// ```
#[napi]
pub async fn append(&self, path: String, content: Either<Buffer, String>) -> Result<()> {
let c = match content {
Either::A(buf) => buf.as_ref().to_owned(),
Either::B(s) => s.into_bytes(),
};

self.0.append(&path, c).await.map_err(format_napi_error)
}

/// Copy file according to given `from` and `to` path.
///
/// ### Example
Expand Down

0 comments on commit 9bdd422

Please sign in to comment.