Skip to content

feat(pipe): add support for pipes between commands#370

Merged
ariporad merged 1 commit intomasterfrom
feat-pipes
Feb 21, 2016
Merged

feat(pipe): add support for pipes between commands#370
ariporad merged 1 commit intomasterfrom
feat-pipes

Conversation

@nfischer
Copy link
Copy Markdown
Member

Fixes #148

Commands can now be piped, of the form:

grep('foo', 'file1.txt', 'file2.txt').sed(/o/g, 'a').to('output.txt');
echo('files with o\'s in the name:\n' + ls().grep('o'));
cat('test.js').exec('node'); // pipe to exec() call

Commands can send their output to another command in a pipe-like fashion. sed, grep, cat, exec, to, and toEnd can appear on the right-hand side of a pipe. Pipes can be chained. You can even pipe into async exec().

This also improves performance slightly. I got rid of the slow, home-grown escape() function inside of src/exec.js and replaced it with JSON.stringify(), which I measured to be slightly faster, and much more reliable (it properly escapes newlines, tabs, etc. as well).

I couldn't think of any tests to add for piping to exec() on Windows (can't think of any native commands that can be on the right-hand side of a pipe). I'm fairly certain this code does work on Windows, I just couldn't think of a use-case. Worst case, we can go back and have someone more Windows-savvy make a PR adding the tests.

@nfischer nfischer added this to the v0.7.0 milestone Feb 21, 2016
@nfischer nfischer mentioned this pull request Feb 21, 2016
src/common.js Outdated
var that = new String(str);
that.to = wrap('to', _to, {idx: 1});
that.toEnd = wrap('toEnd', _toEnd, {idx: 1});
that.cat = wrap('cat', _cat, {idx: 1});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this just be shell.to/cat/toEnd/sed/grep/exec? It seems like repeatedly re-wrapping command isn't performant, and it's not DRY.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In terms of performance, it actually doesn't hurt much. wrap() is actually very quick, since it just returns another function. But I agree with the DRY part, so this should be fixed now.

@nfischer nfischer force-pushed the feat-pipes branch 3 times, most recently from cb01a59 to 910dd37 Compare February 21, 2016 06:00
@nfischer
Copy link
Copy Markdown
Member Author

@ariporad this should be a much cleaner implementation now. ShellString() is a little different than you discussed, but I think it solves the same problem. Let me know what you think.

}
that.stderr = stderr;
that.to = function() {wrap('to', _to, {idx: 1}).apply(that.stdout, arguments); return that;};
that.toEnd = function() {wrap('toEnd', _toEnd, {idx: 1}).apply(that.stdout, arguments); return that;};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we still re-wraping these?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe these are only wrapped in this spot (they're methods, so they're wrapped here now).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is is possible to cache the call to common.wrap?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure of a good way to do this, because if I move it out of the function, then that isn't defined anymore. So then the code only works for strings, not arrays as well. So I think we might have to leave it as-is. The code should still be very efficient, since wrap is just a closure.

Or we can modify .to() to join this if it's an array, but that would incur a performance hit.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.

@ariporad
Copy link
Copy Markdown
Contributor

One small nit, otherwise looks good.

@ariporad
Copy link
Copy Markdown
Contributor

LGTM!

ariporad added a commit that referenced this pull request Feb 21, 2016
feat(pipe): add support for pipes between commands
@ariporad ariporad merged commit 77b41d8 into master Feb 21, 2016
@ariporad ariporad deleted the feat-pipes branch February 21, 2016 21:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants