shell.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. export type ShellFunction = (input: Uint8Array) => Uint8Array
  2. export type ShellExpression =
  3. | { toString(): string }
  4. | Array<ShellExpression>
  5. | string
  6. | { raw: string }
  7. | ReadableStream
  8. export interface BunShell {
  9. (strings: TemplateStringsArray, ...expressions: ShellExpression[]): BunShellPromise
  10. /**
  11. * Perform bash-like brace expansion on the given pattern.
  12. * @param pattern - Brace pattern to expand
  13. */
  14. braces(pattern: string): string[]
  15. /**
  16. * Escape strings for input into shell commands.
  17. */
  18. escape(input: string): string
  19. /**
  20. * Change the default environment variables for shells created by this instance.
  21. */
  22. env(newEnv?: Record<string, string | undefined>): BunShell
  23. /**
  24. * Default working directory to use for shells created by this instance.
  25. */
  26. cwd(newCwd?: string): BunShell
  27. /**
  28. * Configure the shell to not throw an exception on non-zero exit codes.
  29. */
  30. nothrow(): BunShell
  31. /**
  32. * Configure whether or not the shell should throw an exception on non-zero exit codes.
  33. */
  34. throws(shouldThrow: boolean): BunShell
  35. }
  36. export interface BunShellPromise extends Promise<BunShellOutput> {
  37. readonly stdin: WritableStream
  38. /**
  39. * Change the current working directory of the shell.
  40. */
  41. cwd(newCwd: string): this
  42. /**
  43. * Set environment variables for the shell.
  44. */
  45. env(newEnv: Record<string, string> | undefined): this
  46. /**
  47. * By default, the shell will write to the current process's stdout and stderr, as well as buffering that output.
  48. * This configures the shell to only buffer the output.
  49. */
  50. quiet(): this
  51. /**
  52. * Read from stdout as a string, line by line
  53. * Automatically calls quiet() to disable echoing to stdout.
  54. */
  55. lines(): AsyncIterable<string>
  56. /**
  57. * Read from stdout as a string.
  58. * Automatically calls quiet() to disable echoing to stdout.
  59. */
  60. text(encoding?: BufferEncoding): Promise<string>
  61. /**
  62. * Read from stdout as a JSON object
  63. * Automatically calls quiet()
  64. */
  65. json(): Promise<any>
  66. /**
  67. * Read from stdout as an ArrayBuffer
  68. * Automatically calls quiet()
  69. */
  70. arrayBuffer(): Promise<ArrayBuffer>
  71. /**
  72. * Read from stdout as a Blob
  73. * Automatically calls quiet()
  74. */
  75. blob(): Promise<Blob>
  76. /**
  77. * Configure the shell to not throw an exception on non-zero exit codes.
  78. */
  79. nothrow(): this
  80. /**
  81. * Configure whether or not the shell should throw an exception on non-zero exit codes.
  82. */
  83. throws(shouldThrow: boolean): this
  84. }
  85. export interface BunShellOutput {
  86. readonly stdout: Buffer
  87. readonly stderr: Buffer
  88. readonly exitCode: number
  89. /**
  90. * Read from stdout as a string
  91. */
  92. text(encoding?: BufferEncoding): string
  93. /**
  94. * Read from stdout as a JSON object
  95. */
  96. json(): any
  97. /**
  98. * Read from stdout as an ArrayBuffer
  99. */
  100. arrayBuffer(): ArrayBuffer
  101. /**
  102. * Read from stdout as an Uint8Array
  103. */
  104. bytes(): Uint8Array
  105. /**
  106. * Read from stdout as a Blob
  107. */
  108. blob(): Blob
  109. }
  110. export type BunShellError = Error & BunShellOutput