Skip to content

Instantly share code, notes, and snippets.

@ordinaryzelig
Last active December 15, 2015 13:18
Show Gist options
  • Save ordinaryzelig/5266342 to your computer and use it in GitHub Desktop.
Save ordinaryzelig/5266342 to your computer and use it in GitHub Desktop.

I'm starting a JavaScript project using the Jasmine Ruby gem for testing, and I only want to use CoffeeScript. After looking around to see how other projects did it, I didn't find any project with a "pure" CoffeeScript solution. The projects I saw only had the source code written in CoffeeScript but tests written in JavaScript. I wanted all my code including tests to be written in CoffeeScript.

This is what the basic directory tree looks like in my project:

~models
  file1.coffee
  file2.coffee
~spec
  ~models
    file1Spec.coffee
    file2Spec.coffee

What I want is to have an off-to-the-side .compiledJS directory to store the compiled JavaScript files. And I also want to preserve the directory structure — i.e. spec/models/file1Spec.coffee would compile and be stored as .compiledJS/spec/models/file1spec.js. Having this structure would allow me to use the Jasmine Ruby gem without much of a fuss. I would only need to change the default directories in jasmine.yml.

I'm using the guard-coffeescript Ruby gem to watch and compile my *.coffee files whenever they are saved. It also preserves the directory structure just like I wanted. Here's my guard-coffeescript config:

guard 'coffeescript', {
  :output => '.compiledJS',
  :bare =>   true
} do
  watch %r{(.*/(.+\.coffee))}
end

The trick is not using the typical :input option and setting up a custom watcher. The regular expression passed to watch() specifies that I want to watch any .coffee file in the ENTIRE project. The 2 sets of parentheses are VERY important in preserving the directory structure.

Now I have a "pure" CoffeeScript project with a nice and neat directory structure and an off-to-the-side .compiledJS directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment