選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

child_process.d.ts 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. declare module "child_process" {
  2. import * as events from "events";
  3. import * as net from "net";
  4. import { Writable, Readable, Stream, Pipe } from "stream";
  5. interface ChildProcess extends events.EventEmitter {
  6. stdin: Writable | null;
  7. stdout: Readable | null;
  8. stderr: Readable | null;
  9. readonly channel?: Pipe | null;
  10. readonly stdio: [
  11. Writable | null, // stdin
  12. Readable | null, // stdout
  13. Readable | null, // stderr
  14. Readable | Writable | null | undefined, // extra
  15. Readable | Writable | null | undefined // extra
  16. ];
  17. readonly killed: boolean;
  18. readonly pid: number;
  19. readonly connected: boolean;
  20. kill(signal?: string): void;
  21. send(message: any, callback?: (error: Error | null) => void): boolean;
  22. send(message: any, sendHandle?: net.Socket | net.Server, callback?: (error: Error | null) => void): boolean;
  23. send(message: any, sendHandle?: net.Socket | net.Server, options?: MessageOptions, callback?: (error: Error | null) => void): boolean;
  24. disconnect(): void;
  25. unref(): void;
  26. ref(): void;
  27. /**
  28. * events.EventEmitter
  29. * 1. close
  30. * 2. disconnect
  31. * 3. error
  32. * 4. exit
  33. * 5. message
  34. */
  35. addListener(event: string, listener: (...args: any[]) => void): this;
  36. addListener(event: "close", listener: (code: number, signal: string) => void): this;
  37. addListener(event: "disconnect", listener: () => void): this;
  38. addListener(event: "error", listener: (err: Error) => void): this;
  39. addListener(event: "exit", listener: (code: number | null, signal: string | null) => void): this;
  40. addListener(event: "message", listener: (message: any, sendHandle: net.Socket | net.Server) => void): this;
  41. emit(event: string | symbol, ...args: any[]): boolean;
  42. emit(event: "close", code: number, signal: string): boolean;
  43. emit(event: "disconnect"): boolean;
  44. emit(event: "error", err: Error): boolean;
  45. emit(event: "exit", code: number | null, signal: string | null): boolean;
  46. emit(event: "message", message: any, sendHandle: net.Socket | net.Server): boolean;
  47. on(event: string, listener: (...args: any[]) => void): this;
  48. on(event: "close", listener: (code: number, signal: string) => void): this;
  49. on(event: "disconnect", listener: () => void): this;
  50. on(event: "error", listener: (err: Error) => void): this;
  51. on(event: "exit", listener: (code: number | null, signal: string | null) => void): this;
  52. on(event: "message", listener: (message: any, sendHandle: net.Socket | net.Server) => void): this;
  53. once(event: string, listener: (...args: any[]) => void): this;
  54. once(event: "close", listener: (code: number, signal: string) => void): this;
  55. once(event: "disconnect", listener: () => void): this;
  56. once(event: "error", listener: (err: Error) => void): this;
  57. once(event: "exit", listener: (code: number | null, signal: string | null) => void): this;
  58. once(event: "message", listener: (message: any, sendHandle: net.Socket | net.Server) => void): this;
  59. prependListener(event: string, listener: (...args: any[]) => void): this;
  60. prependListener(event: "close", listener: (code: number, signal: string) => void): this;
  61. prependListener(event: "disconnect", listener: () => void): this;
  62. prependListener(event: "error", listener: (err: Error) => void): this;
  63. prependListener(event: "exit", listener: (code: number | null, signal: string | null) => void): this;
  64. prependListener(event: "message", listener: (message: any, sendHandle: net.Socket | net.Server) => void): this;
  65. prependOnceListener(event: string, listener: (...args: any[]) => void): this;
  66. prependOnceListener(event: "close", listener: (code: number, signal: string) => void): this;
  67. prependOnceListener(event: "disconnect", listener: () => void): this;
  68. prependOnceListener(event: "error", listener: (err: Error) => void): this;
  69. prependOnceListener(event: "exit", listener: (code: number | null, signal: string | null) => void): this;
  70. prependOnceListener(event: "message", listener: (message: any, sendHandle: net.Socket | net.Server) => void): this;
  71. }
  72. // return this object when stdio option is undefined or not specified
  73. interface ChildProcessWithoutNullStreams extends ChildProcess {
  74. stdin: Writable;
  75. stdout: Readable;
  76. stderr: Readable;
  77. readonly stdio: [
  78. Writable, // stdin
  79. Readable, // stdout
  80. Readable, // stderr
  81. Readable | Writable | null | undefined, // extra, no modification
  82. Readable | Writable | null | undefined // extra, no modification
  83. ];
  84. }
  85. interface MessageOptions {
  86. keepOpen?: boolean;
  87. }
  88. type StdioOptions = "pipe" | "ignore" | "inherit" | Array<("pipe" | "ipc" | "ignore" | "inherit" | Stream | number | null | undefined)>;
  89. interface ProcessEnvOptions {
  90. uid?: number;
  91. gid?: number;
  92. cwd?: string;
  93. env?: NodeJS.ProcessEnv;
  94. }
  95. interface CommonOptions extends ProcessEnvOptions {
  96. /**
  97. * @default true
  98. */
  99. windowsHide?: boolean;
  100. /**
  101. * @default 0
  102. */
  103. timeout?: number;
  104. }
  105. interface SpawnOptions extends CommonOptions {
  106. argv0?: string;
  107. stdio?: StdioOptions;
  108. detached?: boolean;
  109. shell?: boolean | string;
  110. windowsVerbatimArguments?: boolean;
  111. }
  112. interface SpawnOptionsWithoutStdio extends SpawnOptions {
  113. stdio?: 'pipe' | Array<null | undefined | 'pipe'>;
  114. }
  115. function spawn(command: string, options?: SpawnOptionsWithoutStdio): ChildProcessWithoutNullStreams;
  116. function spawn(command: string, options: SpawnOptions): ChildProcess;
  117. function spawn(command: string, args?: ReadonlyArray<string>, options?: SpawnOptionsWithoutStdio): ChildProcessWithoutNullStreams;
  118. function spawn(command: string, args: ReadonlyArray<string>, options: SpawnOptions): ChildProcess;
  119. interface ExecOptions extends CommonOptions {
  120. shell?: string;
  121. maxBuffer?: number;
  122. killSignal?: string;
  123. }
  124. interface ExecOptionsWithStringEncoding extends ExecOptions {
  125. encoding: BufferEncoding;
  126. }
  127. interface ExecOptionsWithBufferEncoding extends ExecOptions {
  128. encoding: string | null; // specify `null`.
  129. }
  130. interface ExecException extends Error {
  131. cmd?: string;
  132. killed?: boolean;
  133. code?: number;
  134. signal?: string;
  135. }
  136. // no `options` definitely means stdout/stderr are `string`.
  137. function exec(command: string, callback?: (error: ExecException | null, stdout: string, stderr: string) => void): ChildProcess;
  138. // `options` with `"buffer"` or `null` for `encoding` means stdout/stderr are definitely `Buffer`.
  139. function exec(command: string, options: { encoding: "buffer" | null } & ExecOptions, callback?: (error: ExecException | null, stdout: Buffer, stderr: Buffer) => void): ChildProcess;
  140. // `options` with well known `encoding` means stdout/stderr are definitely `string`.
  141. function exec(command: string, options: { encoding: BufferEncoding } & ExecOptions, callback?: (error: ExecException | null, stdout: string, stderr: string) => void): ChildProcess;
  142. // `options` with an `encoding` whose type is `string` means stdout/stderr could either be `Buffer` or `string`.
  143. // There is no guarantee the `encoding` is unknown as `string` is a superset of `BufferEncoding`.
  144. function exec(command: string, options: { encoding: string } & ExecOptions, callback?: (error: ExecException | null, stdout: string | Buffer, stderr: string | Buffer) => void): ChildProcess;
  145. // `options` without an `encoding` means stdout/stderr are definitely `string`.
  146. function exec(command: string, options: ExecOptions, callback?: (error: ExecException | null, stdout: string, stderr: string) => void): ChildProcess;
  147. // fallback if nothing else matches. Worst case is always `string | Buffer`.
  148. function exec(
  149. command: string,
  150. options: ({ encoding?: string | null } & ExecOptions) | undefined | null,
  151. callback?: (error: ExecException | null, stdout: string | Buffer, stderr: string | Buffer) => void,
  152. ): ChildProcess;
  153. // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime.
  154. namespace exec {
  155. function __promisify__(command: string): Promise<{ stdout: string, stderr: string }>;
  156. function __promisify__(command: string, options: { encoding: "buffer" | null } & ExecOptions): Promise<{ stdout: Buffer, stderr: Buffer }>;
  157. function __promisify__(command: string, options: { encoding: BufferEncoding } & ExecOptions): Promise<{ stdout: string, stderr: string }>;
  158. function __promisify__(command: string, options: ExecOptions): Promise<{ stdout: string, stderr: string }>;
  159. function __promisify__(command: string, options?: ({ encoding?: string | null } & ExecOptions) | null): Promise<{ stdout: string | Buffer, stderr: string | Buffer }>;
  160. }
  161. interface ExecFileOptions extends CommonOptions {
  162. maxBuffer?: number;
  163. killSignal?: string;
  164. windowsVerbatimArguments?: boolean;
  165. shell?: boolean | string;
  166. }
  167. interface ExecFileOptionsWithStringEncoding extends ExecFileOptions {
  168. encoding: BufferEncoding;
  169. }
  170. interface ExecFileOptionsWithBufferEncoding extends ExecFileOptions {
  171. encoding: 'buffer' | null;
  172. }
  173. interface ExecFileOptionsWithOtherEncoding extends ExecFileOptions {
  174. encoding: string;
  175. }
  176. function execFile(file: string): ChildProcess;
  177. function execFile(file: string, options: ({ encoding?: string | null } & ExecFileOptions) | undefined | null): ChildProcess;
  178. function execFile(file: string, args?: ReadonlyArray<string> | null): ChildProcess;
  179. function execFile(file: string, args: ReadonlyArray<string> | undefined | null, options: ({ encoding?: string | null } & ExecFileOptions) | undefined | null): ChildProcess;
  180. // no `options` definitely means stdout/stderr are `string`.
  181. function execFile(file: string, callback: (error: Error | null, stdout: string, stderr: string) => void): ChildProcess;
  182. function execFile(file: string, args: ReadonlyArray<string> | undefined | null, callback: (error: Error | null, stdout: string, stderr: string) => void): ChildProcess;
  183. // `options` with `"buffer"` or `null` for `encoding` means stdout/stderr are definitely `Buffer`.
  184. function execFile(file: string, options: ExecFileOptionsWithBufferEncoding, callback: (error: Error | null, stdout: Buffer, stderr: Buffer) => void): ChildProcess;
  185. function execFile(
  186. file: string,
  187. args: ReadonlyArray<string> | undefined | null,
  188. options: ExecFileOptionsWithBufferEncoding,
  189. callback: (error: Error | null, stdout: Buffer, stderr: Buffer) => void,
  190. ): ChildProcess;
  191. // `options` with well known `encoding` means stdout/stderr are definitely `string`.
  192. function execFile(file: string, options: ExecFileOptionsWithStringEncoding, callback: (error: Error | null, stdout: string, stderr: string) => void): ChildProcess;
  193. function execFile(
  194. file: string,
  195. args: ReadonlyArray<string> | undefined | null,
  196. options: ExecFileOptionsWithStringEncoding,
  197. callback: (error: Error | null, stdout: string, stderr: string) => void,
  198. ): ChildProcess;
  199. // `options` with an `encoding` whose type is `string` means stdout/stderr could either be `Buffer` or `string`.
  200. // There is no guarantee the `encoding` is unknown as `string` is a superset of `BufferEncoding`.
  201. function execFile(
  202. file: string,
  203. options: ExecFileOptionsWithOtherEncoding,
  204. callback: (error: Error | null, stdout: string | Buffer, stderr: string | Buffer) => void,
  205. ): ChildProcess;
  206. function execFile(
  207. file: string,
  208. args: ReadonlyArray<string> | undefined | null,
  209. options: ExecFileOptionsWithOtherEncoding,
  210. callback: (error: Error | null, stdout: string | Buffer, stderr: string | Buffer) => void,
  211. ): ChildProcess;
  212. // `options` without an `encoding` means stdout/stderr are definitely `string`.
  213. function execFile(file: string, options: ExecFileOptions, callback: (error: Error | null, stdout: string, stderr: string) => void): ChildProcess;
  214. function execFile(file: string, args: ReadonlyArray<string> | undefined | null, options: ExecFileOptions, callback: (error: Error | null, stdout: string, stderr: string) => void): ChildProcess;
  215. // fallback if nothing else matches. Worst case is always `string | Buffer`.
  216. function execFile(
  217. file: string,
  218. options: ({ encoding?: string | null } & ExecFileOptions) | undefined | null,
  219. callback: ((error: Error | null, stdout: string | Buffer, stderr: string | Buffer) => void) | undefined | null,
  220. ): ChildProcess;
  221. function execFile(
  222. file: string,
  223. args: ReadonlyArray<string> | undefined | null,
  224. options: ({ encoding?: string | null } & ExecFileOptions) | undefined | null,
  225. callback: ((error: Error | null, stdout: string | Buffer, stderr: string | Buffer) => void) | undefined | null,
  226. ): ChildProcess;
  227. // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime.
  228. namespace execFile {
  229. function __promisify__(file: string): Promise<{ stdout: string, stderr: string }>;
  230. function __promisify__(file: string, args: string[] | undefined | null): Promise<{ stdout: string, stderr: string }>;
  231. function __promisify__(file: string, options: ExecFileOptionsWithBufferEncoding): Promise<{ stdout: Buffer, stderr: Buffer }>;
  232. function __promisify__(file: string, args: string[] | undefined | null, options: ExecFileOptionsWithBufferEncoding): Promise<{ stdout: Buffer, stderr: Buffer }>;
  233. function __promisify__(file: string, options: ExecFileOptionsWithStringEncoding): Promise<{ stdout: string, stderr: string }>;
  234. function __promisify__(file: string, args: string[] | undefined | null, options: ExecFileOptionsWithStringEncoding): Promise<{ stdout: string, stderr: string }>;
  235. function __promisify__(file: string, options: ExecFileOptionsWithOtherEncoding): Promise<{ stdout: string | Buffer, stderr: string | Buffer }>;
  236. function __promisify__(file: string, args: string[] | undefined | null, options: ExecFileOptionsWithOtherEncoding): Promise<{ stdout: string | Buffer, stderr: string | Buffer }>;
  237. function __promisify__(file: string, options: ExecFileOptions): Promise<{ stdout: string, stderr: string }>;
  238. function __promisify__(file: string, args: string[] | undefined | null, options: ExecFileOptions): Promise<{ stdout: string, stderr: string }>;
  239. function __promisify__(file: string, options: ({ encoding?: string | null } & ExecFileOptions) | undefined | null): Promise<{ stdout: string | Buffer, stderr: string | Buffer }>;
  240. function __promisify__(
  241. file: string,
  242. args: string[] | undefined | null,
  243. options: ({ encoding?: string | null } & ExecFileOptions) | undefined | null,
  244. ): Promise<{ stdout: string | Buffer, stderr: string | Buffer }>;
  245. }
  246. interface ForkOptions extends ProcessEnvOptions {
  247. execPath?: string;
  248. execArgv?: string[];
  249. silent?: boolean;
  250. stdio?: StdioOptions;
  251. detached?: boolean;
  252. windowsVerbatimArguments?: boolean;
  253. }
  254. function fork(modulePath: string, args?: ReadonlyArray<string>, options?: ForkOptions): ChildProcess;
  255. interface SpawnSyncOptions extends CommonOptions {
  256. argv0?: string; // Not specified in the docs
  257. input?: string | Buffer | NodeJS.TypedArray | DataView;
  258. stdio?: StdioOptions;
  259. killSignal?: string | number;
  260. maxBuffer?: number;
  261. encoding?: string;
  262. shell?: boolean | string;
  263. windowsVerbatimArguments?: boolean;
  264. }
  265. interface SpawnSyncOptionsWithStringEncoding extends SpawnSyncOptions {
  266. encoding: BufferEncoding;
  267. }
  268. interface SpawnSyncOptionsWithBufferEncoding extends SpawnSyncOptions {
  269. encoding: string; // specify `null`.
  270. }
  271. interface SpawnSyncReturns<T> {
  272. pid: number;
  273. output: string[];
  274. stdout: T;
  275. stderr: T;
  276. status: number | null;
  277. signal: string | null;
  278. error?: Error;
  279. }
  280. function spawnSync(command: string): SpawnSyncReturns<Buffer>;
  281. function spawnSync(command: string, options?: SpawnSyncOptionsWithStringEncoding): SpawnSyncReturns<string>;
  282. function spawnSync(command: string, options?: SpawnSyncOptionsWithBufferEncoding): SpawnSyncReturns<Buffer>;
  283. function spawnSync(command: string, options?: SpawnSyncOptions): SpawnSyncReturns<Buffer>;
  284. function spawnSync(command: string, args?: ReadonlyArray<string>, options?: SpawnSyncOptionsWithStringEncoding): SpawnSyncReturns<string>;
  285. function spawnSync(command: string, args?: ReadonlyArray<string>, options?: SpawnSyncOptionsWithBufferEncoding): SpawnSyncReturns<Buffer>;
  286. function spawnSync(command: string, args?: ReadonlyArray<string>, options?: SpawnSyncOptions): SpawnSyncReturns<Buffer>;
  287. interface ExecSyncOptions extends CommonOptions {
  288. input?: string | Buffer | Uint8Array;
  289. stdio?: StdioOptions;
  290. shell?: string;
  291. killSignal?: string | number;
  292. maxBuffer?: number;
  293. encoding?: string;
  294. }
  295. interface ExecSyncOptionsWithStringEncoding extends ExecSyncOptions {
  296. encoding: BufferEncoding;
  297. }
  298. interface ExecSyncOptionsWithBufferEncoding extends ExecSyncOptions {
  299. encoding: string; // specify `null`.
  300. }
  301. function execSync(command: string): Buffer;
  302. function execSync(command: string, options?: ExecSyncOptionsWithStringEncoding): string;
  303. function execSync(command: string, options?: ExecSyncOptionsWithBufferEncoding): Buffer;
  304. function execSync(command: string, options?: ExecSyncOptions): Buffer;
  305. interface ExecFileSyncOptions extends CommonOptions {
  306. input?: string | Buffer | NodeJS.TypedArray | DataView;
  307. stdio?: StdioOptions;
  308. killSignal?: string | number;
  309. maxBuffer?: number;
  310. encoding?: string;
  311. shell?: boolean | string;
  312. }
  313. interface ExecFileSyncOptionsWithStringEncoding extends ExecFileSyncOptions {
  314. encoding: BufferEncoding;
  315. }
  316. interface ExecFileSyncOptionsWithBufferEncoding extends ExecFileSyncOptions {
  317. encoding: string; // specify `null`.
  318. }
  319. function execFileSync(command: string): Buffer;
  320. function execFileSync(command: string, options?: ExecFileSyncOptionsWithStringEncoding): string;
  321. function execFileSync(command: string, options?: ExecFileSyncOptionsWithBufferEncoding): Buffer;
  322. function execFileSync(command: string, options?: ExecFileSyncOptions): Buffer;
  323. function execFileSync(command: string, args?: ReadonlyArray<string>, options?: ExecFileSyncOptionsWithStringEncoding): string;
  324. function execFileSync(command: string, args?: ReadonlyArray<string>, options?: ExecFileSyncOptionsWithBufferEncoding): Buffer;
  325. function execFileSync(command: string, args?: ReadonlyArray<string>, options?: ExecFileSyncOptions): Buffer;
  326. }