Book Image

CoffeeScript Application Development Cookbook

By : Mike Hatfield
Book Image

CoffeeScript Application Development Cookbook

By: Mike Hatfield

Overview of this book

Table of Contents (18 chapters)
CoffeeScript Application Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Executing shell commands with spawn


The spawn() function of the child_process library is very similar to the exec() function, except that instead of returning buffers, it returns a stream. Streams are extremely handy for certain circumstances. For example, what if we wanted to take the output of one command and send that as the input for a second command? The spawn() function can help with this.

Getting ready

In our example, we will use spawn to execute a simple CoffeeScript statement and retrieve the results.

We will be using the native Node library for this example.

How to do it...

In this example, we will demonstrate the use of spawn() to execute a CoffeeScript statement:

  1. Begin by loading the child_process library and grabbing its spawn() function:

    spawn = require('child_process').spawn
    
  2. Define our CoffeeScript statement:

    coffeeCode = 'console.log "The answer to life is #{6 * 7}"'
    
  3. In our example, we want two processes: echo and coffee. The echo process will place our CoffeeScript statement into...