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



48
49
50
51
52
53
54
55
56
# File 'lib/bibliothecary/parsers/npm.rb', line 48

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

.mappingObject



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

def self.mapping
  {
    /^(?!node_modules).+*package\.json$/ => {
      kind: 'manifest',
      parser: :parse_manifest
    },
    /^(?!node_modules).+*npm-shrinkwrap\.json$/ => {
      kind: 'lockfile',
      parser: :parse_shrinkwrap
    },
    /^(?!node_modules).+*yarn\.lock$/ => {
      kind: 'lockfile',
      parser: :parse_yarn_lock
    }
  }
end

.parse_manifest(file_contents) ⇒ Object



36
37
38
39
40
# File 'lib/bibliothecary/parsers/npm.rb', line 36

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

.parse_shrinkwrap(file_contents) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/bibliothecary/parsers/npm.rb', line 25

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

.parse_yarn_lock(file_contents) ⇒ Object



42
43
44
45
46
# File 'lib/bibliothecary/parsers/npm.rb', line 42

def self.parse_yarn_lock(file_contents)
  response = Typhoeus.post("https://yarn-parser.libraries.io/parse", body: file_contents)
  return [] unless response.response_code == 200
  JSON.parse(response.body)
end