UNPKG

1.28 kBJavaScriptView Raw
1#!/usr/bin/env node
2'use strict';
3
4var _shx = require('./shx');
5
6// `input` is null if we're running from a TTY, or a string of all stdin if
7// running from the right-hand side of a pipe
8var run = function run(input) {
9 // Pass stdin to shx as the 'this' parameter
10 var code = _shx.shx.call(input, process.argv);
11
12 // Make sure output is flushed before exiting the process. Please see:
13 // - https://github.com/shelljs/shx/issues/85
14 // - https://github.com/mochajs/mocha/issues/333
15 var streamCount = 0;
16 var streams = [process.stdout, process.stderr];
17 streams.forEach(function (stream) {
18 streamCount++; // count each stream
19 stream.write('', function () {
20 streamCount--; // mark each stream as finished
21 if (streamCount === 0) process.exit(code);
22 });
23 });
24};
25
26// ShellJS doesn't support input streams, so we have to collect all input first
27if (process.stdin.isTTY) {
28 // There's no stdin, so we can immediately invoke the ShellJS function
29 run(null);
30} else {
31 (function () {
32 // Read all stdin first, and then pass that onto ShellJS
33 var chunks = [];
34 process.stdin.on('data', function (data) {
35 return chunks.push(data);
36 });
37 process.stdin.on('end', function () {
38 return run(chunks.join(''));
39 });
40 })();
41}
\No newline at end of file