gitlab-org--gitlab-foss/danger/prettier/Dangerfile
Lukas Eipert 253e1818aa
Create helper to get all changed files
Danger apparently has three different objects which could contain files
you often want to check:

 - git.added_files
 - git.modified_files
 - git.renamed_files

The problem: If a file is renamed, `modified_files` contains the file
path before the rename. In some Danger checks we use `added_files` +
`modified_files`, which might contain the deleted paths of renamed
files, but missing the new paths of renamed files.

So we need to consider `renamed_files` as well.
2018-10-22 11:04:55 +02:00

39 lines
985 B
Ruby

# frozen_string_literal: true
def get_prettier_files(files)
files.select do |file|
file.end_with?('.js', '.scss', '.vue')
end
end
prettier_candidates = get_prettier_files(helper.all_changed_files)
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.'
markdown(<<~MARKDOWN)
## Pretty print Frontend files
The following files should have been pretty printed with `prettier`:
* #{unpretty.map { |path| "`#{path}`" }.join("\n* ")}
Please run
```
node_modules/.bin/prettier --write \\
#{unpretty.map { |path| " '#{path}'" }.join(" \\\n")}
```
Also consider auto-formatting [on-save].
[on-save]: https://docs.gitlab.com/ee/development/new_fe_guide/style/prettier.html
MARKDOWN