| 19 | // secondly for when we've reached EOF. |
| 20 | |
| 21 | function tail(inputs: NodeJS.ReadableStream[], output: NodeJS.WritableStream, numlines: number, code: number): void { |
| 22 | 'use strict'; |
| 23 | if (!inputs || !inputs.length) { |
| 24 | process.exit(code); |
| 25 | return; |
| 26 | } |
| 27 | |
| 28 | let current = inputs[0]; |
| 29 | inputs = inputs.slice(1); |
| 30 | let n = 0; |
| 31 | let outstanding = 0; |
| 32 | let linebuffer: string[] = []; |
| 33 | if (!current) { |
| 34 | // use setTimeout to avoid a deep stack as well as |
| 35 | // cooperatively yield |
| 36 | setTimeout(tail, 0, inputs, output, numlines, code); |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | current.on('readable', function(): void { |
| 41 | let rl = readline.createInterface({ |
| 42 | input: current, |
| 43 | output: null |
| 44 | }); |
| 45 | |
| 46 | rl.on('line', (line: string) => { |
| 47 | n++; |
| 48 | linebuffer.push(line); |
| 49 | if (n > numlines) { |
| 50 | linebuffer.shift(); |
| 51 | } |
| 52 | }); |
| 53 | }); |
| 54 | |
| 55 | // FIXME: this only works for the case of a single input file |
| 56 | current.on('end', function(): void { |
| 57 | outstanding = linebuffer.length; |
| 58 | for (let i = 0; i < linebuffer.length; i++) { |
| 59 | output.write(linebuffer[i] + "\n", () => { |
| 60 | outstanding--; |
| 61 | if (!outstanding) |
| 62 | process.exit(0); |
| 63 | }); |
| 64 | } |
| 65 | //setTimeout(tail, 0, inputs, output, numlines, code); |
| 66 | }); |
| 67 | } |
| 68 | |
| 69 | function main(): void { |
| 70 | 'use strict'; |