Sha256: 7a0732c3d14b5e28962f0ecd89bd69fec956e9f59c26fb53b08ace3506fa9efb

Contents?: true

Size: 1.91 KB

Versions: 36

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

# typed: true

module CodeOwnership
  module Private
    # Modeled off of ParsePackwerk
    module ParseJsPackages
      extend T::Sig

      ROOT_PACKAGE_NAME = 'root'
      PACKAGE_JSON_NAME = T.let('package.json', String)
      METADATA = 'metadata'

      class Package < T::Struct
        extend T::Sig

        const :name, String
        const :metadata, T::Hash[String, T.untyped]

        sig { params(pathname: Pathname).returns(Package) }
        def self.from(pathname)
          package_loaded_json = JSON.parse(pathname.read)

          package_name = if pathname.dirname == Pathname.new('.')
            ROOT_PACKAGE_NAME
          else
            pathname.dirname.cleanpath.to_s
          end

          new(
            name: package_name,
            metadata: package_loaded_json[METADATA] || {}
          )
        rescue JSON::ParserError => e
          error_message = <<~MESSAGE
            #{e.inspect}

            #{pathname} has invalid JSON, so code ownership cannot be determined.

            Please either make the JSON in that file valid or specify `js_package_paths` in config/code_ownership.yml.
          MESSAGE

          raise InvalidCodeOwnershipConfigurationError.new(error_message)
        end

        sig { returns(Pathname) }
        def directory
          root_pathname = Pathname.new('.')
          name == ROOT_PACKAGE_NAME ? root_pathname.cleanpath : root_pathname.join(name).cleanpath
        end
      end

      sig do
        returns(T::Array[Package])
      end
      def self.all
        package_glob_patterns = Private.configuration.js_package_paths.map do |pathspec|
          File.join(pathspec, PACKAGE_JSON_NAME)
        end

        # The T.unsafe is because the upstream RBI is wrong for Pathname.glob
        T.unsafe(Pathname).glob(package_glob_patterns).map(&:cleanpath).map do |path|
          Package.from(path)
        end
      end
    end
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
code_ownership-1.36.2 lib/code_ownership/private/parse_js_packages.rb
code_ownership-1.36.1 lib/code_ownership/private/parse_js_packages.rb
code_ownership-1.36.0 lib/code_ownership/private/parse_js_packages.rb
code_ownership-1.35.0 lib/code_ownership/private/parse_js_packages.rb
code_ownership-1.34.2 lib/code_ownership/private/parse_js_packages.rb
code_ownership-1.34.1 lib/code_ownership/private/parse_js_packages.rb
code_ownership-1.33.1 lib/code_ownership/private/parse_js_packages.rb
code_ownership-1.33.0 lib/code_ownership/private/parse_js_packages.rb
code_ownership-1.32.19 lib/code_ownership/private/parse_js_packages.rb
code_ownership-1.32.18 lib/code_ownership/private/parse_js_packages.rb
code_ownership-1.32.17 lib/code_ownership/private/parse_js_packages.rb
code_ownership-1.32.16 lib/code_ownership/private/parse_js_packages.rb
code_ownership-1.32.15 lib/code_ownership/private/parse_js_packages.rb
code_ownership-1.32.14 lib/code_ownership/private/parse_js_packages.rb
code_ownership-1.32.13 lib/code_ownership/private/parse_js_packages.rb
code_ownership-1.32.12 lib/code_ownership/private/parse_js_packages.rb
code_ownership-1.32.11 lib/code_ownership/private/parse_js_packages.rb
code_ownership-1.32.10 lib/code_ownership/private/parse_js_packages.rb
code_ownership-1.32.9 lib/code_ownership/private/parse_js_packages.rb
code_ownership-1.32.8 lib/code_ownership/private/parse_js_packages.rb