require 'jshint'
require 'jshint/reporters'

namespace :jshint do
  desc "Runs JSHint, the JavaScript lint tool over this project's JavaScript assets"
  task :lint, [:reporter] => :environment do |_, args|
	  # We need to OR this because we can call rake jshint. The argument gets pushed to extras.
	  args.with_defaults(reporter: args.extras[0] || :Default)

    linter = Jshint::Lint.new
    linter.lint
    reporter = Jshint::Reporters.const_get(args.reporter).new(linter.errors)
    puts reporter.report
  end

  desc "Copies the default JSHint options to your Rails application"
  task :install_config => :environment do
    source_file = File.join(Jshint.root, 'config', 'jshint.yml')
    source_dest = File.join(Rails.root, 'config', '')
    FileUtils.cp(source_file, source_dest)
  end
  task :all => [:lint]
end

desc "Runs JSHint, the JavaScript lint tool over this projects JavaScript assets"
task :jshint => ["jshint:all"]

require 'jshint'
require 'jshint/reporters'

namespace :jshint do
  desc "Runs JSHint, the JavaScript lint tool over this project's JavaScript assets"
  task :lint => :environment do |_, args|
    # Our own argument parsing, since rake jshint will push extra nil's.
    reporter_name = :Default
    file = nil
    reporter_name = args.extras[0] if args.extras.length >= 1
    file = args.extras[1] if args.extras.length >= 2

    linter = Jshint::Lint.new
    linter.lint
    reporter = Jshint::Reporters.const_get(reporter_name).new(linter.errors)

    printer = lambda do |stream|
      stream.puts reporter.report
    end
    if file
      File.open(file, 'w') do |stream|
        printer.call(stream)
      end
    else
      printer.call($stdout)
    end
  end

  desc "Copies the default JSHint options to your Rails application"
  task :install_config => :environment do
    source_file = File.join(Jshint.root, 'config', 'jshint.yml')
    source_dest = File.join(Rails.root, 'config', '')
    FileUtils.cp(source_file, source_dest)
  end
  task :all => [:lint]
end

desc "Runs JSHint, the JavaScript lint tool over this projects JavaScript assets"
task :jshint => ["jshint:all"]

Allow us to specify a file name to output to.
Allow us to specify a file name to output to.
