add `input` option
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fixes #12. None of the new tests are setup to run on Windows. Is there a cross platform command I can execute that will work well on Travis and AppVeyor? Closes #15
authored by James Talmage · committed by Sindre Sorhus · parent 71a58ee · Browse files · 5 files·+68 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+68 added−0 removed
- other:+68 added−0 removed
Provenance
- Commit
605a674- Tree
f40519c- Author
James Talmage <james@talmage.io>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
71a58ee
5 files changed+68 added−0 removed
added:fixtures/stdin+3 added
1+#!/usr/bin/env node2+'use strict';3+process.stdin.pipe(process.stdout);modified:index.js+21 added
2 unmodified lines3456789101133 unmodified lines4546474849505152535455565758596061626364656645 unmodified lines11211311411511611711811912012114 unmodified lines1361371381391401411421431441451461475 unmodified lines2 unmodified linesvar crossSpawnAsync = require('cross-spawn-async');var stripEof = require('strip-eof');var objectAssign = require('object-assign');var npmRunPath = require('npm-run-path');var isStream = require('is-stream');var pathKey = require('path-key')();var TEN_MEBIBYTE = 1024 * 1024 * 10;
function handleArgs(cmd, args, opts) {33 unmodified lines opts: opts };}
function handleInput(spawned, opts) { var input = opts.input;
if (input === null || input === undefined) { return; }
if (isStream(input)) { input.pipe(spawned.stdin); } else { spawned.stdin.end(input); }}
function handleOutput(opts, val) { if (opts.stripEof) { val = stripEof(val); }45 unmodified lines }); });
crossSpawnAsync._enoent.hookChildProcess(spawned, parsed);
handleInput(spawned, parsed.opts); });
promise.kill = spawned.kill.bind(spawned); promise.pid = spawned.pid;14 unmodified lines return spawned;};
module.exports.sync = function (cmd, args, opts) { if (opts && isStream(opts.input)) { throw new TypeError('The `input` option cannot be a stream in sync mode'); }
var parsed = handleArgs(cmd, args, opts); var out = childProcess.execFileSync(parsed.cmd, parsed.args, parsed.opts);
return handleOutput(parsed.opts, out);5 unmodified linesmodified:package.json+1 added
36 unmodified lines37383940414243444510 unmodified lines36 unmodified lines "local" ], "dependencies": { "cross-spawn-async": "^2.1.1", "is-stream": "^1.1.0", "npm-run-path": "^1.0.0", "object-assign": "^4.0.1", "path-key": "^1.0.0", "strip-eof": "^1.0.0"10 unmodified linesmodified:readme.md+7 added
114 unmodified lines115 116 Prefer locally installed binaries when looking for a binary to execute.<br>117 If you `$ npm install foo`, you can then `execa('foo')`.118 119+#### input120 121+Type: `string` `Buffer` `ReadableStream`122+123+Write some input to the `stdin` of your binary.<br>124+Streams are not allowed when using the synchronous methods.125+126+127 ## License128 129 MIT © [Sindre Sorhus](https://sindresorhus.com)modified:test.js+36 added
1 import path from 'path';2+import stream from 'stream';3 import test from 'ava';4 import getStream from 'get-stream';5 import m from './';6 60 unmodified lines67 await t.throws(m('cat-names', {preferLocal: false}), /spawn .* ENOENT/);68 process.env.PATH = _path;69 });70 71+test('input option can be a String', async t => {72+ const {stdout} = await m('stdin', [], {input: 'foobar'});73+ t.is(stdout, 'foobar');74+});75+76+test('input option can be a Buffer', async t => {77+ const {stdout} = await m('stdin', [], {input: 'testing12'});78+ t.is(stdout, 'testing12');79+});80+81+test('input can be a Stream', async t => {82+ var s = new stream.PassThrough();83+ s.write('howdy');84+ s.end();85+ const {stdout} = await m('stdin', [], {input: s});86+ t.is(stdout, 'howdy');87+});88+89+test('input option can be a String - sync', async t => {90+ const stdout = m.sync('stdin', [], {input: 'foobar'});91+ t.is(stdout, 'foobar');92+});93+94+test('input option can be a Buffer - sync', async t => {95+ const stdout = m.sync('stdin', [], {input: new Buffer('testing12', 'utf8')});96+ t.is(stdout, 'testing12');97+});98+99+test('helpful error trying to provide an input stream in sync mode', t => {100+ t.throws(101+ () => m.sync('stdin', [], {input: new stream.PassThrough()}),102+ /The `input` option cannot be a stream in sync mode/103+ );104+});105+106 test('execa() returns a promise with kill() and pid', t => {107 const promise = m('noop', ['foo']);108 t.is(typeof promise.kill, 'function');109 t.is(typeof promise.pid, 'number');1 unmodified line