Sha256: c0d7406257259509994be0f3781910c607ee6c5d94c843e8806db19d75d8ac60

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

require "json"

module PhlexCustomElementGenerator
  class ManifestReader
    attr_accessor :manifest, :manifest_json

    # Reads a given string and determins if it should fetch over HTTP, Read a file, or read the contents.
    def read_manifest(str)
      # return read_manifest_from_http(str) if str =~ /^https?:\/\//

      return JSON.parse(File.read(str)) if File.exist?(str)

      # Try finding it in node_modules?
      # return read_from_node_modules(str)

      # Fallback to the string and hope its valid JSON
      JSON.parse(str)
    end

    def initialize(manifest:)
      @manifest = manifest
      @manifest_json = read_manifest(manifest)
    end

    def find_all_custom_elements
      json = @manifest_json
      custom_elements = {}

      json["modules"].flatten.each do |mod|
        parent_module = mod
        mod["declarations"].flatten.each do |dec|
          # Needs to be != true because == false fails nil checks.
          next if dec["customElement"] != true

          tag_name = dec["tagName"]

          dec["parent_module"] = parent_module
          custom_elements[tag_name] = dec if tag_name
        end
      end

      custom_elements
    end

    def list_tag_names
      find_all_custom_elements.keys
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
phlex_custom_element_generator-0.1.0 lib/phlex_custom_element_generator/manifest_reader.rb