Sha256: a9c4c103583ac67e053d7b73d6ddf169dbf4d8540040e2502fdb956a9da21c46

Contents?: true

Size: 1.97 KB

Versions: 3

Compression:

Stored size: 1.97 KB

Contents

require 'json'

module Bibliothecary
  module Parsers
    class NPM
      include Bibliothecary::Analyser

      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

      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

      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

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

      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
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bibliothecary-6.2.1 lib/bibliothecary/parsers/npm.rb
bibliothecary-6.2.0 lib/bibliothecary/parsers/npm.rb
bibliothecary-6.1.0 lib/bibliothecary/parsers/npm.rb