Document openInEditor

This commit is contained in:
Colin McDonnell
2023-03-07 21:05:37 -08:00
parent 24e90726fd
commit 95b59ea0ef
3 changed files with 32 additions and 2 deletions

View File

@@ -134,11 +134,13 @@ Bun.serve({
Thus far, the examples on this page have used the explicit `Bun.serve` API. Bun also supports an alternate syntax.
```ts#server.ts
import {type Serve} from "bun";
export default {
fetch(req) {
return new Response(`Bun!`);
},
};
} satisfies Serve;
```
Instead of passing the server options into `Bun.serve`, export it. This file can be executed as-is; when Bun runs a file with a `default` export containing a `fetch` handler, it passes it into `Bun.serve` under the hood.

View File

@@ -118,3 +118,29 @@ test("peek.status", () => {
expect(peek.status(rejected)).toBe("rejected");
});
```
## `Bun.openInEditor`
Open a file in your default editor. Bun auto-detects your editor via the `$VISUAL` or `$EDITOR` environment variables.
```ts
const currentFile = import.meta.url;
Bun.openInEditor(currentFile);
```
You can override this via the `debug.editor` setting in your [`bunfig.toml`](/docs/project/configuration)
```toml-diff#bunfig.toml
+ [debug]
+ editor = "code"
```
Or specify an editor with the `editor` param. You can also specify a line and column number.
```ts
Bun.openInEditor(import.meta.url, {
editor: "vscode", // or "subl"
line: 10,
column: 5,
})
```

View File

@@ -39,6 +39,8 @@ Traditional file watchers like `nodemon` restart the entire process, so HTTP ser
Bun provides the following simplified API for implementing HTTP servers. Refer to [API > HTTP](/docs/api/http) for full details.
```ts#server.ts
import {type Serve} from "bun";
globalThis.count = globalThis.count ?? 0;
globalThis.count++;
@@ -47,7 +49,7 @@ export default {
return new Response(`Reloaded ${globalThis.count} times`);
},
port: 3000,
};
} satisfies Serve;
```
The file above is simply exporting an object with a `fetch` handler defined. When this file is executed, Bun interprets this as an HTTP server and passes the exported object into `Bun.serve`.