Class: Inspec::Resources::NpmPackage

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

Instance Method Summary collapse

Constructor Details

#initialize(package_name, opts = {}) ⇒ NpmPackage

Returns a new instance of NpmPackage.



21
22
23
24
25
# File 'lib/resources/npm.rb', line 21

def initialize(package_name, opts = {})
  @package_name = package_name
  @location = opts[:path]
  @cache = nil
end

Instance Method Details

#infoObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/resources/npm.rb', line 27

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

  if @location
    npm = "cd #{Shellwords.escape @location} && npm"
  else
    npm = 'npm -g'
  end

  cmd = inspec.command("#{npm} ls --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)


49
50
51
# File 'lib/resources/npm.rb', line 49

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

#to_sObject



57
58
59
# File 'lib/resources/npm.rb', line 57

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

#versionObject



53
54
55
# File 'lib/resources/npm.rb', line 53

def version
  info[:version]
end