CoffeeScript for Command-line Apps
Updated: 2011-10-27: published the app to github and the npm registry.
This post details my learning experience porting a command-line application I previously wrote in Python to CoffeeScript.
I won’t describe how the SMS application works or how to use it, you can find this in a separate post describing the Python version.
The clisms.coffee source code is on github, the shebang line allows it to be executed directly (provided you have the coffee compiler installed), or you could compile it separately to JavaScript (using the cake build command). The program does the typical things most command-line apps do: process command-line args; read environment variables; read/write files; shell other apps.
Learning and using CoffeeScript
CoffeeScript is surprisingly well documented for such a new language. If you know Python, Ruby and JavaScript you’ll learn it in a couple of hours. Here are resources I found useful:
- CoffeeScript website.
- Watch this video.
- Wikipedia: http://en.wikipedia.org/wiki/CoffeeScript
- A list of CoffeeScript resources.
- CoffeeScript wiki.
- Smooth CoffeeScript is a free CoffeeScript book.
- CoffeeScript Cookbook.
- I also found the CoffeeScript book from the Pragmatic Bookshelf a good read.
Libraries can make or break scripting languages. CoffeeScript uses JavaScript libraries so you’ll need to be familiar with JavaScript. The foremost client-side library is the NodeJS API. I was able to write clisms.coffee using just the NodeJS API. The NodeJS API is reasonably well documented though on occasion you may find it necessary to consult the NodeJS source.
- NodeJS wiki.
- NodeJS API documentation.
- NodeJS library source.
- The npm registry is a great source of other Node compatible JavaScript libraries.
I found the hardest part was not CoffeeScript itself but getting used to the NodeJS asynchronous I/O paradigm – once you’ve twigged it’s not much harder to write or understand it’s just different.
Impressions of CoffeeScript
The port from Python went smoothly, much of the time being spent hunting around for equivalent JavaScript library functions.
I would characterise CoffeeScript as having borrowed the best features of Python and Ruby to synthesise an entirely new language that compiles to JavaScript and retains JavaScript semantics.
I’m quite smitten by CoffeeScript, it’s incredibly expressive and has that ineffable “just right” feel about it, no rough edges, no surprises, everything just works – hats off to Jeremy Ashkenas, CoffeeScript’s creator.
Unlike JavaScript, which I’ve only ever coded out of necessity, writing in CoffeeScript is a pleasurable experience. Would I recommend CoffeeScript as a general purpose scripting language? Absolutely, especially with the recent surge in development of JavaScript libraries.