How To Install CoffeeScript On Mac OS X

If you are a Rails developer, you should know that Rails 3.1 beta has been released today. I am trying out the beta version so I don’t have to stress about migrating or upgrading later.

Install CofeeScript Using Homebrew

Install homebrew if you haven’t yet. It’s a lot better than fink. Macports didn’t work well for me too. I use homebrew.

  # update home brew (recommended)
  brew update

  brew install node
  
  Caveats:
  
  Please add /usr/local/lib/node to your NODE_PATH environment variable to have node libraries picked up.

NPM should be available. Otherwise, please try this:

curl https://npmjs.org/install.sh | sh

To install coffeescript, you will need to execute this command:

  npm install -g coffee-script

This is the output after install node.

==> Downloading http://nodejs.org/dist/node-v0.4.7.tar.gz
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/node/0.4.7
==> make install
==> Caveats
Please add /usr/local/lib/node to your NODE_PATH environment variable to have node libraries picked up.
==> Summary
/usr/local/Cellar/node/0.4.7: 72 files, 7.5M, built in 3.1 minutes

Check if everything is working:

  node -v
  npm -v
  coffee -v

Test CoffeeScript

Create a file called test.coffee

# Assignment:
number   = 42
opposite = true

# Conditions:
number = -42 if opposite

# Functions:
square = (x) -> x * x

# Arrays:
list = [1, 2, 3, 4, 5]

# Objects:
math =
  root:   Math.sqrt
  square: square
  cube:   (x) -> x * square x

# Splats:
race = (winner, runners...) ->
  print winner, runners

# Existence:
alert "I knew it!" if elvis?

# Array comprehensions:
cubes = (math.cube num for num in list)

Then run “coffee -c test.coffee”. If you see a test.js file on the same directory then it’s all good.

CoffeeScript for Textmate

  cd ~/Library/Application\ Support/TextMate/Bundles
  git clone git://github.com/jashkenas/coffee-script-tmbundle CoffeeScript.tmbundle

Debian or Ubuntu

If you use Debian or Ubuntu for production, you will find this link useful.