Class: Licensed::Source::NPM

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

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ NPM

Returns a new instance of NPM.



7
8
9
# File 'lib/licensed/source/npm.rb', line 7

def initialize(config)
  @config = config
end

Instance Method Details

#dependenciesObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/licensed/source/npm.rb', line 19

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

  locations = {}
  package_location_command.lines.each do |line|
    path, id = line.split(":")[0, 2]
    locations[id] ||= path
  end

  packages = recursive_dependencies(JSON.parse()["dependencies"])

  @dependencies = packages.map do |name, package|
    path = package["realPath"] || locations["#{package["name"]}@#{package["version"]}"]
    fail "couldn't locate #{name} under node_modules/" unless path
    Dependency.new(path, {
      "type"     => type,
      "name"     => package["name"],
      "version"  => package["version"],
      "summary"  => package["description"],
      "homepage" => package["homepage"]
    })
  end
end

#enabled?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/licensed/source/npm.rb', line 15

def enabled?
  @config.enabled?(type) && File.exist?(@config.pwd.join("package.json"))
end

#npm_list_command(*args) ⇒ Object

Executes an ‘npm list` command with the provided args and returns the output from stdout



65
66
67
# File 'lib/licensed/source/npm.rb', line 65

def npm_list_command(*args)
  Licensed::Shell.execute("npm", "list", *args)
end

#package_location_commandObject

Returns the output from running ‘npm list` to get package paths



54
55
56
# File 'lib/licensed/source/npm.rb', line 54

def package_location_command
  npm_list_command("--parseable", "--production", "--long")
end

#package_metadata_commandObject

Returns the output from running ‘npm list` to get package metadata



59
60
61
# File 'lib/licensed/source/npm.rb', line 59

def 
  npm_list_command("--json", "--production", "--long")
end

#recursive_dependencies(dependencies, result = {}) ⇒ Object

Recursively parse dependency JSON data. Returns a hash mapping the package name to it’s metadata



45
46
47
48
49
50
51
# File 'lib/licensed/source/npm.rb', line 45

def recursive_dependencies(dependencies, result = {})
  dependencies.each do |name, dependency|
    (result[name] ||= {}).update(dependency)
    recursive_dependencies(dependency["dependencies"] || {}, result)
  end
  result
end

#typeObject



11
12
13
# File 'lib/licensed/source/npm.rb', line 11

def type
  "npm"
end