-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbun-cli.mdc
More file actions
304 lines (225 loc) · 7.24 KB
/
Copy pathbun-cli.mdc
File metadata and controls
304 lines (225 loc) · 7.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
---
description: Bun CLI Development Standards - Leveraging Bun's native APIs for optimal performance
globs: "packages/cli/**/*.{ts,tsx}"
alwaysApply: true
---
# Bun CLI Development Standards
This project uses **Bun** as both runtime and build tool. Always prefer Bun-native APIs over Node.js equivalents for better performance and cleaner code.
## Bun API Reference
Full documentation: https://bun.sh/docs
## File Operations
**Use Bun's file APIs instead of `node:fs` for reading/writing:**
```typescript
// Reading files
const file = Bun.file(filepath);
if (await file.exists()) {
const text = await file.text(); // Read as string
const json = await file.json(); // Parse JSON directly
const buffer = await file.bytes(); // Read as Uint8Array
}
// Writing files
await Bun.write(filepath, content); // String or Buffer
await Bun.write(filepath, Bun.file(other)); // Copy file
// File metadata
const stats = await Bun.file(filepath).stat();
```
**Exception:** Use `node:fs` for directory creation with specific permissions:
```typescript
import { mkdirSync } from "node:fs";
mkdirSync(CONFIG_DIR, { recursive: true, mode: 0o700 });
```
Docs: https://bun.sh/docs/api/file-io
## Process Spawning
**Use `Bun.spawn()` and `Bun.which()` instead of `node:child_process`:**
```typescript
// Find executable
const git = Bun.which("git"); // Returns path or null
// Spawn process
const proc = Bun.spawn(["git", "status"], {
cwd: "/path/to/repo",
stdout: "pipe",
stderr: "pipe",
env: { ...process.env, CUSTOM: "value" },
});
// Read output
const stdout = await Bun.readableStreamToText(proc.stdout);
const exitCode = await proc.exited;
```
Docs: https://bun.sh/docs/api/spawn
## Shell Commands (Scripting)
**For build scripts and automation, use `Bun.$`:**
```typescript
import { $ } from "bun";
// Tagged template shell commands
await $`git add . && git commit -m "message"`;
const sha = await $`git rev-parse HEAD`.text();
// With error handling
const result = await $`npm test`.nothrow();
if (result.exitCode !== 0) {
console.error(result.stderr.toString());
}
```
Docs: https://bun.sh/docs/runtime/shell
## Glob Pattern Matching
**Use `Bun.Glob` for file discovery:**
```typescript
const glob = new Bun.Glob("**/*.{ts,js}");
for await (const file of glob.scan({ cwd: "./src", onlyFiles: true })) {
console.log(file);
}
// Check if path matches pattern
if (glob.match("src/index.ts")) {
// ...
}
```
Docs: https://bun.sh/docs/api/glob
## HTTP Server (if needed)
**Use `Bun.serve()` for local servers (OAuth callbacks, etc.):**
```typescript
const server = Bun.serve({
port: 0, // Auto-assign port
fetch(req) {
const url = new URL(req.url);
if (url.pathname === "/callback") {
return new Response("Success!");
}
return new Response("Not found", { status: 404 });
},
});
console.log(`Server running on port ${server.port}`);
server.stop(); // When done
```
Docs: https://bun.sh/docs/api/http
## Building Binaries
**Use `Bun.build()` with `compile` option for standalone executables:**
```typescript
await Bun.build({
entrypoints: ["./src/bin.ts"],
compile: {
target: "bun-darwin-arm64", // or linux-x64, windows-x64, etc.
outfile: "dist/sentry",
},
define: {
CLI_VERSION: JSON.stringify(version),
// Inject secrets at build time for npm distribution
SENTRY_CLIENT_ID_BUILD: JSON.stringify(process.env.SENTRY_CLIENT_ID ?? ""),
},
sourcemap: "external",
});
```
**Build-time secrets pattern:**
For values that need to be baked into the binary (like OAuth client IDs):
1. Read from env in build script: `process.env.SENTRY_CLIENT_ID`
2. Inject via `define`: `SENTRY_CLIENT_ID_BUILD: JSON.stringify(value)`
3. Use in code with runtime override support:
```typescript
// Declare the build-time constant
declare const SENTRY_CLIENT_ID_BUILD: string | undefined;
// Allow runtime override (for self-hosted), fall back to build-time value
const CLIENT_ID =
process.env.SENTRY_CLIENT_ID ??
(typeof SENTRY_CLIENT_ID_BUILD !== "undefined" ? SENTRY_CLIENT_ID_BUILD : "");
```
**Build command with secrets:**
```bash
SENTRY_CLIENT_ID=xxx bun run build:all
```
Supported targets:
- `bun-darwin-arm64`, `bun-darwin-x64`, `bun-darwin-x64-baseline`
- `bun-linux-x64`, `bun-linux-arm64`, `bun-linux-x64-baseline`
- `bun-linux-x64-musl`, `bun-linux-arm64-musl`
- `bun-windows-x64`, `bun-windows-x64-baseline`
Baseline variants target older CPUs without AVX2 support.
Docs: https://bun.sh/docs/bundler/executables
## Utilities
```typescript
// Sleep
await Bun.sleep(1000); // 1 second
// Fast hashing
const hash = Bun.hash.xxHash32(data);
// TOML parsing
const config = Bun.TOML.parse(content);
// Module resolution
const path = await Bun.resolve("package/file", import.meta.dir);
// Environment
const value = Bun.env.MY_VAR;
// Direct I/O
Bun.stderr.write("Error message\n");
const input = await Bun.stdin.text();
```
## Testing
**Use `bun:test` for all tests:**
```typescript
import { describe, expect, test, mock, beforeEach } from "bun:test";
describe("feature", () => {
test("should work", async () => {
expect(await someFunction()).toBe(expected);
});
});
// Mocking
mock.module("./some-module", () => ({
default: () => "mocked",
}));
```
Run tests: `bun test`
Docs: https://bun.sh/docs/cli/test
## What NOT to Use
Avoid these Node.js APIs when Bun equivalents exist:
| Avoid | Use Instead |
|-------|-------------|
| `fs.readFileSync()` | `await Bun.file(path).text()` |
| `fs.writeFileSync()` | `await Bun.write(path, content)` |
| `fs.existsSync()` | `await Bun.file(path).exists()` |
| `child_process.spawn()` | `Bun.spawn()` |
| `child_process.exec()` | `Bun.$\`command\`` |
| `which` package | `Bun.which()` |
| `glob` package | `new Bun.Glob()` |
| `fast-glob` | `new Bun.Glob()` |
**Keep using `node:fs` for:**
- Directory creation with permissions (`mkdirSync` with `mode`)
- Operations that need sync behavior in specific contexts
## CLI Framework
This project uses **Stricli** (`@stricli/core`) for CLI command definitions. Key patterns:
```typescript
import { buildCommand, buildRouteMap } from "@stricli/core";
export const myCommand = buildCommand({
docs: {
brief: "Short description",
fullDescription: "Detailed description with examples",
},
parameters: {
flags: {
json: { kind: "boolean", brief: "Output as JSON", default: false },
limit: { kind: "parsed", parse: Number, brief: "Max items", default: 10 },
},
},
async func(this: SentryContext, flags) {
// Implementation - all config functions are async, use await
},
});
```
## Validation
Use **Zod** for runtime validation of configs and API responses:
```typescript
import { z } from "zod";
const ConfigSchema = z.object({
token: z.string(),
org: z.string().optional(),
});
type Config = z.infer<typeof ConfigSchema>;
// Validates and throws if invalid
const config = ConfigSchema.parse(rawData);
// Returns { success: boolean, data?, error? }
const result = ConfigSchema.safeParse(rawData);
```
Docs: https://zod.dev
## Async Patterns
All config functions in this project are async. Always await them:
```typescript
// Config operations
const token = await getAuthToken();
const isAuth = await isAuthenticated();
const org = await getDefaultOrganization();
await setAuthToken(token, expiresIn);
await clearAuth();
```