Sha256: 2d97c4fd287db1f7c080768f7cff03c23b052bae4c16de917fa804da453f4036

Contents?: true

Size: 1008 Bytes

Versions: 2

Compression:

Stored size: 1008 Bytes

Contents

# frozen_string_literal: true

module ImportmapCLI
  module Commands
    # Class that generates importmap in json format
    class Json < BaseCommand
      def run
        # ensure importmap.rb exists
        unless File.exist?("#{ImportmapCLI.current_dir}/config/importmap.rb")
          puts "[error] #{ImportmapCLI.current_dir}/config/importmap.rb file does not exist"
          exit 1
        end

        imports = {}

        importmap_each_pin do |_line, matcher|
          package, url = matcher.values_at(:package, :url)

          imports.update(package => url)
        end

        puts JSON.pretty_generate({ imports: imports })
      end

      private

      def importmap_each_pin(&block)
        File.readlines("#{ImportmapCLI.current_dir}/config/importmap.rb").each do |line|
          line.chomp!
          next if line.empty? || !line.match(/^pin\s+["'](?<package>.*)['"],\s+to:\s+["'](?<url>.*)['"]/)

          block.call(line, Regexp.last_match)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
importmap-cli-0.4.0 lib/importmap_cli/commands/json.rb
importmap-cli-0.1.0 lib/importmap_cli/commands/json.rb