From 2ea879e29b6e84b8714bff354d2cd3fd8c2002fa Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Wed, 12 Feb 2025 17:32:16 -0800 Subject: [PATCH] Add a couple guides --- docs/guides/runtime/delete-directory.md | 37 +++++++++++++++++++++++++ docs/guides/runtime/delete-file.md | 19 +++++++++++++ docs/guides/runtime/heap-snapshot.md | 2 +- 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 docs/guides/runtime/delete-directory.md create mode 100644 docs/guides/runtime/delete-file.md diff --git a/docs/guides/runtime/delete-directory.md b/docs/guides/runtime/delete-directory.md new file mode 100644 index 0000000000..3a8526a4f4 --- /dev/null +++ b/docs/guides/runtime/delete-directory.md @@ -0,0 +1,37 @@ +--- +name: Delete directories +--- + +To recursively delete a directory and all its contents, use `rm` from `node:fs/promises`. This is like running `rm -rf` in JavaScript. + +```ts +import { rm } from "node:fs/promises"; + +// Delete a directory and all its contents +await rm("path/to/directory", { recursive: true, force: true }); +``` + +--- + +These options configure the deletion behavior: + +- `recursive: true` - Delete subdirectories and their contents +- `force: true` - Don't throw errors if the directory doesn't exist + +You can also use it without `force` to ensure the directory exists: + +```ts +try { + await rm("path/to/directory", { recursive: true }); +} catch (error) { + if (error.code === "ENOENT") { + console.log("Directory doesn't exist"); + } else { + throw error; + } +} +``` + +--- + +See [Docs > API > FileSystem](https://bun.sh/docs/api/file-io) for more filesystem operations. diff --git a/docs/guides/runtime/delete-file.md b/docs/guides/runtime/delete-file.md new file mode 100644 index 0000000000..03ccd21472 --- /dev/null +++ b/docs/guides/runtime/delete-file.md @@ -0,0 +1,19 @@ +--- +name: Delete files +--- + +To delete a file, use `Bun.file(path).delete()`. + +```ts +// Delete a file +const file = Bun.file("path/to/file.txt"); +await file.delete(); + +// Now the file doesn't exist +const exists = await file.exists(); +// => false +``` + +--- + +See [Docs > API > FileSystem](https://bun.sh/docs/api/file-io) for more filesystem operations. diff --git a/docs/guides/runtime/heap-snapshot.md b/docs/guides/runtime/heap-snapshot.md index 4eddec772c..8e5849cfb1 100644 --- a/docs/guides/runtime/heap-snapshot.md +++ b/docs/guides/runtime/heap-snapshot.md @@ -1,5 +1,5 @@ --- -name: Track memory usage using V8 heap snapshots +name: Inspect memory usage using V8 heap snapshots --- Bun implements V8's heap snapshot API, which allows you to create snapshots of the heap at runtime. This helps debug memory leaks in your JavaScript/TypeScript application.