MCPcopy Index your code
hub / github.com/open-cli-tools/concurrently / InputHandler

Class InputHandler

lib/flow-control/input-handler.ts:20–95  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18 * If the input doesn't start with a command identifier, it is then always sent to the default target.
19 */
20export class InputHandler implements FlowController {
21 private readonly logger: Logger;
22 private readonly defaultInputTarget: CommandIdentifier;
23 private readonly inputStream?: Readable;
24 private readonly pauseInputStreamOnFinish: boolean;
25
26 constructor({
27 defaultInputTarget,
28 inputStream,
29 pauseInputStreamOnFinish,
30 logger,
31 }: {
32 inputStream?: Readable;
33 logger: Logger;
34 defaultInputTarget?: CommandIdentifier;
35 pauseInputStreamOnFinish?: boolean;
36 }) {
37 this.logger = logger;
38 this.defaultInputTarget = defaultInputTarget || defaults.defaultInputTarget;
39 this.inputStream = inputStream;
40 this.pauseInputStreamOnFinish = pauseInputStreamOnFinish !== false;
41 }
42
43 handle(commands: Command[]): {
44 commands: Command[];
45 onFinish?: () => void | undefined;
46 } {
47 const { inputStream } = this;
48 if (!inputStream) {
49 return { commands };
50 }
51
52 const commandsMap = new Map<string, Command>();
53 for (const command of commands) {
54 commandsMap.set(command.index.toString(), command);
55 commandsMap.set(command.name, command);
56 }
57
58 Rx.fromEvent(inputStream, 'data')
59 .pipe(map((data) => String(data)))
60 .subscribe((data) => {
61 const dataParts = data.split(/:(.+)/s);
62 let target = dataParts[0];
63 let command = commandsMap.get(target);
64 let input: string;
65
66 if (dataParts.length > 1 && command) {
67 input = dataParts[1];
68 } else {
69 // If `target` does not match a registered command,
70 // fallback to `defaultInputTarget` and forward the whole input data
71 target = this.defaultInputTarget.toString();
72 command = commandsMap.get(target);
73 input = data;
74 }
75
76 if (command?.stdin) {
77 command.stdin.write(input);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…