| 18 | // event listeners, the first for when there is data available, and |
| 19 | // secondly for when we've reached EOF. |
| 20 | function sort(inputs: NodeJS.ReadableStream[], output: NodeJS.WritableStream, code: number, lines: string[]): void { |
| 21 | 'use strict'; |
| 22 | |
| 23 | if (!inputs || !inputs.length) { |
| 24 | lines.sort(); |
| 25 | output.write(lines.join('\n') + '\n', () => { |
| 26 | process.exit(code); |
| 27 | }); |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | let current = inputs[0]; |
| 32 | inputs = inputs.slice(1); |
| 33 | |
| 34 | if (!current) { |
| 35 | // use setTimeout to avoid a deep stack as well as |
| 36 | // cooperatively yield |
| 37 | setTimeout(sort, 0, inputs, output, code, lines); |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | current.on('readable', function(): void { |
| 42 | let rl = readline.createInterface({ |
| 43 | input: current, |
| 44 | output: null |
| 45 | }); |
| 46 | |
| 47 | rl.on('line', (line: string) => { |
| 48 | lines.push(line); |
| 49 | }); |
| 50 | }); |
| 51 | |
| 52 | current.on('end', function(): void { |
| 53 | setTimeout(sort, 0, inputs, output, code, lines); |
| 54 | }); |
| 55 | } |
| 56 | |
| 57 | function main(): void { |
| 58 | 'use strict'; |