Sha256: 7675caa373915a9c5f26925bab2b5127982307d51589e10d8ed5b10ca63d5915

Contents?: true

Size: 1.52 KB

Versions: 6

Compression:

Stored size: 1.52 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] || {}
          )
        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

6 entries across 6 versions & 1 rubygems

Version Path
code_ownership-1.28.0 lib/code_ownership/private/parse_js_packages.rb
code_ownership-1.27.0 lib/code_ownership/private/parse_js_packages.rb
code_ownership-1.26.0 lib/code_ownership/private/parse_js_packages.rb
code_ownership-1.25.0 lib/code_ownership/private/parse_js_packages.rb
code_ownership-1.24.0 lib/code_ownership/private/parse_js_packages.rb
code_ownership-1.23.0 lib/code_ownership/private/parse_js_packages.rb