Sha256: 371ed74c236a89c9bec3f697c787a3c1731f52b1a07280a18ff7562b2e06f25e

Contents?: true

Size: 973 Bytes

Versions: 1

Compression:

Stored size: 973 Bytes

Contents

require 'rack'
require 'listen'
require 'sprockets'

module Pieces
  class Server < Rack::Server
    attr_reader :path

    def initialize(options = {})
      @path = options[:path] || Dir.pwd
      super
    end

    def start
      Pieces::Listener.new(path: path).listen
      super
    end

    def sprockets_env
      Sprockets::Environment.new.tap do |env|
        env.append_path 'app/assets/javascripts'
        env.append_path 'app/assets/stylesheets'
      end
    end

    def app
      urls = files_to_serve(path)
      build_path = "#{path}/build"

      Rack::Builder.app do
        use Rack::Reloader
        use Rack::Static, urls: urls, root: build_path, index: 'index.html'
        map('/assets') { run sprockets_env } unless defined? ::Rails
        run Proc.new { |env| [404, {}, ['Not found']] }
      end
    end

    private

    def files_to_serve(path)
      Dir["#{path}/build/**/*"].map { |file| file.sub("#{path}/build", '') }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pieces-0.4.3 lib/pieces/server.rb