Last active
April 6, 2021 21:38
-
-
Save jaredcwhite/08025f84278ebe459ddc7a6b81e88900 to your computer and use it in GitHub Desktop.
Easiest way to run a Node script from Ruby?!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "json" | |
def execjs(js) | |
IO.popen(["node", "--input-type=module"], "r+") do |pipe| | |
pipe.puts js | |
pipe.close_write | |
pipe.read | |
end | |
end | |
greeting = "Hello Ruby!" | |
output = execjs <<~JS | |
console.info(#{greeting.to_json}) | |
JS | |
if output.strip.length > 0 | |
puts output # Hello Ruby! | |
else | |
puts "Runtime error in Node script, aborted." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment