Sha256: c720f17fef6205e96294aeac0063775f4fb7c80e1d4688b0db7b53fab13f5832

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

require 'json'

module Bibliothecary
  module Parsers
    class NPM
      include Bibliothecary::Analyser

      def self.mapping
        {
          /^package\.json$/ => {
            kind: 'manifest',
            parser: :parse_manifest
          },
          /^npm-shrinkwrap\.json$/ => {
            kind: 'lockfile',
            parser: :parse_shrinkwrap
          },
          /^yarn\.lock$/ => {
            kind: 'lockfile',
            parser: :parse_yarn_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_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.herokuapp.com/parse", body: file_contents)
        return [] unless response.response_code == 200
        JSON.parse(response.body)
      end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bibliothecary-5.1.0 lib/bibliothecary/parsers/npm.rb