Sublime Text 2 Build System Scripts: CoffeeScript & Node

A really great feature of Sublime Text 2 is the ability to create your own build scripts. A build script is helpful when you are writing in a language that needs to be compiled or executed from terminal / command line. I personally use build scripts to compile the current file into CoffeeScript as well as run the current file in Node JS.

Watch the tutorial or continue reading to learn how to make Sublime Text 2 build scripts.

Creating a Build Script

To get started, go to Tools->Build System -> New Build System... and you will be presented with the boilerplate for a build script.

We have four lines to feed into our .sublime-build file:

cmd is an array of commends that will be run.

selector is an optional way to scope the build script to a specific filetype

path is where the command is to be executed. Because sublime executes it in the python console, we need to specify where on our system the compliler we want to use lies.

file_regex specifies where the error lines are and what to be returned.

For a complete list of all the variables available within a build script, check the variable reference.

CoffeeScript Build Script

So, to create our CoffeeScript build file, we simply fill in the following three lines. Keep in mind that with the Node.js coffeescript compiler we would normally execute something similar to this in the terminal:

Now every time we hit CMD + B our coffeescript file will be compiled with the build script. If you want to watch the file for changes after the first compile, simply switch the first line to "cmd": ["coffee","-wc", "$file"],. If anyone is using my CoffeeScript-Growl plugin, change your first line to "cmd": ["coffee", "-r", "coffeescript-growl", "-wc", "$file"], to get growl notifications.

Node.js Files

Another use for Sublime Text build scripts is launching the nodejs file you are currently working on. Our build script now looks something like this:

Thats is!

Pretty simple, eh? There is a lot of room for expanding these builds and making them do a lot of the manual work for you. If you have anything to add, please post it in the comments and I’ll be sure to add it to the post.

And in case anyone is wondering, to compile the JS output to another folder, you can do this:
{
"cmd": ["coffee", "-wc", "-o", "../js", "$file_path" ],
"selector": "source.coffee",
"path": "/usr/local/bin"
}

Any suggestions on how to do this with Sass? I am going to give it a shot but thought I would ask for suggestions while I work on testing making a build instead of always having to do it from the command line.