inspec/lib/resources/npm.rb

45 lines
814 B
Ruby
Raw Normal View History

# encoding: utf-8
2015-10-06 16:55:44 +00:00
# author: Christoph Hartmann
# author: Dominik Richter
# Usage:
# describe npm('bower') do
# it { should be_installed }
# end
2015-10-26 03:04:18 +00:00
class NpmPackage < Inspec.resource(1)
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-10-26 03:04:18 +00:00
cmd = inspec.command("npm ls -g --json #{@package_name}")
@info = {
name: @package_name,
type: 'npm',
installed: cmd.exit_status == 0,
}
return @info unless @info[:installed]
pkgs = JSON.parse(cmd.stdout)
@info[:version] = pkgs['dependencies'][@package_name]['version']
@info
end
def installed?
info[:installed] == true
end
def version
info[:version]
end
def to_s
"Npm Package #{@package_name}"
end
end