Class: AppMap::NodeCLI

Inherits:
Object show all
Defined in:
lib/appmap/node_cli.rb

Overview

Utilities for invoking the @appland/appmap CLI.

Constant Summary collapse

APPMAP_JS =
'@appland/appmap'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verbose: false, appmap_dir: AppMap::DEFAULT_APPMAP_DIR) ⇒ NodeCLI

Returns a new instance of NodeCLI.



15
16
17
18
19
20
# File 'lib/appmap/node_cli.rb', line 15

def initialize(verbose: false, appmap_dir: AppMap::DEFAULT_APPMAP_DIR)
  @verbose = verbose
  @appmap_dir = appmap_dir

  detect_nodejs
end

Instance Attribute Details

#appmap_dirObject

Directory to scan for AppMaps.



13
14
15
# File 'lib/appmap/node_cli.rb', line 13

def appmap_dir
  @appmap_dir
end

#verboseObject (readonly)

Returns the value of attribute verbose.



11
12
13
# File 'lib/appmap/node_cli.rb', line 11

def verbose
  @verbose
end

Instance Method Details

#command(command, options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/appmap/node_cli.rb', line 32

def command(command, options = {})
  command.unshift << '--verbose' if verbose
  command.unshift APPMAP_JS
  command.unshift 'npx'

  warn command.join(' ') if verbose
  stdout, stderr, status = Open3.capture3({ 'NODE_OPTIONS' => '--trace-warnings' }, *command.map(&:shellescape), options)
  stdout_msg = stdout.split("\n").map {|line| "stdout: #{line}"}.join("\n") unless Util.blank?(stdout)
  stderr_msg = stderr.split("\n").map {|line| "stderr: #{line}"}.join("\n") unless Util.blank?(stderr)
  if verbose
    warn stdout_msg if stdout_msg
    warn stderr_msg if stderr_msg
  end
  unless status.exitstatus == 0
    raise CommandError.new(command, [ stdout_msg, stderr_msg ].compact.join("\n"))
  end
  [ stdout, stderr ]
end

#detect_nodejsObject



22
23
24
25
# File 'lib/appmap/node_cli.rb', line 22

def detect_nodejs
  do_fail('node', 'please install NodeJS') unless system('node --version 2>&1 > /dev/null')
  true
end

#index_appmapsObject



27
28
29
30
# File 'lib/appmap/node_cli.rb', line 27

def index_appmaps
  command [ 'index', '--appmap-dir', appmap_dir ]
  true
end