lib/csscss/cli.rb in csscss-1.0.0 vs lib/csscss/cli.rb in csscss-1.1.0

- old
+ new

@@ -6,26 +6,27 @@ @color = true @minimum = 3 @compass = false @ignored_properties = [] @ignored_selectors = [] + @match_shorthand = true end def run parse(@argv) execute end def execute + warn_old_debug_flag if ENV["CSSCSS_DEBUG"] - all_redundancies = @argv.map do |filename| - contents = if %w(.scss .sass).include?(File.extname(filename).downcase) && !(filename =~ URI.regexp) + all_contents = @argv.map do |filename| + if %w(.scss .sass).include?(File.extname(filename).downcase) && !(filename =~ URI.regexp) begin require "sass" rescue LoadError - puts "Must install sass gem before parsing sass/scss files" - exit 1 + abort "Must install sass gem before parsing sass/scss files" end sass_options = {cache:false} sass_options[:load_paths] = Compass.configuration.sass_load_paths if @compass begin @@ -39,40 +40,35 @@ end end else open(filename) {|f| f.read } end + end.join("\n") - RedundancyAnalyzer.new(contents).redundancies(minimum: @minimum, - ignored_properties: @ignored_properties, - ignored_selectors: @ignored_selectors) - end + unless all_contents.strip.empty? + redundancies = RedundancyAnalyzer.new(all_contents).redundancies( + minimum: @minimum, + ignored_properties: @ignored_properties, + ignored_selectors: @ignored_selectors, + match_shorthand: @match_shorthand + ) - combined_redundancies = all_redundancies.inject({}) do |combined, redundancies| - if combined.empty? - redundancies + if @json + puts JSONReporter.new(redundancies).report else - combined.merge(redundancies) do |_, v1, v2| - (v1 + v2).uniq - end + report = Reporter.new(redundancies).report(verbose:@verbose, color:@color) + puts report unless report.empty? end end - if @json - puts JSONReporter.new(combined_redundancies).report - else - report = Reporter.new(combined_redundancies).report(verbose:@verbose, color:true) - puts report unless report.empty? - end - rescue Parslet::ParseFailed => e - line, column = e.cause.source.line_and_column + line, column = e.cause.source.line_and_column(e.cause.pos) puts "Had a problem parsing the css at line: #{line}, column: #{column}".red - if ENV['CSSCSS_DEBUG'] == 'true' + if @show_parser_errors || ENV['CSSCSS_DEBUG'] == 'true' puts e.cause.ascii_tree.red else - puts "Run with CSSCSS_DEBUG=true for verbose parser errors".red + puts "Run with --show-parser-errors for verbose parser errors".red end exit 1 end def parse(argv) @@ -82,11 +78,11 @@ opts.on("-v", "--[no-]verbose", "Display each rule") do |v| @verbose = v end - opts.on("--[no-]color", "Colorizes output") do |c| + opts.on("--[no-]color", "Colorize output (default is true)") do |c| @color = c end opts.on("-n", "--num N", Integer, "Print matches with at least this many rules. Defaults to 3") do |n| @minimum = n @@ -103,25 +99,31 @@ opts.on("-V", "--version", "Show version") do |v| puts opts.ver exit end - opts.on("--[no-]compass", "Enables compass extensions when parsing sass/scss") do |compass| - if @compass = compass - begin - require "compass" - rescue LoadError - puts "Must install compass gem before enabling its extensions" - exit 1 - end - end + opts.on("--[no-]compass", "Enable compass extensions when parsing sass/scss (default is false)") do |compass| + enable_compass if @compass = compass end + opts.on("--compass-with-config config", "Enable compass extensions when parsing sass/scss and pass config file") do |config| + @compass = true + enable_compass(config) + end + + opts.on("--[no-]match-shorthand", "Expands shorthand rules and matches on explicit rules (default is true)") do |match_shorthand| + @match_shorthand = match_shorthand + end + opts.on("-j", "--[no-]json", "Output results in JSON") do |j| @json = j end + opts.on("--show-parser-errors", "Print verbose parser errors") do |show_parser_errors| + @show_parser_errors = show_parser_errors + end + opts.on_tail("-h", "--help", "Show this message") do print_help(opts) end end opts.parse!(argv) @@ -133,9 +135,25 @@ private def print_help(opts) puts opts exit + end + + def warn_old_debug_flag + $stderr.puts "CSSCSS_DEBUG is now deprecated. Use --show-parser-errors instead".red + end + + def enable_compass(config = nil) + require "compass" + + if config + Compass.add_configuration(config) + else + Compass.add_configuration("config.rb") if File.exist?("config.rb") + end + rescue LoadError + abort "Must install compass gem before enabling its extensions" end class << self def run(argv) new(argv).run