class Inspec::Resources::NpmPackage

Public Class Methods

new(package_name, opts = {}) click to toggle source
# File lib/inspec/resources/npm.rb, line 20
def initialize(package_name, opts = {})
  @package_name = package_name
  @location = opts[:path]
  @cache = nil
end

Public Instance Methods

info() click to toggle source
# File lib/inspec/resources/npm.rb, line 26
def info
  return @info if defined?(@info)

  if @location
    command_separator = inspec.os.platform?("windows") ? ";" : "&&"
    invocation = "cd #{Shellwords.escape @location} #{command_separator} npm"
  else
    invocation = "npm -g"
  end

  invocation = "#{invocation} ls --json #{@package_name}"

  # If on unix, wrap in sh -c to protect against sudo
  unless inspec.os.platform?("windows")
    invocation = "sh -c '#{invocation}'"
  end

  cmd = inspec.command(invocation)
  @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?() click to toggle source
# File lib/inspec/resources/npm.rb, line 56
def installed?
  info[:installed] == true
end
to_s() click to toggle source
# File lib/inspec/resources/npm.rb, line 64
def to_s
  "Npm Package #{@package_name}"
end
version() click to toggle source
# File lib/inspec/resources/npm.rb, line 60
def version
  info[:version]
end