Update nodejs cookbook
This commit is contained in:
@@ -14,20 +14,31 @@ module NodeJs
|
||||
end
|
||||
end
|
||||
|
||||
def npm_list(path = nil)
|
||||
def npm_list(path = nil, environment = {})
|
||||
require 'json'
|
||||
if path
|
||||
cmd = Mixlib::ShellOut.new('npm list -json', :cwd => path)
|
||||
cmd = Mixlib::ShellOut.new('npm list -json', :cwd => path, :environment => environment)
|
||||
else
|
||||
cmd = Mixlib::ShellOut.new('npm list -global -json')
|
||||
cmd = Mixlib::ShellOut.new('npm list -global -json', :environment => environment)
|
||||
end
|
||||
|
||||
JSON.parse(cmd.run_command.stdout, :max_nesting => false)
|
||||
end
|
||||
|
||||
def npm_package_installed?(package, version = nil, path = nil)
|
||||
list = npm_list(path)['dependencies']
|
||||
def url_valid?(list, package)
|
||||
list.fetch(package, {}).fetch('resolved', '').include?('url')
|
||||
end
|
||||
|
||||
def version_valid?(list, package, version)
|
||||
(version ? list[package]['version'] == version : true)
|
||||
end
|
||||
|
||||
def npm_package_installed?(package, version = nil, path = nil, npm_token = nil)
|
||||
environment = { 'NPM_TOKEN' => npm_token } if npm_token
|
||||
|
||||
list = npm_list(path, environment)['dependencies']
|
||||
# Return true if package installed and installed to good version
|
||||
(!list.nil?) && list.key?(package) && (version ? list[package]['version'] == version : true)
|
||||
(!list.nil?) && list.key?(package) && version_valid?(list, package, version) && url_valid?(list, package)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user