Class: BrowserifyRb::Nvm

Inherits:
Object
  • Object
show all
Defined in:
lib/browserify_rb/nvm.rb

Constant Summary collapse

LOG =
Logger.new(STDERR)
NVM_SH =
File.join(__dir__, "nvm.sh")

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nvm_dir = "#{ENV["HOME"]}/.nvm") ⇒ Nvm

Returns a new instance of Nvm.



13
14
15
# File 'lib/browserify_rb/nvm.rb', line 13

def initialize nvm_dir = "#{ENV["HOME"]}/.nvm"
  @nvm_dir = nvm_dir
end

Class Method Details

.versionObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/browserify_rb/nvm.rb', line 50

def self.version
  out = StringIO.new
  err = StringIO.new
  cmd = "    . \"\#{NVM_SH}\" || {\n      printf \"Abort\\\\n\" >&2\n      exit 1\n    }\n    nvm --version\n  CMD\n\n  LOG.debug \"run: \#{cmd}\"\n  st = BrowserifyRb::Popen3.async_exec(\n    cmd,\n    stdout_handler: proc {|d| out << d },\n    stderr_handler: proc {|d| err << d }\n  ).value\n  raise \"non-zero exit status: \#{status.to_i}\\n\#{err}\" unless st.success?\n\n  out.string.strip\nend\n"

Instance Method Details

#envObject



17
18
19
# File 'lib/browserify_rb/nvm.rb', line 17

def env
  { "NVM_DIR" => @nvm_dir }
end

#run(cmd, stdin: "", stdout_handler: proc{|d| STDOUT.print d }, stderr_handler: proc{|d| STDERR.print d }, node_ver: "stable", env: {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/browserify_rb/nvm.rb', line 21

def run(
      cmd,
      stdin: "",
      stdout_handler: proc{|d| STDOUT.print d },
      stderr_handler: proc{|d| STDERR.print d },
      node_ver: "stable",
      env: {})
  new_env = (env).merge(self.env)
  cmd = "    . \"\#{NVM_SH}\" || {\n      printf \"Abort\\\\n\" >&2\n      exit 1\n    }\n    if ! nvm use \"\#{node_ver}\" >&2; then\n      nvm install \"\#{node_ver}\" >&2\n      nvm use \"\#{node_ver}\" >&2\n    fi\n    \#{cmd}\n  CMD\n\n  LOG.debug \"run: \#{cmd}\"\n  BrowserifyRb::Popen3.async_exec(\n    cmd,\n    input: stdin, env: new_env,\n    stdout_handler: stdout_handler,\n    stderr_handler: stderr_handler\n  )\nend\n"