Skip to content

Instantly share code, notes, and snippets.

@bernardo-cs
Created November 30, 2015 12:12
Show Gist options
  • Select an option

  • Save bernardo-cs/c2c1839cb025d9e0250f to your computer and use it in GitHub Desktop.

Select an option

Save bernardo-cs/c2c1839cb025d9e0250f to your computer and use it in GitHub Desktop.
Rake task build application.js assets
require 'sprockets'
require 'pry'
require 'fileutils'
namespace :karma do
task :start => :environment do
with_tmp_config :start
end
task :run => :environment do
fail unless with_tmp_config(:start, "--single-run")
end
private
def with_tmp_config(command, args = nil)
Tempfile.open(['karma', '.config.js.coffee'], Rails.root.join('tmp')) do |f|
build_js do |app_js_file|
f.write new_karma_config_file(app_js_file)
f.flush
return system("karma #{command} #{f.path} #{args}")
end
end
end
def build_js
sprocket = Sprockets::Environment.new
sprocket.append_path('app/assets/javascripts')
sprocket.append_path('vendor/assets/bower_components')
Tempfile.open('application.js', Rails.root.join('tmp')) do |appjs_file|
puts 'building app.js..'
appjs_file.puts(sprocket.find_asset('application.js'))
puts 'app.js built, booting karma..'
yield appjs_file
appjs_file.close
end
end
def new_karma_config_file(app_js)
File.open('config/karma.conf.js.coffee', 'r').read
.gsub(/'http.*application\.js'/, "'" + app_js.path + "'")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment