Skip to content

Instantly share code, notes, and snippets.

@plapier
Last active December 14, 2015 02:59
Show Gist options
  • Save plapier/5018143 to your computer and use it in GitHub Desktop.
Save plapier/5018143 to your computer and use it in GitHub Desktop.
Convert Javascript to CoffeeScript (vice versa) directly from vim. Uses js2coffee library.
#!/usr/bin/env ruby
require 'tempfile'
file = Tempfile.new(['js2coffee', '.js'])
file.write ARGF.read # write vim selection to tmp file
file.rewind
file.close
puts `js2coffee #{file.path}` # requires js2coffee
file.unlink # deletes the temp file
@unrealhoang
Copy link

@gabebw I tried your solution but still got Invalid Token due to the BOM header.
So I modified your solution a bit

#!/usr/bin/env ruby

input = ARGF.read.gsub("\xEF\xBB\xBF", '')
# requires js2coffee
output = `js2coffee <<EOJS\n#{input}\nEOJS`
puts output

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