Class: Opal::CliRunners::Nodejs

Inherits:
Object
  • Object
show all
Defined in:
opal/lib/opal/cli_runners/nodejs.rb

Defined Under Namespace

Classes: MissingNodeJS

Constant Summary collapse

NODE_PATH =

We can't rely on Opal.gem_dir for now...

File.expand_path('../stdlib/nodejs/node_modules', ::Opal.gem_dir)
DIR =
__dir__

Class Method Summary collapse

Class Method Details

.call(data) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'opal/lib/opal/cli_runners/nodejs.rb', line 19

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

.node_modulesObject

Ensure stdlib node_modules is among NODE_PATHs



41
42
43
44
45
46
# File 'opal/lib/opal/cli_runners/nodejs.rb', line 41

def self.node_modules
  npsep = Gem.win_platform? ? ';' : ':'
  ENV['NODE_PATH'].to_s.split(npsep).tap do |paths|
    paths << NODE_PATH unless paths.include? NODE_PATH
  end.join(npsep)
end