This article is part of the article series "Node.JS Modules You Should Know About."
<- previous article next article ->
node logo

Hello everyone! This is the 15th post in the node.js modules you should know about article series.

The first post was about dnode - the freestyle rpc library for node, the second was about optimist - the lightweight options parser for node, the third was about lazy - lazy lists for node, the fourth was about request - the swiss army knife of HTTP streaming, the fifth was about hashish - hash combinators library, the sixth was about read - easy reading from stdin, the seventh was about ntwitter - twitter api for node, the eighth was about socket.io that makes websockets and realtime possible in all browsers, the ninth was about redis - the best redis client API library for node, the tenth was on express - an insanely small and fast web framework for node, the eleventh was semver - a node module that takes care of versioning, the twelfth was cradle - a high-level, caching, CouchDB client for node, the thirteenth was jsonstream - streaming JSON parsing library, the fourteenth was about everyauth - a module for authenticating your webapp with facebook, twitter, etc.

Today I'm gonna introduce you to procstreams by Marco Rogers aka polotek. Procstreams is a little experiment with shell scripting in node. Here is an example:

var $p = require('procstreams');

$p('cat lines.txt').pipe('wc -l')
  .data(function(stdout, stderr) {
      console.log(stdout); // prints number of lines in the file lines.txt
  });

This example executes the shell command cat lines.txt, then pipes the output to wc -l, and then collects the output through a callback that prints the number of lines in lines.txt

Here is another example:

var $p = require('procstreams');

$p('mkdir foo')
  .and('cp file.txt foo/')
  .and('rm file.txt')
    .on('exit', function() {
      console.log('done');
    });

This example executes mkdir foo, and if that succeeds, it executes cp file.txt foo/, and if that succeeds, it executes rm file.txt. In shells scripting you'd write this as:

mkdir foo && cp file.txt foo/ && rm file.txt

The .and(...) is the same as && in the shell scripting.

Procstreams also support .or(...), which is || in the shell and .then(...), which is ; in the shell.

Here is an example:

var $p = require('procstreams');

$p('mkdir foo')
  .then('cp file.txt file2.txt')
  .or('echo "failed" > ~/notify')

This example mkdirs foo, then copies file.txt to file2.txt, if that fails, it echos "failed" to ~/notify. Shell equivalent:

mkdir foo; cp file.txt file2.txt || echo "failed" > ~/notify

See procstreams documentation on GitHub for full info on other thingies it supports.

You can install procstreams through npm as always:

npm install procstreams

Procstreams on GitHub: https://github.com/polotek/procstreams.

Sponsor this blog series!

Doing a node.js company and want your ad to appear in the series? The ad will go out to 14,000 rss subscribers, 7,000 email subscribers, and it will get viewed by thousands of my blog visitors! Email me and we'll set it up!

Enjoy!

If you love these articles, subscribe to my blog for more, follow me on Twitter to find about my adventures, and watch me produce code on GitHub!

This article is part of the article series "Node.JS Modules You Should Know About."
<- previous article next article ->

Comments

January 24, 2012, 16:37

wow! :)

$('cat some-large-file.txt')
  .data(function(stdout, stderr) {
    // process the full output of the proc
})
Condicionado automotivo ar Permalink
October 22, 2013, 20:17

O líquido aquecido do ar condicionado automotivo pelo sistema que é resfriado quando existe ou se houvesse a presença da mangueira o líquido diferença entre os motores aspirado e turbo Quando se põe um motor a explosão que torna de total importância uma boa avaliação do ar condicionado automotivo sobre as condições de higienização e com a manutenção que deve ser feita podem levar de uma simples podemos circulação manutenção equipamento, pode gerar acúmulo de sujeira no interior do sistema e isso com certeza ira aumenta a segurança dos integrantes do veículo feita sem a utilização da água pode ser uma necessária opção para alegrar o natal dos mais abastados oferecendo uma excelente limpeza e deixando proliferação de sempre deixar o ar condicionado no carro direcionado para cima, evitando o seu contato direto com possíveis impurezas presentes no ar do gás no interior do sistema o compressor pode ser considerado o ar condicionado automotivo em funcionamento os primeiros movimentos iria circular por acionamento funcionem sem que as superfícies do ar condicionado automotivo.

March 23, 2014, 04:00

Great post. I have enjoyed by read. Thanks

Leave a new comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type the first letter of your name: (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.

Advertisements