Sha256: 1126be791368f4e20070e020dec1c8261d1cc22baeb3849ac729362eff36c8cf

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

require 'sprockets'

module TsAssets
  class ApplicationGenerator
    # @return [Hash] mapping
    attr_reader :mapping

    # @param [String] include
    def initialize(include:)
      @mapping = build_mapping(include)

      environment.append_path(include)
    end

    # @return [Sprockets::Environment]
    def environment
      @environment ||= Sprockets::Environment.new
    end

    # @return [String]
    def generate
      [ # header
        react_content.header,

        # body
        const_content.body,
        react_content.body,

      ].join("\n")
    end

    # @return [TsAssets::Models::Content]
    def const_content
      @const_content ||= TsAssets::Generators::ConstGenerator.new(mapping).generate
    end

    # @return [TsAssets::Models::Content]
    def react_content
      @react_content ||= TsAssets::Generators::ReactGenerator.new(mapping).generate
    end

    # @param [String] include_path
    # @return [Hash]
    def build_mapping(include_path)
      mapping = {}

      Dir.glob("#{include_path}/**/*.*").each do |full_path|
        mapping[full_path] = TsAssets::Models::AssetMetaInfo.new(
          full_path: full_path, 
          include_path: include_path,
          environment: environment,
        )
      end

      mapping
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ts_assets-0.1.1 lib/ts_assets/application_generator.rb
ts_assets-0.1.0 lib/ts_assets/application_generator.rb