Sha256: 50779ccbc30968205fd60c8804045b7cc54c4cd615d30407de0f0a590bf0e28a

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

require 'shellwords'
require 'opal/paths'
require 'opal/cli_runners/system_runner'

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

      def self.call(data)
        (data[:options] ||= {})[:env] = { 'NODE_PATH' => node_modules }

        argv = data[:argv].dup.to_a
        argv.unshift('--') if argv.any?

        opts = Shellwords.shellwords(ENV['NODE_OPTS'] || '')

        SystemRunner.call(data) do |tempfile|
          [
            'node',
            '--require', "#{__dir__}/source-map-support-node",
            *opts,
            tempfile.path,
            *argv
          ]
        end
      rescue Errno::ENOENT
        raise MissingNodeJS, 'Please install Node.js to be able to run Opal scripts.'
      end

      # Ensure stdlib node_modules is among NODE_PATHs
      def self.node_modules
        ENV['NODE_PATH'].to_s.split(':').tap do |paths|
          paths << NODE_PATH unless paths.include? NODE_PATH
        end.join(':')
      end

      class MissingNodeJS < RunnerError
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
opal-1.3.0.alpha1 lib/opal/cli_runners/nodejs.rb
opal-1.2.0 lib/opal/cli_runners/nodejs.rb
opal-1.2.0.beta1 lib/opal/cli_runners/nodejs.rb