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 exec


Node has a built-in library called child_process that allows us to execute shell commands using either the child_process.exec() function or the child_process.spawn() function.

In this section, you will learn how to execute shell commands using the exec() function in Node's child_process library.

Getting ready

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

How to do it...

In this example, we will execute the coffee command with the --help parameter to get the help text displayed by the CoffeeScript executable:

  1. Load the child_process library and grab its exec() function:

    exec = require('child_process').exec
    
  2. Execute CoffeeScript's coffee --help command, which will display the help text for CoffeeScript:

    exec 'coffee -help', (error, stdout, stderr) ->
    
  3. The following result will be displayed:

      if stdout
        console.log "Received #{stdout.length} bytes."
        console.log stdout
    
      if stderr
        console.error stderr
    

How it works...

The exec() function takes...