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
2015-10-26 03:04:18 +00:00
class NpmPackage < Inspec . resource ( 1 )
2015-09-08 22:12:25 +00:00
name 'npm'
2015-11-27 13:02:38 +00:00
desc 'Use the npm InSpec audit resource to test if a global npm package is installed. npm is the the package manager for Nodejs packages, such as bower and StatsD.'
example "
describe npm ( 'bower' ) do
it { should be_installed }
end
"
2015-09-08 22:12:25 +00:00
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