UNPKG

3.58 kBTypeScriptView Raw
1/// <reference types="node" />
2/// <reference types="node" />
3/// <reference types="node" />
4/// <reference types="node" />
5import { ChildProcess as BaseChildProcess, SpawnOptions } from 'child_process';
6import * as Rx from 'rxjs';
7import { EventEmitter, Writable } from 'stream';
8/**
9 * Identifier for a command; if string, it's the command's name, if number, it's the index.
10 */
11export type CommandIdentifier = string | number;
12export interface CommandInfo {
13 /**
14 * Command's name.
15 */
16 name: string;
17 /**
18 * Which command line the command has.
19 */
20 command: string;
21 /**
22 * Which environment variables should the spawned process have.
23 */
24 env?: Record<string, unknown>;
25 /**
26 * The current working directory of the process when spawned.
27 */
28 cwd?: string;
29 /**
30 * Color to use on prefix of the command.
31 */
32 prefixColor?: string;
33 /**
34 * Output command in raw format.
35 */
36 raw?: boolean;
37}
38export interface CloseEvent {
39 command: CommandInfo;
40 /**
41 * The command's index among all commands ran.
42 */
43 index: number;
44 /**
45 * Whether the command exited because it was killed.
46 */
47 killed: boolean;
48 /**
49 * The exit code or signal for the command.
50 */
51 exitCode: string | number;
52 timings: {
53 startDate: Date;
54 endDate: Date;
55 durationSeconds: number;
56 };
57}
58export interface TimerEvent {
59 startDate: Date;
60 endDate?: Date;
61}
62/**
63 * Subtype of NodeJS's child_process including only what's actually needed for a command to work.
64 */
65export type ChildProcess = EventEmitter & Pick<BaseChildProcess, 'pid' | 'stdin' | 'stdout' | 'stderr'>;
66/**
67 * Interface for a function that must kill the process with `pid`, optionally sending `signal` to it.
68 */
69export type KillProcess = (pid: number, signal?: string) => void;
70/**
71 * Interface for a function that spawns a command and returns its child process instance.
72 */
73export type SpawnCommand = (command: string, options: SpawnOptions) => ChildProcess;
74export declare class Command implements CommandInfo {
75 private readonly killProcess;
76 private readonly spawn;
77 private readonly spawnOpts;
78 readonly index: number;
79 /** @inheritdoc */
80 readonly name: string;
81 /** @inheritdoc */
82 readonly command: string;
83 /** @inheritdoc */
84 readonly prefixColor?: string;
85 /** @inheritdoc */
86 readonly env: Record<string, unknown>;
87 /** @inheritdoc */
88 readonly cwd?: string;
89 readonly close: Rx.Subject<CloseEvent>;
90 readonly error: Rx.Subject<unknown>;
91 readonly stdout: Rx.Subject<Buffer>;
92 readonly stderr: Rx.Subject<Buffer>;
93 readonly timer: Rx.Subject<TimerEvent>;
94 process?: ChildProcess;
95 stdin?: Writable;
96 pid?: number;
97 killed: boolean;
98 exited: boolean;
99 /** @deprecated */
100 get killable(): boolean;
101 constructor({ index, name, command, prefixColor, env, cwd }: CommandInfo & {
102 index: number;
103 }, spawnOpts: SpawnOptions, spawn: SpawnCommand, killProcess: KillProcess);
104 /**
105 * Starts this command, piping output, error and close events onto the corresponding observables.
106 */
107 start(): void;
108 /**
109 * Kills this command, optionally specifying a signal to send to it.
110 */
111 kill(code?: string): void;
112 /**
113 * Detects whether a command can be killed.
114 *
115 * Also works as a type guard on the input `command`.
116 */
117 static canKill(command: Command): command is Command & {
118 pid: number;
119 process: ChildProcess;
120 };
121}