Class: Middleman::ScssLint::Extension

Inherits:
Extension
  • Object
show all
Defined in:
lib/middleman-scss-lint/extension.rb

Instance Method Summary collapse

Instance Method Details

#location(path, descr) ⇒ Object



88
89
90
# File 'lib/middleman-scss-lint/extension.rb', line 88

def location(path, descr)
  "#{path.color(:cyan)}:#{descr["line"].to_s.color(:magenta)}"
end

#message(descr) ⇒ Object



96
97
98
99
# File 'lib/middleman-scss-lint/extension.rb', line 96

def message(descr)
  linter_name = "#{descr["linter"]}: ".color(:green)
  "#{linter_name}#{descr["reason"]}"
end

#readyObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/middleman-scss-lint/extension.rb', line 7

def ready
  require 'rainbow'
  require 'rainbow/ext/string'

  result = run_once

  if app.build?
    if options[:fail_build] && !result
      logger.error "== SCSSLint failed"
      exit(1)
    end
  else
    logger.info "== SCSSLint succeeded" if result
    watch_and_run
  end
end

#run_linter(files_to_lint) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/middleman-scss-lint/extension.rb', line 45

def run_linter(files_to_lint)
  logger.info "== Linting SCSS"

  cli_args = ['--format', 'JSON']
  cli_args = cli_args + ['--config', options[:config]] if options[:config]
  cli_args = cli_args + files_to_lint

  begin
    output = ""

    ::IO.popen("bundle exec scss-lint #{cli_args.join(' ')}", 'r') do |pipe|
      while buf = pipe.gets
        output << buf
      end
    end

    error_count = 0

    result = ::JSON.parse(output)

    result.each do |file_path, lints|
      relative_path = file_path.sub(app.root, '')

      lints.each do |descr|
        msg = "#{location(relative_path, descr)} #{type(descr)} #{message(descr)}"

        error_count += 1

        if descr["severity"] == "warning"
          logger.warn msg
        else
          logger.error msg
        end 
      end
    end

    error_count <= 0
  rescue ::Errno::ENOENT => e
    logger.error "== SCSSLint: Command failed with message: #{e.message}"
    exit(1)
  end
end

#run_onceObject



24
25
26
27
28
29
30
# File 'lib/middleman-scss-lint/extension.rb', line 24

def run_once
  paths = app.sitemap.resources
      .select { |r| File.extname(r.source_file) == '.scss' }
      .map { |r| r.source_file }

  run_linter(paths)
end

#type(descr) ⇒ Object



92
93
94
# File 'lib/middleman-scss-lint/extension.rb', line 92

def type(descr)
  descr["severity"] == "error" ? '[E]'.color(:red) : '[W]'.color(:yellow)
end

#watch_and_runObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/middleman-scss-lint/extension.rb', line 32

def watch_and_run
  app.files.on_change :source do |changed|
    changed_scss = changed.select do |f|
      f[:full_path].extname == '.scss'
    end

    if changed_scss.length > 0
      result = run_linter(changed_scss.map { |r| r[:full_path].to_s })
      logger.info "== SCSSLint succeeded" if result
    end
  end
end