Skip to content

Instantly share code, notes, and snippets.

@rajiteh
Created January 12, 2015 13:25
Show Gist options
  • Save rajiteh/c07db70c264b11a19498 to your computer and use it in GitHub Desktop.
Save rajiteh/c07db70c264b11a19498 to your computer and use it in GitHub Desktop.
Platform independent method to detect a working nodejs binary in the system via a ruby script. (C) MIT
class DetectNode
MIN_VER = [0, 10, 0]
def self.detect
node_bins.each do |node|
detected_version = (`#{node} --version`)[1..-1].split(".") rescue [0,0,0]
if detected_version[0].to_i >= MIN_VER[0] and
detected_version[1].to_i >= MIN_VER[1] and
detected_version[2].to_i >= MIN_VER[2]
return node
end
end
raise "Could not find a nodejs version equal or higher to v#{MIN_VER.join(".")}"
end
private
def self.node_bins
node_binaries = [ 'node', 'nodejs' ]
detected = []
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
exts.each { |ext|
node_binaries.each do |cmd|
bin = File.join(path, "#{cmd}#{ext}")
detected << bin if File.executable?(bin) && !File.directory?(bin)
end
}
end
return detected
end
end
@lovesitdoll
Copy link

Slightly different nice tkw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment