Monday, October 27, 2014

How To Create a CLI Program Using Coffee-Script

I'll show you how to create a command line program with coffee-script.

First of all, you'll need node.js - if you don't have it installed, then head over to http://nodejs.org/ and install it. You will also need coffee-script:
$ sudo npm install coffee-script -g

We will call our application arg,  so let's create the project directory:

$ mkdir arg
$ cd arg
$ mkdir bin
$ npm init

The npm init command will ask you for your project info - for this demo, you can just hit enter for each prompt. This creates the file package.json, which we will edit, adding:


  "bin": {
    "arg": "./bin/arg"
  },


Next create create the file ./bin/arg (no extension) with the following lines

#!/usr/bin/env coffee
http = require('http')
url = "http://postlikeapirate.com/AJAXtranslate.php?typing="

http.get url+escape(process.argv.slice(2).join(' ')), (res) ->
  res.on 'data', (chunk) ->
    console.log String(chunk)
  .on 'error', (err) ->
    console.log err
 
Finally, mark it as executable and install:

$ chmod a+x ./bin/arg
$ sudo npm install . -g

Now enter

$ arg I like coffee-script
I like spiced rum-script

Spiced Rum-Script? Hmmm...

Arg follow 'tis link to github to download th' gist







No comments:

Post a Comment