Module: Opal::CliRunners

Defined in:
opal/lib/opal/cli_runners.rb,
opal/lib/opal/cli_runners/gjs.rb,
opal/lib/opal/cli_runners/chrome.rb,
opal/lib/opal/cli_runners/nodejs.rb,
opal/lib/opal/cli_runners/server.rb,
opal/lib/opal/cli_runners/nashorn.rb,
opal/lib/opal/cli_runners/quickjs.rb,
opal/lib/opal/cli_runners/mini_racer.rb,
opal/lib/opal/cli_runners/applescript.rb

Overview

Opal::CliRunners is the register in which JavaScript runners can be defined for use by Opal::CLI. Runners will be called via the #call method and passed a Hash containing the following keys:

  • options: a hash of options for the runner
  • output: an IO-like object responding to #write and #puts
  • argv: is the arguments vector coming from the CLI that is being forwarded to the program
  • builder: a proc returning a new instance of Opal::Builder so it can be re-created and pick up the most up-to-date sources

Runners can be registered using #register_runner(name, runner).

Defined Under Namespace

Classes: Applescript, Chrome, Gjs, MiniRacer, Nashorn, Nodejs, Quickjs, RunnerError, Server

Constant Summary collapse

Compiler =

The compiler runner will just output the compiled JavaScript

->(data) {
  options  = data[:options] || {}
  builder  = data.fetch(:builder).call
  map_file = options[:map_file]
  output   = data.fetch(:output)

  compiled_source = builder.to_s
  compiled_source += "\n" + builder.source_map.to_data_uri_comment unless options[:no_source_map]
  output.puts compiled_source
  File.write(map_file, builder.source_map.to_json) if map_file

  0
}

Class Method Summary collapse

Class Method Details

.[](name) ⇒ Object



24
25
26
# File 'opal/lib/opal/cli_runners.rb', line 24

def self.[](name)
  @register[name.to_sym]&.call
end

.[]=(name, runner) ⇒ Object



29
30
31
32
33
# File 'opal/lib/opal/cli_runners.rb', line 29

def self.[]=(name, runner)
  warn "Overwriting Opal CLI runner: #{name}" if @register.key? name.to_sym

  @register[name.to_sym] = runner
end

.alias_runner(new_name, old_name) ⇒ Object

Alias a runner name



58
59
60
61
62
# File 'opal/lib/opal/cli_runners.rb', line 58

def self.alias_runner(new_name, old_name)
  self[new_name.to_sym] = -> { self[old_name.to_sym] }

  nil
end

.register_runner(name, runner, path = nil) ⇒ Object

Parameters:

  • name (Symbol)

    the name at which the runner can be reached

  • runner (#call)

    a callable object that will act as the "runner"

  • runner (Symbol)

    a constant name that once autoloaded will point to a callable.

  • path (nil, String) (defaults to: nil)

    a path for setting up autoload on the constant



45
46
47
48
49
50
51
52
53
54
55
# File 'opal/lib/opal/cli_runners.rb', line 45

def self.register_runner(name, runner, path = nil)
  autoload runner, path if path

  if runner.respond_to? :call
    self[name] = -> { runner }
  else
    self[name] = -> { const_get(runner) }
  end

  nil
end

.to_hObject



36
37
38
# File 'opal/lib/opal/cli_runners.rb', line 36

def self.to_h
  @register
end