Sha256: 488d92b14b6b6f855270e5f4f0727d4bd27fcc2eb721861ea1b2761bae87856b

Contents?: true

Size: 1.43 KB

Versions: 20

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

module Spandx
  module Js
    class YarnLock
      include Enumerable

      START_OF_DEPENDENCY_REGEX = %r{^"?(?<name>(@|\w|-|\.|/)+)@}i.freeze
      INJECT_COLON = /(?<=\w|")\s(?=\w|")/.freeze

      attr_reader :file_path

      def initialize(file_path)
        @file_path = file_path
        @metadatum = collect_metadatum
      end

      def each
        @metadatum.each do |metadata|
          yield metadata
        end
      end

      private

      def collect_metadatum
        items = Set.new
        File.open(file_path, 'r') do |io|
          until io.eof?
            metadata = map_from(io)
            next if metadata.nil? || metadata.empty?

            items << metadata
          end
        end
        items
      end

      def map_from(io)
        header = io.readline
        return unless (matches = header.match(START_OF_DEPENDENCY_REGEX))

        metadata_from(matches[:name].gsub(/"/, ''), io)
      end

      def metadata_from(name, io)
        YAML.safe_load(to_yaml(name, read_lines(io)))
      end

      def to_yaml(name, lines)
        (["name: \"#{name}\""] + lines)
          .map { |x| x.sub(INJECT_COLON, ': ') }
          .join("\n")
      end

      def read_lines(io)
        [].tap do |lines|
          line = io.readline.strip
          until line.empty? || io.eof?
            lines << line
            line = io.readline.strip
          end
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
spandx-0.18.3 lib/spandx/js/yarn_lock.rb
spandx-0.18.2 lib/spandx/js/yarn_lock.rb
spandx-0.18.1 lib/spandx/js/yarn_lock.rb
spandx-0.18.0 lib/spandx/js/yarn_lock.rb
spandx-0.17.0 lib/spandx/js/yarn_lock.rb
spandx-0.16.1 lib/spandx/js/yarn_lock.rb
spandx-0.16.0 lib/spandx/js/yarn_lock.rb
spandx-0.15.1 lib/spandx/js/yarn_lock.rb
spandx-0.15.0 lib/spandx/js/yarn_lock.rb
spandx-0.14.0 lib/spandx/js/yarn_lock.rb
spandx-0.13.5 lib/spandx/js/yarn_lock.rb
spandx-0.13.4 lib/spandx/js/yarn_lock.rb
spandx-0.13.3 lib/spandx/js/yarn_lock.rb
spandx-0.13.2 lib/spandx/js/yarn_lock.rb
spandx-0.13.1 lib/spandx/js/yarn_lock.rb
spandx-0.13.0 lib/spandx/js/yarn_lock.rb
spandx-0.12.3 lib/spandx/js/yarn_lock.rb
spandx-0.12.2 lib/spandx/js/yarn_lock.rb
spandx-0.12.1 lib/spandx/js/yarn_lock.rb
spandx-0.12.0 lib/spandx/js/yarn_lock.rb