BigW Consortium Gitlab

package_json_linker.rb 1.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
module Gitlab
  module DependencyLinker
    class PackageJsonLinker < JsonLinker
      self.file_type = :package_json

      private

      def link_dependencies
        link_json('name', json["name"], &method(:package_url))
        link_json('license', &method(:license_url))
        link_json(%w[homepage url], URL_REGEX, &:itself)

        link_packages
      end

      def link_packages
        link_packages_at_key("dependencies", &method(:package_url))
        link_packages_at_key("devDependencies", &method(:package_url))
      end

      def link_packages_at_key(key, &url_proc)
        dependencies = json[key]
        return unless dependencies

        dependencies.each do |name, version|
          link_json(name, version, link: :key, &url_proc)

          link_json(name) do |value|
            case value
            when /\A#{URL_REGEX}\z/
              value
            when /\A#{REPO_REGEX}\z/
              github_url(value)
            end
          end
        end
      end

      def package_url(name)
        "https://npmjs.com/package/#{name}"
      end
    end
  end
end