Class: Inspec::Resources::NpmPackage

Inherits:
Object
  • Object
show all
Defined in:
lib/resources/npm.rb

Instance Method Summary collapse

Constructor Details

#initialize(package_name) ⇒ NpmPackage

Returns a new instance of NpmPackage.



15
16
17
18
# File 'lib/resources/npm.rb', line 15

def initialize(package_name)
  @package_name = package_name
  @cache = nil
end

Instance Method Details

#infoObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/resources/npm.rb', line 20

def info
  return @info if defined?(@info)

  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

#installed?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/resources/npm.rb', line 36

def installed?
  info[:installed] == true
end

#to_sObject



44
45
46
# File 'lib/resources/npm.rb', line 44

def to_s
  "Npm Package #{@package_name}"
end

#versionObject



40
41
42
# File 'lib/resources/npm.rb', line 40

def version
  info[:version]
end