shell.d.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**
  2. * @since 1.0.0
  3. */
  4. interface SpawnOptions {
  5. /** Current working directory. */
  6. cwd?: string;
  7. /** Environment variables. set to `null` to clear the process env. */
  8. env?: Record<string, string>;
  9. /**
  10. * Character encoding for stdout/stderr
  11. *
  12. * @since 1.1.0
  13. * */
  14. encoding?: string;
  15. }
  16. /**
  17. * @since 1.0.0
  18. */
  19. interface ChildProcess {
  20. /** Exit code of the process. `null` if the process was terminated by a signal on Unix. */
  21. code: number | null;
  22. /** If the process was terminated by a signal, represents that signal. */
  23. signal: number | null;
  24. /** The data that the process wrote to `stdout`. */
  25. stdout: string;
  26. /** The data that the process wrote to `stderr`. */
  27. stderr: string;
  28. }
  29. /**
  30. * @since 1.0.0
  31. */
  32. declare class EventEmitter<E extends string> {
  33. /** @ignore */
  34. private eventListeners;
  35. /**
  36. * Alias for `emitter.on(eventName, listener)`.
  37. *
  38. * @since 1.1.0
  39. */
  40. addListener(eventName: E, listener: (...args: any[]) => void): this;
  41. /**
  42. * Alias for `emitter.off(eventName, listener)`.
  43. *
  44. * @since 1.1.0
  45. */
  46. removeListener(eventName: E, listener: (...args: any[]) => void): this;
  47. /**
  48. * Adds the `listener` function to the end of the listeners array for the
  49. * event named `eventName`. No checks are made to see if the `listener` has
  50. * already been added. Multiple calls passing the same combination of `eventName`and `listener` will result in the `listener` being added, and called, multiple
  51. * times.
  52. *
  53. * Returns a reference to the `EventEmitter`, so that calls can be chained.
  54. *
  55. * @since 1.0.0
  56. */
  57. on(eventName: E, listener: (...args: any[]) => void): this;
  58. /**
  59. * Adds a **one-time**`listener` function for the event named `eventName`. The
  60. * next time `eventName` is triggered, this listener is removed and then invoked.
  61. *
  62. * Returns a reference to the `EventEmitter`, so that calls can be chained.
  63. *
  64. * @since 1.1.0
  65. */
  66. once(eventName: E, listener: (...args: any[]) => void): this;
  67. /**
  68. * Removes the all specified listener from the listener array for the event eventName
  69. * Returns a reference to the `EventEmitter`, so that calls can be chained.
  70. *
  71. * @since 1.1.0
  72. */
  73. off(eventName: E, listener: (...args: any[]) => void): this;
  74. /**
  75. * Removes all listeners, or those of the specified eventName.
  76. *
  77. * Returns a reference to the `EventEmitter`, so that calls can be chained.
  78. *
  79. * @since 1.1.0
  80. */
  81. removeAllListeners(event?: E): this;
  82. /**
  83. * @ignore
  84. * Synchronously calls each of the listeners registered for the event named`eventName`, in the order they were registered, passing the supplied arguments
  85. * to each.
  86. *
  87. * @returns `true` if the event had listeners, `false` otherwise.
  88. */
  89. emit(eventName: E, ...args: any[]): boolean;
  90. /**
  91. * Returns the number of listeners listening to the event named `eventName`.
  92. *
  93. * @since 1.1.0
  94. */
  95. listenerCount(eventName: E): number;
  96. /**
  97. * Adds the `listener` function to the _beginning_ of the listeners array for the
  98. * event named `eventName`. No checks are made to see if the `listener` has
  99. * already been added. Multiple calls passing the same combination of `eventName`and `listener` will result in the `listener` being added, and called, multiple
  100. * times.
  101. *
  102. * Returns a reference to the `EventEmitter`, so that calls can be chained.
  103. *
  104. * @since 1.1.0
  105. */
  106. prependListener(eventName: E, listener: (...args: any[]) => void): this;
  107. /**
  108. * Adds a **one-time**`listener` function for the event named `eventName` to the_beginning_ of the listeners array. The next time `eventName` is triggered, this
  109. * listener is removed, and then invoked.
  110. *
  111. * Returns a reference to the `EventEmitter`, so that calls can be chained.
  112. *
  113. * @since 1.1.0
  114. */
  115. prependOnceListener(eventName: E, listener: (...args: any[]) => void): this;
  116. }
  117. /**
  118. * @since 1.1.0
  119. */
  120. declare class Child {
  121. /** The child process `pid`. */
  122. pid: number;
  123. constructor(pid: number);
  124. /**
  125. * Writes `data` to the `stdin`.
  126. *
  127. * @param data The message to write, either a string or a byte array.
  128. * @example
  129. * ```typescript
  130. * import { Command } from '@tauri-apps/api/shell';
  131. * const command = new Command('node');
  132. * const child = await command.spawn();
  133. * await child.write('message');
  134. * await child.write([0, 1, 2, 3, 4, 5]);
  135. * ```
  136. *
  137. * @returns A promise indicating the success or failure of the operation.
  138. */
  139. write(data: string | Uint8Array): Promise<void>;
  140. /**
  141. * Kills the child process.
  142. *
  143. * @returns A promise indicating the success or failure of the operation.
  144. */
  145. kill(): Promise<void>;
  146. }
  147. /**
  148. * The entry point for spawning child processes.
  149. * It emits the `close` and `error` events.
  150. * @example
  151. * ```typescript
  152. * import { Command } from '@tauri-apps/api/shell';
  153. * const command = new Command('node');
  154. * command.on('close', data => {
  155. * console.log(`command finished with code ${data.code} and signal ${data.signal}`)
  156. * });
  157. * command.on('error', error => console.error(`command error: "${error}"`));
  158. * command.stdout.on('data', line => console.log(`command stdout: "${line}"`));
  159. * command.stderr.on('data', line => console.log(`command stderr: "${line}"`));
  160. *
  161. * const child = await command.spawn();
  162. * console.log('pid:', child.pid);
  163. * ```
  164. *
  165. * @since 1.1.0
  166. *
  167. */
  168. declare class Command extends EventEmitter<'close' | 'error'> {
  169. /** @ignore Program to execute. */
  170. private readonly program;
  171. /** @ignore Program arguments */
  172. private readonly args;
  173. /** @ignore Spawn options. */
  174. private readonly options;
  175. /** Event emitter for the `stdout`. Emits the `data` event. */
  176. readonly stdout: EventEmitter<"data">;
  177. /** Event emitter for the `stderr`. Emits the `data` event. */
  178. readonly stderr: EventEmitter<"data">;
  179. /**
  180. * Creates a new `Command` instance.
  181. *
  182. * @param program The program name to execute.
  183. * It must be configured on `tauri.conf.json > tauri > allowlist > shell > scope`.
  184. * @param args Program arguments.
  185. * @param options Spawn options.
  186. */
  187. constructor(program: string, args?: string | string[], options?: SpawnOptions);
  188. /**
  189. * Creates a command to execute the given sidecar program.
  190. * @example
  191. * ```typescript
  192. * import { Command } from '@tauri-apps/api/shell';
  193. * const command = Command.sidecar('my-sidecar');
  194. * const output = await command.execute();
  195. * ```
  196. *
  197. * @param program The program to execute.
  198. * It must be configured on `tauri.conf.json > tauri > allowlist > shell > scope`.
  199. */
  200. static sidecar(program: string, args?: string | string[], options?: SpawnOptions): Command;
  201. /**
  202. * Executes the command as a child process, returning a handle to it.
  203. *
  204. * @returns A promise resolving to the child process handle.
  205. */
  206. spawn(): Promise<Child>;
  207. /**
  208. * Executes the command as a child process, waiting for it to finish and collecting all of its output.
  209. * @example
  210. * ```typescript
  211. * import { Command } from '@tauri-apps/api/shell';
  212. * const output = await new Command('echo', 'message').execute();
  213. * assert(output.code === 0);
  214. * assert(output.signal === null);
  215. * assert(output.stdout === 'message');
  216. * assert(output.stderr === '');
  217. * ```
  218. *
  219. * @returns A promise resolving to the child process output.
  220. */
  221. execute(): Promise<ChildProcess>;
  222. }
  223. /**
  224. * Opens a path or URL with the system's default app,
  225. * or the one specified with `openWith`.
  226. *
  227. * The `openWith` value must be one of `firefox`, `google chrome`, `chromium` `safari`,
  228. * `open`, `start`, `xdg-open`, `gio`, `gnome-open`, `kde-open` or `wslview`.
  229. *
  230. * @example
  231. * ```typescript
  232. * import { open } from '@tauri-apps/api/shell';
  233. * // opens the given URL on the default browser:
  234. * await open('https://github.com/tauri-apps/tauri');
  235. * // opens the given URL using `firefox`:
  236. * await open('https://github.com/tauri-apps/tauri', 'firefox');
  237. * // opens a file using the default program:
  238. * await open('/path/to/file');
  239. * ```
  240. *
  241. * @param path The path or URL to open.
  242. * This value is matched against the string regex defined on `tauri.conf.json > tauri > allowlist > shell > open`,
  243. * which defaults to `^((mailto:\w+)|(tel:\w+)|(https?://\w+)).+`.
  244. * @param openWith The app to open the file or URL with.
  245. * Defaults to the system default application for the specified path type.
  246. *
  247. * @since 1.0.0
  248. */
  249. declare function open(path: string, openWith?: string): Promise<void>;
  250. export { Command, Child, EventEmitter, open };
  251. export type { ChildProcess, SpawnOptions };