Skip to content

Instantly share code, notes, and snippets.

@polotek
Created February 4, 2012 07:31
Show Gist options
  • Save polotek/1736163 to your computer and use it in GitHub Desktop.
Save polotek/1736163 to your computer and use it in GitHub Desktop.
Potential apis for procstreams chdir support
var $p = require('procstreams');
// this is the one I'm leaning towards. simple and intuitive
// cd is a special chain function that changes dir
// and keeps state for subsequent proc chains
$p.cd('tests')
.and('cat test-simple.js')
.pipe('grep data')
.then
// chdir is an alias.
// notice how it hangs directly off the logic functions. that might be
// a pain, but anything else feels ugly
.chdir('../examples')
.and('cat foo.js')
.pipe('grep test')
var $p = require('procstreams');
// this is a little more verbose. it's inspired by "scoped blocks" in
// langs like ruby and python. may not translate well to js. also harder
// to implement I think
// cd runs a scoped function where any procs run within
// execute in the given dir
$p.cd('tests', function(chain) {
chain('cat test-simple.js')
.pipe('grep data')
})
.then
// notice that each cd starts from the original pwd instead of relative to last cd
.cd('examples', function(chain) {
chain('cat foo.js')
.pipe('grep test')
})
var $p = require('procstreams');
// This also "feels" nice. but I don't think I want to
// get into special casing commands passed as args. it ruins
// the semantics of "give me something and I'll shell it out as is".
// the command parser recognizes "cd" command and handles
// it correctly for you
$p('cd tests')
.and('cat test-simple.js')
.pipe('grep data')
.then('cd ../examples')
.and('cat foo.js')
.pipe('grep test')
@mikeal
Copy link

mikeal commented Feb 5, 2012

what does this actually do? does it change the default cwd passed to new processes? if so, i think it should be cwd().

@polotek
Copy link
Author

polotek commented Feb 5, 2012

Well I was considering that too. In a bash script, you call cd and it changes the cwd of the entire execution. The equivalent here would be using process.cwd() underneath. But I'm also considering maybe you don't really want that. Maybe the cd should be local to the proc chain. In that case, it would just save the absolute path and pass it to each proc in the cwd option. That way you could have 2 separate proc chains execute in parallel in separate dir scopes.

@polotek
Copy link
Author

polotek commented Feb 5, 2012

For example

$p.cd('foo/').and('do stuff'');
$p.cd('bar/').and('do other stuff');

@stevengill
Copy link

I like first implementation. Easy to follow along and I think that is how people would expect it to work. I would say that keeping cd local to the proc chain makes the most sense. This is how child processes works. Each process is its own being that shouldn't really rely on previous processes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment