Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion guard-coffeescript.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Gem::Specification.new do |s|

s.add_development_dependency 'bundler'
s.add_development_dependency 'guard-rspec'
s.add_development_dependency 'rspec'
s.add_development_dependency 'rspec', '2.99.0'

s.files = Dir.glob('{lib}/**/*') + %w[LICENSE README.md]
s.require_path = 'lib'
Expand Down
15 changes: 8 additions & 7 deletions lib/guard/coffeescript.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
require 'guard'
require 'guard/guard'
require 'guard/plugin'
require 'guard/watcher'

module Guard

# The CoffeeScript guard that gets notifications about the following
# Guard events: `start`, `stop`, `reload`, `run_all` and `run_on_change`.
#
class CoffeeScript < Guard
class CoffeeScript < Plugin

autoload :Formatter, 'guard/coffeescript/formatter'
autoload :Inspector, 'guard/coffeescript/inspector'
Expand All @@ -25,27 +25,28 @@ class CoffeeScript < Guard

# Initialize Guard::CoffeeScript.
#
# @param [Array<Guard::Watcher>] watchers the watchers in the Guard block

# @param [Hash] options the options for the Guard
# @option options [String] :input the input directory
# @option options [String] :output the output directory
# @option options [Array<Guard::Watcher>] :watchers the watchers in the Guard block
# @option options [Boolean] :bare do not wrap the output in a top level function
# @option options [Boolean] :shallow do not create nested directories
# @option options [Boolean] :hide_success hide success message notification
# @option options [Boolean] :all_on_start generate all JavaScripts files on start
# @option options [Boolean] :noop do not generate an output file
# @option options [Boolean] :source_map generate the source map files
#
def initialize(watchers = [], options = {})
watchers = [] if !watchers
def initialize(options = {})
defaults = DEFAULT_OPTIONS.clone

if options[:input]
defaults.merge!({ :output => options[:input] })
watchers << ::Guard::Watcher.new(%r{^#{ options[:input] }/(.+\.(?:coffee|coffee\.md|litcoffee))$})
options[:watchers] = [] unless options[:watchers]
options[:watchers] << ::Guard::Watcher.new(%r{^#{ options[:input] }/(.+\.(?:coffee|coffee\.md|litcoffee))$})
end

super(watchers, defaults.merge(options))
super(defaults.merge(options))
end

# Gets called once when Guard starts.
Expand Down
15 changes: 7 additions & 8 deletions lib/guard/coffeescript/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def run(files, watchers, options = { })
#
def remove(files, watchers, options = { })
removed_files = []
directories = detect_nested_directories(watchers, files, options)
directories = detect_nested_directories(files, watchers, options)

directories.each do |directory, scripts|
scripts.each do |file|
Expand Down Expand Up @@ -76,15 +76,14 @@ def notify_start(files, options)

# Compiles all CoffeeScript files and writes the JavaScript files.
#
# @param [Array<String>] files the files to compile
# @param [Array<Guard::Watcher>] watchers the Guard watchers in the block
# @param [Array<String>] files the files to compile
# @param [Hash] options the options for the execution
# @return [Array<Array<String>, Array<String>] the result for the compilation run
#
def compile_files(files, watchers, options)
errors = []
changed_files = []
directories = detect_nested_directories(watchers, files, options)
directories = detect_nested_directories(files, watchers, options)

directories.each do |directory, scripts|
scripts.each do |file|
Expand Down Expand Up @@ -217,14 +216,14 @@ def javascript_file_name(file, directory)
# Detects the output directory for each CoffeeScript file. Builds
# the product of all watchers and assigns to each directory
# the files to which it belongs to.
#
# @param [Array<Guard::Watcher>] watchers the Guard watchers in the block
#
# @param [Array<String>] files the CoffeeScript files
# @param [Hash] options the options for the execution
# @param [Array<Guard::Watcher>] watchers the Guard watchers in the block
# @param [Hash] options the options for the execution
# @option options [String] :output the output directory
# @option options [Boolean] :shallow do not create nested directories
#
def detect_nested_directories(watchers, files, options)
def detect_nested_directories(files, watchers, options)
return { options[:output] => files } if options[:shallow]

directories = { }
Expand Down
24 changes: 18 additions & 6 deletions spec/guard/coffeescript_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@

describe '#initialize' do
context 'when no options are provided' do
it 'sets a default :watchers option' do
guard.watchers.should be_a Array
guard.watchers.should be_empty
end

it 'sets a default :wrap option' do
guard.options[:bare].should be_false
end
Expand Down Expand Up @@ -44,15 +49,22 @@
end

context 'with options besides the defaults' do
let(:guard) { Guard::CoffeeScript.new(nil, { :output => 'output_folder',
let(:watcher) { Guard::Watcher.new('^x/.+\.(?:coffee|coffee\.md|litcoffee)$') }

let(:guard) { Guard::CoffeeScript.new( { :output => 'output_folder',
:bare => true,
:shallow => true,
:hide_success => true,
:all_on_start => true,
:noop => true,
:source_map => true
:source_map => true,
:watchers => [watcher]
}) }

it 'sets the provided :watchers option' do
guard.watchers.should == [watcher]
end

it 'sets the provided :bare option' do
guard.options[:bare].should be_true
end
Expand All @@ -79,7 +91,7 @@
end

context 'with a input option' do
let(:guard) { Guard::CoffeeScript.new(nil, { :input => 'app/coffeescripts' }) }
let(:guard) { Guard::CoffeeScript.new( { :input => 'app/coffeescripts' }) }

it 'creates a watcher' do
guard.should have(1).watchers
Expand All @@ -96,7 +108,7 @@
end

context 'with an output option' do
let(:guard) { Guard::CoffeeScript.new(nil, { :input => 'app/coffeescripts',
let(:guard) { Guard::CoffeeScript.new( { :input => 'app/coffeescripts',
:output => 'public/javascripts' }) }

it 'keeps the output directory' do
Expand All @@ -113,7 +125,7 @@
end

context 'with the :all_on_start option' do
let(:guard) { Guard::CoffeeScript.new(nil, :all_on_start => true) }
let(:guard) { Guard::CoffeeScript.new( :all_on_start => true) }

it 'calls #run_all' do
guard.should_receive(:run_all)
Expand All @@ -123,7 +135,7 @@
end

describe '#run_all' do
let(:guard) { Guard::CoffeeScript.new([Guard::Watcher.new('^x/.+\.(?:coffee|coffee\.md|litcoffee)$')]) }
let(:guard) { Guard::CoffeeScript.new( { :watchers => [Guard::Watcher.new('^x/.+\.(?:coffee|coffee\.md|litcoffee)$')] } ) }

before do
Dir.stub(:glob).and_return ['x/a.coffee', 'x/b.coffee', 'y/c.coffee', 'x/d.coffeeemd', 'x/e.litcoffee']
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

RSpec.configure do |config|

config.color_enabled = true
config.color = true
config.filter_run :focus => true
config.run_all_when_everything_filtered = true

Expand Down