2015-09-08 22:12:25 +00:00
|
|
|
# encoding: utf-8
|
2015-10-06 16:55:44 +00:00
|
|
|
# author: Christoph Hartmann
|
|
|
|
# author: Dominik Richter
|
2015-09-08 22:12:25 +00:00
|
|
|
|
|
|
|
# Usage:
|
|
|
|
# describe npm('bower') do
|
|
|
|
# it { should be_installed }
|
|
|
|
# end
|
2015-10-26 03:04:18 +00:00
|
|
|
class NpmPackage < Inspec.resource(1)
|
2015-09-08 22:12:25 +00:00
|
|
|
name 'npm'
|
|
|
|
|
|
|
|
def initialize(package_name)
|
|
|
|
@package_name = package_name
|
|
|
|
@cache = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def info
|
2015-10-01 13:25:08 +00:00
|
|
|
return @info if defined?(@info)
|
2015-09-17 14:43:10 +00:00
|
|
|
|
2015-10-26 03:04:18 +00:00
|
|
|
cmd = inspec.command("npm ls -g --json #{@package_name}")
|
2015-09-17 14:43:10 +00:00
|
|
|
@info = {
|
2015-09-08 22:12:25 +00:00
|
|
|
name: @package_name,
|
2015-09-09 16:52:27 +00:00
|
|
|
type: 'npm',
|
2015-09-17 18:35:14 +00:00
|
|
|
installed: cmd.exit_status == 0,
|
2015-09-08 22:12:25 +00:00
|
|
|
}
|
2015-09-17 14:43:10 +00:00
|
|
|
return @info unless @info[:installed]
|
|
|
|
|
|
|
|
pkgs = JSON.parse(cmd.stdout)
|
2015-09-17 18:35:14 +00:00
|
|
|
@info[:version] = pkgs['dependencies'][@package_name]['version']
|
|
|
|
@info
|
2015-09-08 22:12:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def installed?
|
2015-09-17 18:51:06 +00:00
|
|
|
info[:installed] == true
|
2015-09-08 22:12:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def version
|
2015-09-17 14:43:10 +00:00
|
|
|
info[:version]
|
2015-09-08 22:12:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
2015-10-12 11:01:58 +00:00
|
|
|
"Npm Package #{@package_name}"
|
2015-09-08 22:12:25 +00:00
|
|
|
end
|
|
|
|
end
|