Skip to content

Instantly share code, notes, and snippets.

@ralphschindler
Created October 6, 2013 01:31
Show Gist options
  • Save ralphschindler/6848232 to your computer and use it in GitHub Desktop.
Save ralphschindler/6848232 to your computer and use it in GitHub Desktop.
Running unsaved JavaScript though Node.js with TextMate 2 Notes: It's basically 2 files, one created with the bundle editor (See the tmCommand), and the actual node runner. This also requires an environment variable to be setup pointing to the node command line utility: TM_NODE=/usr/local/bin/node for node installed via homebrew
require "#{ENV["TM_SUPPORT_PATH"]}/lib/scriptmate"
class NodeScript < UserScript
def lang; "JavaScript" end
def default_extension; ".js" end
def args
[]
end
def executable; @hashbang || ENV['TM_NODE'] || 'node' end
def version_string
path = ENV['PATH'].split(':').find { |e| File.exists? File.join(e, executable) }
node_path = File.join(path.to_s, executable)
res = %x{ #{executable} -v }.split[0..2].join %{ }
res + " (#{node_path})"
end
end
class NodeMate < ScriptMate
def filter_stderr(str)
# strings from stderr are passed through this method before printing
super(str).
gsub(/(#\d+ )((.+?)\((\d+)\)): /) {
$1 + '<a href="txmt://open?' + (ENV.has_key?('TM_FILEPATH') ? "url=file://#{$3}&amp;" : '') + 'line=' + $4 + '"> ' + $2 + '</a>: '
}.
gsub(/in (.+?) on line (\d+)(<br>$)?/) {
'in <a href="txmt://open?' + (ENV.has_key?('TM_FILEPATH') ? "url=file://#{$1}&amp;" : '') + 'line=' + $2 + '"> ' + $1 + ' on line ' + $2 + '</a>' + $3.to_s
}
end
def filter_stdout(str)
# strings from stderr are passed through this method before printing
super(str).gsub(/\[([^\]]+?) line (\d+)\]<br>$/) {
'[<a href="txmt://open?' + (ENV.has_key?('TM_FILEPATH') ? "url=file://#{$1}&amp;" : '') + 'line=' + $2 + '">' + $1 + ' line ' + $2 + '</a>]<br>'
}
end
end
script = NodeScript.new(STDIN.read)
NodeMate.new(script).emit_html
<!--
The script is really just this (much like the PHP runner), but the
#!/bin/sh
/usr/bin/env ruby -- "$TM_BUNDLE_SUPPORT/lib/node_mate.rb"
-->
<?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>nop</string>
<key>command</key>
<string>#!/bin/sh
/usr/bin/env ruby -- "$TM_BUNDLE_SUPPORT/lib/node_mate.rb"
</string>
<key>input</key>
<string>selection</string>
<key>inputFormat</key>
<string>text</string>
<key>keyEquivalent</key>
<string>@R</string>
<key>name</key>
<string>Run In Node.js</string>
<key>outputCaret</key>
<string>afterOutput</string>
<key>outputFormat</key>
<string>html</string>
<key>outputLocation</key>
<string>newWindow</string>
<key>scope</key>
<string>source.js</string>
<key>uuid</key>
<string>1DBDEAB4-E18D-4FAD-AC13-318ABEACA3B3</string>
<key>version</key>
<integer>2</integer>
</dict>
</plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment