Module: Spider::Node

Defined in:
lib/spider-node.rb,
lib/spider-node/version.rb,
lib/spider-node/compile_result.rb

Defined Under Namespace

Classes: CompileResult

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.check_nodeObject



83
84
85
86
87
88
# File 'lib/spider-node.rb', line 83

def check_node
  unless locate_executable(node)
    raise "spider-node requires node command, but it's not found. Please install it. " +
        "Set TS_NODE environmental variable If you want to use node command in non-standard path."
  end
end

.compile(source, *spiderc_options) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/spider-node.rb', line 46

def compile(source, *spiderc_options)
  js_file = Tempfile.new(["spider-node", ".spider"])
  begin
    js_file.write(source)
    js_file.close
    result = compile_file(js_file.path, *spiderc_options)

    if result.success?
      result.js
    else
      raise result.stderr
    end
  ensure
    js_file.unlink
  end
end

.compile_file(source_file, *spiderc_options) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/spider-node.rb', line 26

def compile_file(source_file, *spiderc_options)
  
  target = File.basename source_file, '.spider'
  target_dir = File.dirname source_file

  Dir.mktmpdir do |output_dir|
    output_file = File.join(target_dir, target + '.js')
    exit_status, stdout, stderr = spiderc(*spiderc_options, '-c', source_file)

    output_js = File.exists?(output_file) ? File.read(output_file) : nil

    CompileResult.new(
        output_js,
        exit_status,
        stdout,
        stderr,
    )
  end
end

.locate_executable(cmd) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/spider-node.rb', line 67

def locate_executable(cmd)
  if RbConfig::CONFIG["host_os"] =~ /mswin|mingw/ && File.extname(cmd) == ""
    cmd << ".exe"
  end

  if File.executable? cmd
    cmd
  else
    path = ENV['PATH'].split(File::PATH_SEPARATOR).find { |p|
      full_path = File.join(p, cmd)
      File.executable?(full_path) && File.file?(full_path)
    }
    path && File.expand_path(cmd, path)
  end
end

.nodeObject



63
64
65
# File 'lib/spider-node.rb', line 63

def node
   ENV["TS_NODE"] || "node"
end

.spider_versionObject



12
13
14
# File 'lib/spider-node.rb', line 12

def spider_version
  Spider::Src.version
end

.spiderc(*args) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/spider-node.rb', line 17

def spiderc(*args)
  cmd = [node, Spider::Src.js_path.to_s, *args]

  Open3.popen3(*cmd) do |stdin, stdout, stderr, wait_thr|
    stdin.close
    [wait_thr.value, stdout.read, stderr.read]
  end
end