Class: Bibliothecary::Parsers::NPM

Inherits:
Object
  • Object
show all
Includes:
Analyser
Defined in:
lib/bibliothecary/parsers/npm.rb

Class Method Summary collapse

Methods included from Analyser

included

Class Method Details

.map_dependencies(hash, key, type) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/bibliothecary/parsers/npm.rb', line 37

def self.map_dependencies(hash, key, type)
  hash.fetch(key,[]).map do |name, requirement|
    {
      name: name,
      requirement: requirement,
      type: type
    }
  end
end

.parse(filename, path) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/bibliothecary/parsers/npm.rb', line 8

def self.parse(filename, path)
  if filename.match(/package\.json$/)
    file_contents = File.open(path).read
    json = JSON.parse(file_contents)
    parse_manifest(json)
  elsif filename.match(/npm-shrinkwrap\.json$/)
    file_contents = File.open(path).read
    json = JSON.parse(file_contents)
    parse_shrinkwrap(json)
  else
    []
  end
end

.parse_manifest(manifest) ⇒ Object



32
33
34
35
# File 'lib/bibliothecary/parsers/npm.rb', line 32

def self.parse_manifest(manifest)
  map_dependencies(manifest, 'dependencies', 'runtime') +
  map_dependencies(manifest, 'devDependencies', 'development')
end

.parse_shrinkwrap(manifest) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/bibliothecary/parsers/npm.rb', line 22

def self.parse_shrinkwrap(manifest)
  manifest.fetch('dependencies',[]).map do |name, requirement|
    {
      name: name,
      requirement: requirement["version"],
      type: 'runtime'
    }
  end
end