Class: Opal::CliRunners::Nodejs
- Defined in:
- opal/lib/opal/cli_runners/nodejs.rb
Defined Under Namespace
Classes: MissingNodeJS
Instance Attribute Summary collapse
-
#exit_status ⇒ Object
readonly
Returns the value of attribute exit_status.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Instance Method Summary collapse
-
#initialize(output) ⇒ Nodejs
constructor
A new instance of Nodejs.
- #node_modules ⇒ Object
- #puts(*args) ⇒ Object
- #run(code, argv) ⇒ Object
-
#system_with_output(env, *cmd) ⇒ Object
Let's support fake IO objects like StringIO.
Constructor Details
#initialize(output) ⇒ Nodejs
Returns a new instance of Nodejs
7 8 9 |
# File 'opal/lib/opal/cli_runners/nodejs.rb', line 7 def initialize(output) @output ||= output end |
Instance Attribute Details
#exit_status ⇒ Object (readonly)
Returns the value of attribute exit_status
10 11 12 |
# File 'opal/lib/opal/cli_runners/nodejs.rb', line 10 def exit_status @exit_status end |
#output ⇒ Object (readonly)
Returns the value of attribute output
10 11 12 |
# File 'opal/lib/opal/cli_runners/nodejs.rb', line 10 def output @output end |
Instance Method Details
#node_modules ⇒ Object
16 17 18 |
# File 'opal/lib/opal/cli_runners/nodejs.rb', line 16 def node_modules File.('../stdlib/nodejs/node_modules', ::Opal.gem_dir) end |
#puts(*args) ⇒ Object
12 13 14 |
# File 'opal/lib/opal/cli_runners/nodejs.rb', line 12 def puts(*args) output.puts(*args) end |
#run(code, argv) ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'opal/lib/opal/cli_runners/nodejs.rb', line 20 def run(code, argv) require 'tempfile' tempfile = Tempfile.new('opal-nodejs-runner-') # tempfile = File.new('opal-nodejs-runner.js', 'w') # for debugging tempfile.write code tempfile.close system_with_output({'NODE_PATH' => node_modules}, 'node', tempfile.path , *argv) rescue Errno::ENOENT raise MissingNodeJS, 'Please install Node.js to be able to run Opal scripts.' end |
#system_with_output(env, *cmd) ⇒ Object
Let's support fake IO objects like StringIO
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'opal/lib/opal/cli_runners/nodejs.rb', line 32 def system_with_output(env, *cmd) if IO.try_convert(output) system(env,*cmd) @exit_status = $?.exitstatus return end if RUBY_PLATFORM == 'java' # JRuby has issues in dealing with subprocesses (at least up to 1.7.15) # @headius told me it's mostly fixed on master, but while we wait for it # to ship here's a tempfile workaround. require 'tempfile' require 'shellwords' tempfile = Tempfile.new('opal-node-output') system(env,cmd.shelljoin+" > #{tempfile.path}") @exit_status = $?.exitstatus captured_output = File.read tempfile.path tempfile.close else require 'open3' captured_output, status = Open3.capture2(env,*cmd) @exit_status = status.exitstatus end output.write captured_output end |