diff --git a/bindings/nodejs/index.d.ts b/bindings/nodejs/index.d.ts index f0bde3fb7e7f..76558d2b1c2a 100644 --- a/bindings/nodejs/index.d.ts +++ b/bindings/nodejs/index.d.ts @@ -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 /** * Copy file according to given `from` and `to` path. * diff --git a/bindings/nodejs/src/lib.rs b/bindings/nodejs/src/lib.rs index 4b26909d72e0..f36419b2c5db 100644 --- a/bindings/nodejs/src/lib.rs +++ b/bindings/nodejs/src/lib.rs @@ -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) -> 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