Skip to content

Instantly share code, notes, and snippets.

@EtienneLem
Created March 7, 2012 15:43
Show Gist options
  • Save EtienneLem/1993872 to your computer and use it in GitHub Desktop.
Save EtienneLem/1993872 to your computer and use it in GitHub Desktop.
Compile CoffeeScript to JS
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>beforeRunningCommand</key>
<string>saveActiveFile</string>
<key>command</key>
<string>#!/usr/bin/env ruby
# encoding: utf-8
in_assets_folder = ENV['TM_FILEPATH'].match(/assets\//)
in_src_folder = ENV['TM_FILEPATH'].match(/src\/(.)+\.coffee/)
# Automagically compile .coffee to .js if:
# · Not in `assets/` folder
# · Not direct child of `src/`
if !in_assets_folder &amp;&amp; !in_src_folder
error = `coffee -c #{ENV['TM_FILEPATH']} 2&gt;&amp;1`
if error == ''
puts "✓ compiled #{ENV['TM_FILEPATH'].sub(/\.coffee$/, '.js')}"
else
puts "✗ #{error}"
end
# Automagically compile `src/abc.coffee` to `lib/abc.js` if:
# · Is a direct child of `src/`
elsif in_src_folder
error = `coffee -co ../lib #{ENV['TM_FILEPATH']} 2&gt;&amp;1`
if error == ''
puts "✓ compiled to #{ENV['TM_FILEPATH'].sub(/\.coffee$/, '.js').sub(/src/, 'lib')}"
else
puts "✗ #{error}"
end
end</string>
<key>input</key>
<string>none</string>
<key>keyEquivalent</key>
<string>@s</string>
<key>name</key>
<string>Compile CoffeeScript to JS</string>
<key>output</key>
<string>showAsTooltip</string>
<key>scope</key>
<string>source.coffee</string>
<key>uuid</key>
<string>CC66EEF5-C60D-4EDC-AE88-500095838FE7</string>
</dict>
</plist>
@lpdumas
Copy link

lpdumas commented Apr 20, 2012

Nice! TY

@rafbm
Copy link

rafbm commented Apr 20, 2012

Précision… c’est qu’en Ruby, les backticks (`) servent à retourner la valeur de standard output. Mais dans le script, on veut utiliser standard error, donc la façon de le retourner c’est en le redirigeant vers standard output.

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