Skip to content

Instantly share code, notes, and snippets.

@bernardo-cs
Created November 26, 2015 12:28
Show Gist options
  • Save bernardo-cs/1fdd1276a562e20c7e42 to your computer and use it in GitHub Desktop.
Save bernardo-cs/1fdd1276a562e20c7e42 to your computer and use it in GitHub Desktop.
Karma with application.js
require 'sprockets'
namespace :karma do
task :start => :environment do
with_tmp_config :start
end
task :run => :environment do
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
system "karma #{command} #{f.path} #{args}"
end
end
end
def build_js
sprocket = Sprockets::Environment.new
sprocket.js_compressor = :uglifier
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