Sha256: 9187b20de828403ca3e631f2f2ade7f5e7c7ffd6949a711697f66775e1103433

Contents?: true

Size: 1.86 KB

Versions: 12

Compression:

Stored size: 1.86 KB

Contents

require 'opal/cli_runners'
require 'opal/paths'

module Opal
  module CliRunners
    class Nodejs
      NODE_PATH = File.expand_path('../stdlib/nodejs/node_modules', ::Opal.gem_dir)

      def initialize(options)
        @output = options.fetch(:output, $stdout)
      end
      attr_reader :output, :exit_status

      def puts(*args)
        output.puts(*args)
      end

      def node_modules
        NODE_PATH
      end

      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

      # Let's support fake IO objects like StringIO
      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

      class MissingNodeJS < RunnerError
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
opal-0.10.6 lib/opal/cli_runners/nodejs.rb
opal-0.10.6.beta lib/opal/cli_runners/nodejs.rb
opal-0.10.5 lib/opal/cli_runners/nodejs.rb
opal-0.10.4 lib/opal/cli_runners/nodejs.rb
opal-0.10.3 lib/opal/cli_runners/nodejs.rb
opal-0.10.2 lib/opal/cli_runners/nodejs.rb
opal-0.10.1 lib/opal/cli_runners/nodejs.rb
opal-0.10.0 lib/opal/cli_runners/nodejs.rb
opal-0.10.0.rc2 lib/opal/cli_runners/nodejs.rb
opal-0.10.0.rc1 lib/opal/cli_runners/nodejs.rb
opal-0.10.0.beta5 lib/opal/cli_runners/nodejs.rb
opal-0.10.0.beta4 lib/opal/cli_runners/nodejs.rb