lib/scss_lint/cli.rb in scss-lint-0.29.0 vs lib/scss_lint/cli.rb in scss-lint-0.30.0

- old
+ new

@@ -18,23 +18,31 @@ no_input: 66, # Input file did not exist or was not readable software: 70, # Internal software error config: 78, # Configuration error } + DEFAULT_REPORTER = [SCSSLint::Reporter::DefaultReporter, :stdout] + # @param args [Array] def initialize(args = []) @args = args - @options = {} + @options = { reporters: [DEFAULT_REPORTER] } @config = Config.default end def parse_arguments begin options_parser.parse!(@args) # Take the rest of the arguments as files/directories - @options[:files] = @args + + if @args.empty? + @options[:files] = @config.scss_files + else + @options[:files] = @args + end + rescue OptionParser::InvalidOption => ex print_help options_parser.help, ex end begin @@ -64,10 +72,18 @@ opts.on('-f', '--format Formatter', 'Specify how to display lints', String) do |format| define_output_format(format) end + opts.on('-o', '--out path', 'Write output to a file instead of STDOUT', String) do |path| + define_output_path(path) + end + + opts.on('-r', '--require path', 'Require Ruby file', String) do |path| + require path + end + opts.on_tail('--show-formatters', 'Shows available formatters') do print_formatters end opts.on('-i', '--include-linter linter,...', Array, @@ -190,21 +206,32 @@ end # @param lints [Array<Lint>] def report_lints(lints) sorted_lints = lints.sort_by { |l| [l.filename, l.location] } - reporter = @options.fetch(:reporter, SCSSLint::Reporter::DefaultReporter) - .new(sorted_lints) - output = reporter.report_lints - print output if output + @options.fetch(:reporters).each do |reporter, output| + results = reporter.new(sorted_lints).report_lints + io = (output == :stdout ? $stdout : File.new(output, 'w+')) + io.print results if results + end end # @param format [String] def define_output_format(format) - @options[:reporter] = SCSSLint::Reporter.const_get(format + 'Reporter') + unless @options[:reporters] == [DEFAULT_REPORTER] && format == 'Default' + @options[:reporters].reject! { |i| i == DEFAULT_REPORTER } + reporter = SCSSLint::Reporter.const_get(format + 'Reporter') + @options[:reporters] << [reporter, :stdout] + end rescue NameError puts "Invalid output format specified: #{format}" halt :config + end + + # @param path [String] + def define_output_path(path) + last_reporter, _output = @options[:reporters].pop + @options[:reporters] << [last_reporter, path] end def print_formatters puts 'Installed formatters:'