Dangerfile 1.03 KB
Newer Older
1 2 3 4 5 6 7 8
# frozen_string_literal: true

def get_prettier_files(files)
  files.select do |file|
    file.end_with?('.js', '.scss', '.vue')
  end
end

9
prettier_candidates = get_prettier_files(helper.all_changed_files)
10 11 12 13 14 15 16 17 18 19 20 21

return if prettier_candidates.empty?

unpretty = `node_modules/prettier/bin-prettier.js --list-different #{prettier_candidates.join(" ")}`
             .split(/$/)
             .map(&:strip)
             .reject(&:empty?)

return if unpretty.empty?

warn 'This merge request changed frontend files without pretty printing them.'

Ash McKenzie's avatar
Ash McKenzie committed
22 23 24
if GitlabDanger.new(helper.gitlab_helper).ci?
  markdown(<<~MARKDOWN)
    ## Pretty print Frontend files
25

Ash McKenzie's avatar
Ash McKenzie committed
26
    The following files should have been pretty printed with `prettier`:
27

Ash McKenzie's avatar
Ash McKenzie committed
28
    * #{unpretty.map { |path| "`#{path}`" }.join("\n* ")}
29

Ash McKenzie's avatar
Ash McKenzie committed
30
    Please run
31

Ash McKenzie's avatar
Ash McKenzie committed
32 33 34 35
    ```
    node_modules/.bin/prettier --write \\
    #{unpretty.map { |path| "  '#{path}'" }.join(" \\\n")}
    ```
36

Ash McKenzie's avatar
Ash McKenzie committed
37
    Also consider auto-formatting [on-save].
38

Ash McKenzie's avatar
Ash McKenzie committed
39 40 41
    [on-save]: https://docs.gitlab.com/ee/development/new_fe_guide/style/prettier.html
  MARKDOWN
end