Sha256: 64968c22562ae415b39d845e27208584140bed5e67ad32fc5605eba5149d811b

Contents?: true

Size: 1.96 KB

Versions: 3

Compression:

Stored size: 1.96 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("https://yarn-parser.libraries.io/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.0.0 lib/bibliothecary/parsers/npm.rb
bibliothecary-5.6.2 lib/bibliothecary/parsers/npm.rb
bibliothecary-5.6.1 lib/bibliothecary/parsers/npm.rb