class Guard::Coffeelint

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method
# File lib/guard/coffeelint.rb, line 9
def initialize(options = {})
  super
  @config_file = options[:config_file]
  @default_paths = options[:paths] || ['.']
end

Public Instance Methods

run_all() click to toggle source
# File lib/guard/coffeelint.rb, line 27
def run_all
  lint_and_report
end
run_on_additions(paths) click to toggle source
# File lib/guard/coffeelint.rb, line 19
def run_on_additions(paths)
  lint_and_report paths
end
run_on_modifications(paths) click to toggle source
# File lib/guard/coffeelint.rb, line 23
def run_on_modifications(paths)
  lint_and_report paths
end
start() click to toggle source
# File lib/guard/coffeelint.rb, line 15
def start
  UI.info "Guard::Coffeelint linting against #{@config_file}"
end

Protected Instance Methods

lint_and_report(paths = nil) click to toggle source
# File lib/guard/coffeelint.rb, line 44
def lint_and_report(paths = nil)
  command = 'coffeelint -c --reporter raw'
  command += " -f #{@config_file}" if @config_file
  command += if paths && paths.length > 0
               " #{paths.join ' '}"
             else
               " #{@default_paths.join(' ')}"
             end

  results = `#{command} 2>&1`

  begin
    results = JSON.parse(results)
  rescue JSON::ParserError
    UI.error "Error linting #{paths}"
    return
  end

  results.each do |file, errors|
    errors.each do |error|
      error_letter = error['level'].chars.first.upcase
      error_letter = case error_letter
                     when 'E' then error_letter.red
                     when 'W' then error_letter.yellow
                     else error_letter
                     end

      puts "#{file.cyan}:#{error['lineNumber']}:#{error_letter}" \
        ": #{error['name']}: #{error['message']}. #{error['context']}"
    end
  end

  file_count = results.size
  error_count = results.map { |_, e| e.size }.reduce(:+)
  puts "\n" + summary(file_count, error_count, color: true)

  notify file_count, error_count

  throw :task_has_failed unless error_count == 0
end
notify(file_count, error_count) click to toggle source
# File lib/guard/coffeelint.rb, line 33
def notify(file_count, error_count)
  msg = summary file_count, error_count
  image = if error_count > 0
    :failed
  else
    :success
  end
  Notifier.notify(msg, title: "CoffeeLint", image: image)
end
summary(file_count, error_count, color: false) click to toggle source
# File lib/guard/coffeelint.rb, line 85
def summary(file_count, error_count, color: false)
  summary = "#{file_count} files scanned, "
  summary << if error_count > 0
               s = "#{error_count} errors"
               color ? s.red : s
             else
               s = "No errors"
               color ? s.green : s
             end
  summary << " found."
end