Class: Fontico::NodeRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/fontico/node_runner.rb

Overview

Font assembly needs a toolchain the sprite does not. It is installed on demand into a cache directory and never touched unless a font target is actually built, so targets: [sprite] stays pure Ruby with no node at all.

Defined Under Namespace

Classes: MissingNode, ScriptFailed

Constant Summary collapse

SCRIPTS =
File.expand_path("node", __dir__)

Instance Method Summary collapse

Constructor Details

#initialize(cache_dir: nil) ⇒ NodeRunner

Returns a new instance of NodeRunner.



17
18
19
# File 'lib/fontico/node_runner.rb', line 17

def initialize(cache_dir: nil)
  @cache = cache_dir || File.join(Dir.home, ".cache", "fontico", "toolchain")
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


21
# File 'lib/fontico/node_runner.rb', line 21

def available? = !which("node").nil?

#run(script, payload) ⇒ Object

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fontico/node_runner.rb', line 23

def run(script, payload)
  ensure_toolchain!
  # Run from inside the cache: ESM resolves bare imports by walking up
  # from the script's own directory, and ignores NODE_PATH entirely.
  path = File.join(@cache, script)

  out, err, status = Open3.capture3(
    which("node"), path, stdin_data: JSON.dump(payload), chdir: @cache
  )
  raise ScriptFailed, "#{script}: #{err.strip.empty? ? "exited #{status.exitstatus}" : err.strip}" unless status.success?

  JSON.parse(out)
end