bun-types 1.1.37-canary.20241124T140524 → 1.1.37-canary.20241125T140601

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (272) hide show
  1. package/docs/api/binary-data.md +1028 -0
  2. package/docs/api/cc.md +197 -0
  3. package/docs/api/color.md +262 -0
  4. package/docs/api/console.md +38 -0
  5. package/docs/api/dns.md +113 -0
  6. package/docs/api/fetch.md +308 -0
  7. package/docs/api/ffi.md +536 -0
  8. package/docs/api/file-io.md +358 -0
  9. package/docs/api/file-system-router.md +112 -0
  10. package/docs/api/file.md +19 -0
  11. package/docs/api/glob.md +157 -0
  12. package/docs/api/globals.md +387 -0
  13. package/docs/api/hashing.md +314 -0
  14. package/docs/api/html-rewriter.md +31 -0
  15. package/docs/api/http.md +661 -0
  16. package/docs/api/import-meta.md +69 -0
  17. package/docs/api/node-api.md +16 -0
  18. package/docs/api/semver.md +52 -0
  19. package/docs/api/spawn.md +441 -0
  20. package/docs/api/sqlite.md +692 -0
  21. package/docs/api/streams.md +214 -0
  22. package/docs/api/tcp.md +221 -0
  23. package/docs/api/test.md +1 -0
  24. package/docs/api/transpiler.md +274 -0
  25. package/docs/api/udp.md +125 -0
  26. package/docs/api/utils.md +773 -0
  27. package/docs/api/websockets.md +547 -0
  28. package/docs/api/workers.md +230 -0
  29. package/docs/benchmarks.md +120 -0
  30. package/docs/bun-flavored-toml.md +42 -0
  31. package/docs/bundler/executables.md +291 -0
  32. package/docs/bundler/index.md +1543 -0
  33. package/docs/bundler/intro.md +75 -0
  34. package/docs/bundler/loaders.md +288 -0
  35. package/docs/bundler/macros.md +327 -0
  36. package/docs/bundler/plugins.md +30 -0
  37. package/docs/bundler/vs-esbuild.md +1127 -0
  38. package/docs/cli/add.md +163 -0
  39. package/docs/cli/bun-completions.md +3 -0
  40. package/docs/cli/bun-create.md +254 -0
  41. package/docs/cli/bun-dev.md +21 -0
  42. package/docs/cli/bun-install.md +255 -0
  43. package/docs/cli/bun-upgrade.md +39 -0
  44. package/docs/cli/bunx.md +80 -0
  45. package/docs/cli/filter.md +57 -0
  46. package/docs/cli/init.md +40 -0
  47. package/docs/cli/install.md +205 -0
  48. package/docs/cli/link.md +38 -0
  49. package/docs/cli/outdated.md +61 -0
  50. package/docs/cli/patch-commit.md +9 -0
  51. package/docs/cli/pm.md +150 -0
  52. package/docs/cli/publish.md +107 -0
  53. package/docs/cli/remove.md +5 -0
  54. package/docs/cli/run.md +196 -0
  55. package/docs/cli/test.md +247 -0
  56. package/docs/cli/unlink.md +7 -0
  57. package/docs/cli/update.md +34 -0
  58. package/docs/contributing/upgrading-webkit.md +57 -0
  59. package/docs/ecosystem/elysia.md +24 -0
  60. package/docs/ecosystem/express.md +37 -0
  61. package/docs/ecosystem/hono.md +18 -0
  62. package/docs/ecosystem/react.md +65 -0
  63. package/docs/ecosystem/stric.md +37 -0
  64. package/docs/guides/binary/arraybuffer-to-array.md +27 -0
  65. package/docs/guides/binary/arraybuffer-to-blob.md +24 -0
  66. package/docs/guides/binary/arraybuffer-to-buffer.md +25 -0
  67. package/docs/guides/binary/arraybuffer-to-string.md +15 -0
  68. package/docs/guides/binary/arraybuffer-to-typedarray.md +39 -0
  69. package/docs/guides/binary/blob-to-arraybuffer.md +14 -0
  70. package/docs/guides/binary/blob-to-dataview.md +14 -0
  71. package/docs/guides/binary/blob-to-stream.md +14 -0
  72. package/docs/guides/binary/blob-to-string.md +15 -0
  73. package/docs/guides/binary/blob-to-typedarray.md +14 -0
  74. package/docs/guides/binary/buffer-to-arraybuffer.md +14 -0
  75. package/docs/guides/binary/buffer-to-blob.md +14 -0
  76. package/docs/guides/binary/buffer-to-readablestream.md +41 -0
  77. package/docs/guides/binary/buffer-to-string.md +25 -0
  78. package/docs/guides/binary/buffer-to-typedarray.md +14 -0
  79. package/docs/guides/binary/dataview-to-string.md +15 -0
  80. package/docs/guides/binary/typedarray-to-arraybuffer.md +25 -0
  81. package/docs/guides/binary/typedarray-to-blob.md +16 -0
  82. package/docs/guides/binary/typedarray-to-buffer.md +14 -0
  83. package/docs/guides/binary/typedarray-to-dataview.md +14 -0
  84. package/docs/guides/binary/typedarray-to-readablestream.md +41 -0
  85. package/docs/guides/binary/typedarray-to-string.md +16 -0
  86. package/docs/guides/ecosystem/astro.md +72 -0
  87. package/docs/guides/ecosystem/discordjs.md +77 -0
  88. package/docs/guides/ecosystem/docker.md +140 -0
  89. package/docs/guides/ecosystem/drizzle.md +185 -0
  90. package/docs/guides/ecosystem/edgedb.md +228 -0
  91. package/docs/guides/ecosystem/elysia.md +31 -0
  92. package/docs/guides/ecosystem/express.md +40 -0
  93. package/docs/guides/ecosystem/hono.md +39 -0
  94. package/docs/guides/ecosystem/mongoose.md +87 -0
  95. package/docs/guides/ecosystem/neon-drizzle.md +220 -0
  96. package/docs/guides/ecosystem/neon-serverless-postgres.md +55 -0
  97. package/docs/guides/ecosystem/nextjs.md +38 -0
  98. package/docs/guides/ecosystem/nuxt.md +56 -0
  99. package/docs/guides/ecosystem/pm2.md +57 -0
  100. package/docs/guides/ecosystem/prisma.md +140 -0
  101. package/docs/guides/ecosystem/qwik.md +107 -0
  102. package/docs/guides/ecosystem/react.md +49 -0
  103. package/docs/guides/ecosystem/remix.md +78 -0
  104. package/docs/guides/ecosystem/render.md +79 -0
  105. package/docs/guides/ecosystem/sentry.md +52 -0
  106. package/docs/guides/ecosystem/solidstart.md +58 -0
  107. package/docs/guides/ecosystem/ssr-react.md +51 -0
  108. package/docs/guides/ecosystem/stric.md +55 -0
  109. package/docs/guides/ecosystem/sveltekit.md +125 -0
  110. package/docs/guides/ecosystem/systemd.md +113 -0
  111. package/docs/guides/ecosystem/vite.md +70 -0
  112. package/docs/guides/http/cluster.md +66 -0
  113. package/docs/guides/http/fetch-unix.md +33 -0
  114. package/docs/guides/http/fetch.md +24 -0
  115. package/docs/guides/http/file-uploads.md +94 -0
  116. package/docs/guides/http/hot.md +22 -0
  117. package/docs/guides/http/proxy.md +24 -0
  118. package/docs/guides/http/server.md +46 -0
  119. package/docs/guides/http/simple.md +18 -0
  120. package/docs/guides/http/stream-file.md +48 -0
  121. package/docs/guides/http/stream-iterator.md +47 -0
  122. package/docs/guides/http/stream-node-streams-in-bun.md +20 -0
  123. package/docs/guides/http/tls.md +30 -0
  124. package/docs/guides/install/add-dev.md +26 -0
  125. package/docs/guides/install/add-git.md +36 -0
  126. package/docs/guides/install/add-optional.md +25 -0
  127. package/docs/guides/install/add-peer.md +17 -0
  128. package/docs/guides/install/add-tarball.md +33 -0
  129. package/docs/guides/install/add.md +42 -0
  130. package/docs/guides/install/azure-artifacts.md +73 -0
  131. package/docs/guides/install/cicd.md +41 -0
  132. package/docs/guides/install/custom-registry.md +30 -0
  133. package/docs/guides/install/from-npm-install-to-bun-install.md +214 -0
  134. package/docs/guides/install/git-diff-bun-lockfile.md +38 -0
  135. package/docs/guides/install/jfrog-artifactory.md +28 -0
  136. package/docs/guides/install/npm-alias.md +23 -0
  137. package/docs/guides/install/registry-scope.md +36 -0
  138. package/docs/guides/install/trusted.md +48 -0
  139. package/docs/guides/install/workspaces.md +70 -0
  140. package/docs/guides/install/yarnlock.md +42 -0
  141. package/docs/guides/process/argv.md +57 -0
  142. package/docs/guides/process/ctrl-c.md +16 -0
  143. package/docs/guides/process/ipc.md +66 -0
  144. package/docs/guides/process/nanoseconds.md +13 -0
  145. package/docs/guides/process/os-signals.md +39 -0
  146. package/docs/guides/process/spawn-stderr.md +31 -0
  147. package/docs/guides/process/spawn-stdout.md +26 -0
  148. package/docs/guides/process/spawn.md +41 -0
  149. package/docs/guides/process/stdin.md +54 -0
  150. package/docs/guides/read-file/arraybuffer.md +28 -0
  151. package/docs/guides/read-file/buffer.md +19 -0
  152. package/docs/guides/read-file/exists.md +16 -0
  153. package/docs/guides/read-file/json.md +17 -0
  154. package/docs/guides/read-file/mime.md +20 -0
  155. package/docs/guides/read-file/stream.md +26 -0
  156. package/docs/guides/read-file/string.md +22 -0
  157. package/docs/guides/read-file/uint8array.md +21 -0
  158. package/docs/guides/read-file/watch.md +68 -0
  159. package/docs/guides/runtime/cicd.md +43 -0
  160. package/docs/guides/runtime/define-constant.md +145 -0
  161. package/docs/guides/runtime/import-html.md +15 -0
  162. package/docs/guides/runtime/import-json.md +44 -0
  163. package/docs/guides/runtime/import-toml.md +30 -0
  164. package/docs/guides/runtime/read-env.md +32 -0
  165. package/docs/guides/runtime/set-env.md +37 -0
  166. package/docs/guides/runtime/shell.md +40 -0
  167. package/docs/guides/runtime/timezone.md +35 -0
  168. package/docs/guides/runtime/tsconfig-paths.md +29 -0
  169. package/docs/guides/runtime/typescript.md +47 -0
  170. package/docs/guides/runtime/vscode-debugger.md +47 -0
  171. package/docs/guides/runtime/web-debugger.md +82 -0
  172. package/docs/guides/streams/node-readable-to-arraybuffer.md +11 -0
  173. package/docs/guides/streams/node-readable-to-blob.md +11 -0
  174. package/docs/guides/streams/node-readable-to-json.md +12 -0
  175. package/docs/guides/streams/node-readable-to-string.md +12 -0
  176. package/docs/guides/streams/node-readable-to-uint8array.md +11 -0
  177. package/docs/guides/streams/to-array.md +14 -0
  178. package/docs/guides/streams/to-arraybuffer.md +14 -0
  179. package/docs/guides/streams/to-blob.md +14 -0
  180. package/docs/guides/streams/to-buffer.md +15 -0
  181. package/docs/guides/streams/to-json.md +14 -0
  182. package/docs/guides/streams/to-string.md +14 -0
  183. package/docs/guides/streams/to-typedarray.md +22 -0
  184. package/docs/guides/test/bail.md +22 -0
  185. package/docs/guides/test/coverage-threshold.md +60 -0
  186. package/docs/guides/test/coverage.md +44 -0
  187. package/docs/guides/test/happy-dom.md +68 -0
  188. package/docs/guides/test/migrate-from-jest.md +110 -0
  189. package/docs/guides/test/mock-clock.md +48 -0
  190. package/docs/guides/test/mock-functions.md +68 -0
  191. package/docs/guides/test/rerun-each.md +14 -0
  192. package/docs/guides/test/run-tests.md +111 -0
  193. package/docs/guides/test/skip-tests.md +39 -0
  194. package/docs/guides/test/snapshot.md +99 -0
  195. package/docs/guides/test/spy-on.md +46 -0
  196. package/docs/guides/test/testing-library.md +87 -0
  197. package/docs/guides/test/timeout.md +15 -0
  198. package/docs/guides/test/todo-tests.md +67 -0
  199. package/docs/guides/test/update-snapshots.md +50 -0
  200. package/docs/guides/test/watch-mode.md +19 -0
  201. package/docs/guides/util/base64.md +15 -0
  202. package/docs/guides/util/deep-equals.md +39 -0
  203. package/docs/guides/util/deflate.md +18 -0
  204. package/docs/guides/util/detect-bun.md +23 -0
  205. package/docs/guides/util/entrypoint.md +17 -0
  206. package/docs/guides/util/escape-html.md +22 -0
  207. package/docs/guides/util/file-url-to-path.md +14 -0
  208. package/docs/guides/util/gzip.md +18 -0
  209. package/docs/guides/util/hash-a-password.md +54 -0
  210. package/docs/guides/util/import-meta-dir.md +13 -0
  211. package/docs/guides/util/import-meta-file.md +13 -0
  212. package/docs/guides/util/import-meta-path.md +13 -0
  213. package/docs/guides/util/main.md +32 -0
  214. package/docs/guides/util/path-to-file-url.md +14 -0
  215. package/docs/guides/util/sleep.md +22 -0
  216. package/docs/guides/util/version.md +21 -0
  217. package/docs/guides/util/which-path-to-executable-bin.md +15 -0
  218. package/docs/guides/websocket/compression.md +31 -0
  219. package/docs/guides/websocket/context.md +72 -0
  220. package/docs/guides/websocket/pubsub.md +38 -0
  221. package/docs/guides/websocket/simple.md +33 -0
  222. package/docs/guides/write-file/append.md +52 -0
  223. package/docs/guides/write-file/basic.md +44 -0
  224. package/docs/guides/write-file/blob.md +28 -0
  225. package/docs/guides/write-file/cat.md +17 -0
  226. package/docs/guides/write-file/file-cp.md +16 -0
  227. package/docs/guides/write-file/filesink.md +52 -0
  228. package/docs/guides/write-file/response.md +17 -0
  229. package/docs/guides/write-file/stdout.md +21 -0
  230. package/docs/guides/write-file/stream.md +17 -0
  231. package/docs/guides/write-file/unlink.md +23 -0
  232. package/docs/index.md +77 -0
  233. package/docs/install/cache.md +57 -0
  234. package/docs/install/index.md +202 -0
  235. package/docs/install/lifecycle.md +46 -0
  236. package/docs/install/lockfile.md +90 -0
  237. package/docs/install/npmrc.md +75 -0
  238. package/docs/install/overrides.md +73 -0
  239. package/docs/install/patch.md +57 -0
  240. package/docs/install/registries.md +30 -0
  241. package/docs/install/workspaces.md +70 -0
  242. package/docs/installation.md +289 -0
  243. package/docs/project/benchmarking.md +203 -0
  244. package/docs/project/building-windows.md +162 -0
  245. package/docs/project/internals/build-process-for-ci.md +75 -0
  246. package/docs/project/roadmap.md +87 -0
  247. package/docs/quickstart.md +144 -0
  248. package/docs/runtime/autoimport.md +94 -0
  249. package/docs/runtime/bun-apis.md +129 -0
  250. package/docs/runtime/bunfig.md +532 -0
  251. package/docs/runtime/debugger.md +325 -0
  252. package/docs/runtime/env.md +214 -0
  253. package/docs/runtime/hot.md +139 -0
  254. package/docs/runtime/index.md +309 -0
  255. package/docs/runtime/jsx.md +326 -0
  256. package/docs/runtime/loaders.md +127 -0
  257. package/docs/runtime/modules.md +298 -0
  258. package/docs/runtime/nodejs-apis.md +456 -0
  259. package/docs/runtime/plugins.md +605 -0
  260. package/docs/runtime/shell.md +537 -0
  261. package/docs/runtime/typescript.md +60 -0
  262. package/docs/runtime/web-apis.md +128 -0
  263. package/docs/test/coverage.md +91 -0
  264. package/docs/test/dom.md +75 -0
  265. package/docs/test/hot.md +15 -0
  266. package/docs/test/lifecycle.md +81 -0
  267. package/docs/test/mocks.md +236 -0
  268. package/docs/test/snapshots.md +15 -0
  269. package/docs/test/time.md +106 -0
  270. package/docs/test/writing.md +547 -0
  271. package/docs/typescript.md +51 -0
  272. package/package.json +5 -4
@@ -0,0 +1,537 @@
1
+ Bun Shell makes shell scripting with JavaScript & TypeScript fun. It's a cross-platform bash-like shell with seamless JavaScript interop.
2
+
3
+ Quickstart:
4
+
5
+ ```js
6
+ import { $ } from "bun";
7
+
8
+ const response = await fetch("https://example.com");
9
+
10
+ // Use Response as stdin.
11
+ await $`cat < ${response} | wc -c`; // 1256
12
+ ```
13
+
14
+ ## Features:
15
+
16
+ - **Cross-platform**: works on Windows, Linux & macOS. Instead of `rimraf` or `cross-env`', you can use Bun Shell without installing extra dependencies. Common shell commands like `ls`, `cd`, `rm` are implemented natively.
17
+ - **Familiar**: Bun Shell is a bash-like shell, supporting redirection, pipes, environment variables and more.
18
+ - **Globs**: Glob patterns are supported natively, including `**`, `*`, `{expansion}`, and more.
19
+ - **Template literals**: Template literals are used to execute shell commands. This allows for easy interpolation of variables and expressions.
20
+ - **Safety**: Bun Shell escapes all strings by default, preventing shell injection attacks.
21
+ - **JavaScript interop**: Use `Response`, `ArrayBuffer`, `Blob`, `Bun.file(path)` and other JavaScript objects as stdin, stdout, and stderr.
22
+ - **Shell scripting**: Bun Shell can be used to run shell scripts (`.bun.sh` files).
23
+ - **Custom interpreter**: Bun Shell is written in Zig, along with it's lexer, parser, and interpreter. Bun Shell is a small programming language.
24
+
25
+ ## Getting started
26
+
27
+ The simplest shell command is `echo`. To run it, use the `$` template literal tag:
28
+
29
+ ```js
30
+ import { $ } from "bun";
31
+
32
+ await $`echo "Hello World!"`; // Hello World!
33
+ ```
34
+
35
+ By default, shell commands print to stdout. To quiet the output, call `.quiet()`:
36
+
37
+ ```js
38
+ import { $ } from "bun";
39
+
40
+ await $`echo "Hello World!"`.quiet(); // No output
41
+ ```
42
+
43
+ What if you want to access the output of the command as text? Use `.text()`:
44
+
45
+ ```js
46
+ import { $ } from "bun";
47
+
48
+ // .text() automatically calls .quiet() for you
49
+ const welcome = await $`echo "Hello World!"`.text();
50
+
51
+ console.log(welcome); // Hello World!\n
52
+ ```
53
+
54
+ By default, `await`ing will return stdout and stderr as `Buffer`s.
55
+
56
+ ```js
57
+ import { $ } from "bun";
58
+
59
+ const { stdout, stderr } = await $`echo "Hello World!"`.quiet();
60
+
61
+ console.log(stdout); // Buffer(6) [ 72, 101, 108, 108, 111, 32 ]
62
+ console.log(stderr); // Buffer(0) []
63
+ ```
64
+
65
+ ## Error handling
66
+
67
+ By default, non-zero exit codes will throw an error. This `ShellError` contains information about the command run.
68
+
69
+ ```js
70
+ import { $ } from "bun";
71
+
72
+ try {
73
+ const output = await $`something-that-may-fail`.text();
74
+ console.log(output);
75
+ } catch (err) {
76
+ console.log(`Failed with code ${err.exitCode}`);
77
+ console.log(err.stdout.toString());
78
+ console.log(err.stderr.toString());
79
+ }
80
+ ```
81
+
82
+ Throwing can be disabled with `.nothrow()`. The result's `exitCode` will need to be checked manually.
83
+
84
+ ```js
85
+ import { $ } from "bun";
86
+
87
+ const { stdout, stderr, exitCode } = await $`something-that-may-fail`
88
+ .nothrow()
89
+ .quiet();
90
+
91
+ if (exitCode !== 0) {
92
+ console.log(`Non-zero exit code ${exitCode}`);
93
+ }
94
+
95
+ console.log(stdout);
96
+ console.log(stderr);
97
+ ```
98
+
99
+ The default handling of non-zero exit codes can be configured by calling `.nothrow()` or `.throws(boolean)` on the `$` function itself.
100
+
101
+ ```js
102
+ import { $ } from "bun";
103
+ // shell promises will not throw, meaning you will have to
104
+ // check for `exitCode` manually on every shell command.
105
+ $.nothrow(); // equivilent to $.throws(false)
106
+
107
+ // default behavior, non-zero exit codes will throw an error
108
+ $.throws(true);
109
+
110
+ // alias for $.nothrow()
111
+ $.throws(false);
112
+
113
+ await $`something-that-may-fail`; // No exception thrown
114
+ ```
115
+
116
+ ## Redirection
117
+
118
+ A command's _input_ or _output_ may be _redirected_ using the typical Bash operators:
119
+
120
+ - `<` redirect stdin
121
+ - `>` or `1>` redirect stdout
122
+ - `2>` redirect stderr
123
+ - `&>` redirect both stdout and stderr
124
+ - `>>` or `1>>` redirect stdout, _appending_ to the destination, instead of overwriting
125
+ - `2>>` redirect stderr, _appending_ to the destination, instead of overwriting
126
+ - `&>>` redirect both stdout and stderr, _appending_ to the destination, instead of overwriting
127
+ - `1>&2` redirect stdout to stderr (all writes to stdout will instead be in stderr)
128
+ - `2>&1` redirect stderr to stdout (all writes to stderr will instead be in stdout)
129
+
130
+ Bun Shell also supports redirecting from and to JavaScript objects.
131
+
132
+ ### Example: Redirect output to JavaScript objects (`>`)
133
+
134
+ To redirect stdout to a JavaScript object, use the `>` operator:
135
+
136
+ ```js
137
+ import { $ } from "bun";
138
+
139
+ const buffer = Buffer.alloc(100);
140
+ await $`echo "Hello World!" > ${buffer}`;
141
+
142
+ console.log(buffer.toString()); // Hello World!\n
143
+ ```
144
+
145
+ The following JavaScript objects are supported for redirection to:
146
+
147
+ - `Buffer`, `Uint8Array`, `Uint16Array`, `Uint32Array`, `Int8Array`, `Int16Array`, `Int32Array`, `Float32Array`, `Float64Array`, `ArrayBuffer`, `SharedArrayBuffer` (writes to the underlying buffer)
148
+ - `Bun.file(path)`, `Bun.file(fd)` (writes to the file)
149
+
150
+ ### Example: Redirect input from JavaScript objects (`<`)
151
+
152
+ To redirect the output from JavaScript objects to stdin, use the `<` operator:
153
+
154
+ ```js
155
+ import { $ } from "bun";
156
+
157
+ const response = new Response("hello i am a response body");
158
+
159
+ const result = await $`cat < ${response}`.text();
160
+
161
+ console.log(result); // hello i am a response body
162
+ ```
163
+
164
+ The following JavaScript objects are supported for redirection from:
165
+
166
+ - `Buffer`, `Uint8Array`, `Uint16Array`, `Uint32Array`, `Int8Array`, `Int16Array`, `Int32Array`, `Float32Array`, `Float64Array`, `ArrayBuffer`, `SharedArrayBuffer` (reads from the underlying buffer)
167
+ - `Bun.file(path)`, `Bun.file(fd)` (reads from the file)
168
+ - `Response` (reads from the body)
169
+
170
+ ### Example: Redirect stdin -> file
171
+
172
+ ```js
173
+ import { $ } from "bun";
174
+
175
+ await $`cat < myfile.txt`;
176
+ ```
177
+
178
+ ### Example: Redirect stdout -> file
179
+
180
+ ```js
181
+ import { $ } from "bun";
182
+
183
+ await $`echo bun! > greeting.txt`;
184
+ ```
185
+
186
+ ### Example: Redirect stderr -> file
187
+
188
+ ```js
189
+ import { $ } from "bun";
190
+
191
+ await $`bun run index.ts 2> errors.txt`;
192
+ ```
193
+
194
+ ### Example: Redirect stderr -> stdout
195
+
196
+ ```js
197
+ import { $ } from "bun";
198
+
199
+ // redirects stderr to stdout, so all output
200
+ // will be available on stdout
201
+ await $`bun run ./index.ts 2>&1`;
202
+ ```
203
+
204
+ ### Example: Redirect stdout -> stderr
205
+
206
+ ```js
207
+ import { $ } from "bun";
208
+
209
+ // redirects stdout to stderr, so all output
210
+ // will be available on stderr
211
+ await $`bun run ./index.ts 1>&2`;
212
+ ```
213
+
214
+ ## Piping (`|`)
215
+
216
+ Like in bash, you can pipe the output of one command to another:
217
+
218
+ ```js
219
+ import { $ } from "bun";
220
+
221
+ const result = await $`echo "Hello World!" | wc -w`.text();
222
+
223
+ console.log(result); // 2\n
224
+ ```
225
+
226
+ You can also pipe with JavaScript objects:
227
+
228
+ ```js
229
+ import { $ } from "bun";
230
+
231
+ const response = new Response("hello i am a response body");
232
+
233
+ const result = await $`cat < ${response} | wc -w`.text();
234
+
235
+ console.log(result); // 6\n
236
+ ```
237
+
238
+ ## Command substitution (`$(...)`)
239
+
240
+ Command substitution allows you to substitute the output of another script into the current script:
241
+
242
+ ```js
243
+ import { $ } from "bun";
244
+
245
+ // Prints out the hash of the current commit
246
+ await $`echo Hash of current commit: $(git rev-parse HEAD)`;
247
+ ```
248
+
249
+ This is a textual insertion of the command's output and can be used to, for example, declare a shell variable:
250
+
251
+ ```js
252
+ import { $ } from "bun";
253
+
254
+ await $`
255
+ REV=$(git rev-parse HEAD)
256
+ docker built -t myapp:$REV
257
+ echo Done building docker image "myapp:$REV"
258
+ `;
259
+ ```
260
+
261
+ {% callout %}
262
+
263
+ **NOTE**: Because Bun internally uses the special [`raw`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#raw_strings) property on the input template literal, using the backtick syntax for command substitution won't work:
264
+
265
+ ```js
266
+ import { $ } from "bun";
267
+
268
+ await $`echo \`echo hi\``;
269
+ ```
270
+
271
+ Instead of printing:
272
+
273
+ ```
274
+ hi
275
+ ```
276
+
277
+ The above will print out:
278
+
279
+ ```
280
+ echo hi
281
+ ```
282
+
283
+ We instead recommend sticking to the `$(...)` syntax.
284
+
285
+ {% /callout %}
286
+
287
+ ## Environment variables
288
+
289
+ Environment variables can be set like in bash:
290
+
291
+ ```js
292
+ import { $ } from "bun";
293
+
294
+ await $`FOO=foo bun -e 'console.log(process.env.FOO)'`; // foo\n
295
+ ```
296
+
297
+ You can use string interpolation to set environment variables:
298
+
299
+ ```js
300
+ import { $ } from "bun";
301
+
302
+ const foo = "bar123";
303
+
304
+ await $`FOO=${foo + "456"} bun -e 'console.log(process.env.FOO)'`; // bar123456\n
305
+ ```
306
+
307
+ Input is escaped by default, preventing shell injection attacks:
308
+
309
+ ```js
310
+ import { $ } from "bun";
311
+
312
+ const foo = "bar123; rm -rf /tmp";
313
+
314
+ await $`FOO=${foo} bun -e 'console.log(process.env.FOO)'`; // bar123; rm -rf /tmp\n
315
+ ```
316
+
317
+ ### Changing the environment variables
318
+
319
+ By default, `process.env` is used as the environment variables for all commands.
320
+
321
+ You can change the environment variables for a single command by calling `.env()`:
322
+
323
+ ```js
324
+ import { $ } from "bun";
325
+
326
+ await $`echo $FOO`.env({ ...process.env, FOO: "bar" }); // bar
327
+ ```
328
+
329
+ You can change the default environment variables for all commands by calling `$.env`:
330
+
331
+ ```js
332
+ import { $ } from "bun";
333
+
334
+ $.env({ FOO: "bar" });
335
+
336
+ // the globally-set $FOO
337
+ await $`echo $FOO`; // bar
338
+
339
+ // the locally-set $FOO
340
+ await $`echo $FOO`.env({ FOO: "baz" }); // baz
341
+ ```
342
+
343
+ You can reset the environment variables to the default by calling `$.env()` with no arguments:
344
+
345
+ ```js
346
+ import { $ } from "bun";
347
+
348
+ $.env({ FOO: "bar" });
349
+
350
+ // the globally-set $FOO
351
+ await $`echo $FOO`; // bar
352
+
353
+ // the locally-set $FOO
354
+ await $`echo $FOO`.env(undefined); // ""
355
+ ```
356
+
357
+ ### Changing the working directory
358
+
359
+ You can change the working directory of a command by passing a string to `.cwd()`:
360
+
361
+ ```js
362
+ import { $ } from "bun";
363
+
364
+ await $`pwd`.cwd("/tmp"); // /tmp
365
+ ```
366
+
367
+ You can change the default working directory for all commands by calling `$.cwd`:
368
+
369
+ ```js
370
+ import { $ } from "bun";
371
+
372
+ $.cwd("/tmp");
373
+
374
+ // the globally-set working directory
375
+ await $`pwd`; // /tmp
376
+
377
+ // the locally-set working directory
378
+ await $`pwd`.cwd("/"); // /
379
+ ```
380
+
381
+ ## Reading output
382
+
383
+ To read the output of a command as a string, use `.text()`:
384
+
385
+ ```js
386
+ import { $ } from "bun";
387
+
388
+ const result = await $`echo "Hello World!"`.text();
389
+
390
+ console.log(result); // Hello World!\n
391
+ ```
392
+
393
+ ### Reading output as JSON
394
+
395
+ To read the output of a command as JSON, use `.json()`:
396
+
397
+ ```js
398
+ import { $ } from "bun";
399
+
400
+ const result = await $`echo '{"foo": "bar"}'`.json();
401
+
402
+ console.log(result); // { foo: "bar" }
403
+ ```
404
+
405
+ ### Reading output line-by-line
406
+
407
+ To read the output of a command line-by-line, use `.lines()`:
408
+
409
+ ```js
410
+ import { $ } from "bun";
411
+
412
+ for await (let line of $`echo "Hello World!"`.lines()) {
413
+ console.log(line); // Hello World!
414
+ }
415
+ ```
416
+
417
+ You can also use `.lines()` on a completed command:
418
+
419
+ ```js
420
+ import { $ } from "bun";
421
+
422
+ const search = "bun";
423
+
424
+ for await (let line of $`cat list.txt | grep ${search}`.lines()) {
425
+ console.log(line);
426
+ }
427
+ ```
428
+
429
+ ### Reading output as a Blob
430
+
431
+ To read the output of a command as a Blob, use `.blob()`:
432
+
433
+ ```js
434
+ import { $ } from "bun";
435
+
436
+ const result = await $`echo "Hello World!"`.blob();
437
+
438
+ console.log(result); // Blob(13) { size: 13, type: "text/plain" }
439
+ ```
440
+
441
+ ## Builtin Commands
442
+
443
+ For cross-platform compatibility, Bun Shell implements a set of builtin commands, in addition to reading commands from the PATH environment variable.
444
+
445
+ - `cd`: change the working directory
446
+ - `ls`: list files in a directory
447
+ - `rm`: remove files and directories
448
+ - `echo`: print text
449
+ - `pwd`: print the working directory
450
+ - `bun`: run bun in bun
451
+ - `cat`
452
+ - `touch`
453
+ - `mkdir`
454
+ - `which`
455
+ - `mv`
456
+ - `exit`
457
+ - `true`
458
+ - `false`
459
+ - `yes`
460
+ - `seq`
461
+ - `dirname`
462
+ - `basename`
463
+
464
+ **Partially** implemented:
465
+
466
+ - `mv`: move files and directories (missing cross-device support)
467
+
468
+ **Not** implemented yet, but planned:
469
+
470
+ - See [Issue #9716](https://github.com/oven-sh/bun/issues/9716) for the full list.
471
+
472
+ ## Utilities
473
+
474
+ Bun Shell also implements a set of utilities for working with shells.
475
+
476
+ ### `$.braces` (brace expansion)
477
+
478
+ This function implements simple [brace expansion](https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html) for shell commands:
479
+
480
+ ```js
481
+ import { $ } from "bun";
482
+
483
+ await $.braces(`echo {1,2,3}`);
484
+ // => ["echo 1", "echo 2", "echo 3"]
485
+ ```
486
+
487
+ ### `$.escape` (escape strings)
488
+
489
+ Exposes Bun Shell's escaping logic as a function:
490
+
491
+ ```js
492
+ import { $ } from "bun";
493
+
494
+ console.log($.escape('$(foo) `bar` "baz"'));
495
+ // => \$(foo) \`bar\` \"baz\"
496
+ ```
497
+
498
+ If you do not want your string to be escaped, wrap it in a `{ raw: 'str' }` object:
499
+
500
+ ```js
501
+ import { $ } from "bun";
502
+
503
+ await $`echo ${{ raw: '$(foo) `bar` "baz"' }}`;
504
+ // => bun: command not found: foo
505
+ // => bun: command not found: bar
506
+ // => baz
507
+ ```
508
+
509
+ ## .sh file loader
510
+
511
+ For simple shell scripts, instead of `/bin/sh`, you can use Bun Shell to run shell scripts.
512
+
513
+ To do so, just run the script with `bun` on a file with the `.sh` extension.
514
+
515
+ ```sh#script.sh
516
+ echo "Hello World! pwd=$(pwd)"
517
+ ```
518
+
519
+ ```sh
520
+ $ bun ./script.sh
521
+ Hello World! pwd=/home/demo
522
+ ```
523
+
524
+ Scripts with Bun Shell are cross platform, which means they work on Windows:
525
+
526
+ ```powershell
527
+ > bun .\script.sh
528
+ Hello World! pwd=C:\Users\Demo
529
+ ```
530
+
531
+ ## Implementation notes
532
+
533
+ Bun Shell is a small programming language in Bun that is implemented in Zig. It includes a handwritten lexer, parser, and interpreter. Unlike bash, zsh, and other shells, Bun Shell runs operations concurrently.
534
+
535
+ ## Credits
536
+
537
+ Large parts of this API were inspired by [zx](https://github.com/google/zx), [dax](https://github.com/dsherret/dax), and [bnx](https://github.com/wobsoriano/bnx). Thank you to the authors of those projects.
@@ -0,0 +1,60 @@
1
+ Bun treats TypeScript as a first-class citizen.
2
+
3
+ {% callout %}
4
+
5
+ **Note** — To add type declarations for Bun APIs like the `Bun` global, follow the instructions at [Intro > TypeScript](https://bun.sh/docs/typescript). This page describes how the Bun runtime runs TypeScript code.
6
+
7
+ {% /callout %}
8
+
9
+ ## Running `.ts` files
10
+
11
+ Bun can directly execute `.ts` and `.tsx` files just like vanilla JavaScript, with no extra configuration. If you import a `.ts` or `.tsx` file (or an `npm` module that exports these files), Bun internally transpiles it into JavaScript then executes the file.
12
+
13
+ **Note** — Similar to other build tools, Bun does not typecheck the files. Use [`tsc`](https://www.typescriptlang.org/docs/handbook/compiler-options.html) (the official TypeScript CLI) if you're looking to catch static type errors.
14
+
15
+ {% callout %}
16
+
17
+ **Is transpiling still necessary?** — Because Bun can directly execute TypeScript, you may not need to transpile your TypeScript to run in production. Bun internally transpiles every file it executes (both `.js` and `.ts`), so the additional overhead of directly executing your `.ts/.tsx` source files is negligible.
18
+
19
+ That said, if you are using Bun as a development tool but still targeting Node.js or browsers in production, you'll still need to transpile.
20
+
21
+ {% /callout %}
22
+
23
+ ## Path mapping
24
+
25
+ When resolving modules, Bun's runtime respects path mappings defined in [`compilerOptions.paths`](https://www.typescriptlang.org/tsconfig#paths) in your `tsconfig.json`. No other runtime does this.
26
+
27
+ Consider the following `tsconfig.json`.
28
+
29
+ ```json
30
+ {
31
+ "compilerOptions": {
32
+ "baseUrl": "./src",
33
+ "paths": {
34
+ "data": ["./data.ts"]
35
+ }
36
+ }
37
+ }
38
+ ```
39
+
40
+ Bun will use `baseUrl` to resolve module paths.
41
+
42
+ ```ts
43
+ // resolves to ./src/components/Button.tsx
44
+ import { Button } from "components/Button.tsx";
45
+ ```
46
+
47
+ Bun will also correctly resolve imports from `"data"`.
48
+
49
+ {% codetabs %}
50
+
51
+ ```ts#index.ts
52
+ import { foo } from "data";
53
+ console.log(foo); // => "Hello world!"
54
+ ```
55
+
56
+ ```ts#data.ts
57
+ export const foo = "Hello world!"
58
+ ```
59
+
60
+ {% /codetabs %}