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
- .check_node ⇒ Object
- .compile(source, *spiderc_options) ⇒ Object
- .compile_file(source_file, *spiderc_options) ⇒ Object
- .locate_executable(cmd) ⇒ Object
- .node ⇒ Object
- .spider_version ⇒ Object
- .spiderc(*args) ⇒ Object
Class Method Details
.check_node ⇒ Object
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, *) js_file = Tempfile.new(["spider-node", ".spider"]) begin js_file.write(source) js_file.close result = compile_file(js_file.path, *) 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, *) 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(*, '-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.(cmd, path) end end |
.node ⇒ Object
63 64 65 |
# File 'lib/spider-node.rb', line 63 def node ENV["TS_NODE"] || "node" end |
.spider_version ⇒ Object
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 |