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