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

.mappingObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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
    },
    /^(?!node_modules).*package-lock\.json$/ => {
      kind: 'lockfile',
      parser: :parse_package_lock
    }
  }
end

.parse_manifest(file_contents) ⇒ Object



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

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

.parse_package_lock(file_contents) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/bibliothecary/parsers/npm.rb', line 40

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

.parse_shrinkwrap(file_contents) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/bibliothecary/parsers/npm.rb', line 29

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



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/bibliothecary/parsers/npm.rb', line 57

def self.parse_yarn_lock(file_contents)
  response = Typhoeus.post("#{Bibliothecary.configuration.yarn_parser_host}/parse", body: file_contents)
  return [] unless response.response_code == 200
  json = JSON.parse(response.body, symbolize_names: true)
  json.uniq.map do |dep|
    {
      name: dep[:name],
      requirement: dep[:version],
      type: dep[:type]
    }
  end
end