erb-formatter
erb-formatter copied to clipboard
Return exit code if file is not formatted
A bit like rubocop, I'd like CI to flag pull requests that do not have .erb files already formatted, and fail the build. I tried sendin erb-format
's output to /dev/null, but its exit code is zero even when I have unformatted input.
Is there a good way to do this?
thanks for erb-formatter, it appears one of a kind.
You can sort of beat around the bush to accomplish this. We use CircleCI and we just run the formatter with the -w
argument then run a git diff HEAD
which will exit code how you'd like.
erbformat:
executor:
name: ruby
steps:
- checkout
- restore_gems
- restore_bootsnap_cache
- restore_node_modules
- run:
name: erbformat
command: bundle exec erb-format app/views/**/*.erb -w --print-width 120
- run:
name: No erbformat errors found
command: git diff HEAD --exit-code
@fdr @darrenterhune I'd be happy to accept a PR for this.
Differently from rubocop
which main aim is checking files and has autocorrection attached, erb-formatter
is a formatter first and only secondarily can be used for checking.
Rubocop has a --fail-level
option that is used to control the exit status.
I think we can go along the same lines and use the same name with these values:
-
[C] check
fail when any file needs correction -
[E] error
fail when any other error occurs
Happy to get other levels suggested if anyone wants to venture into sending a PR for this feature 🙌
You can sort of beat around the bush to accomplish this. We use CircleCI and we just run the formatter with the
-w
argument then run agit diff HEAD
which will exit code how you'd like.erbformat: executor: name: ruby steps: - checkout - restore_gems - restore_bootsnap_cache - restore_node_modules - run: name: erbformat command: bundle exec erb-format app/views/**/*.erb -w --print-width 120 - run: name: No erbformat errors found command: git diff HEAD --exit-code
Thank you for sharing @darrenterhune, here's a GitHub Action version that I'm using :point_down:
name: format
on: push
jobs:
erb-formatter:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
- name: Install Gems
run: bundle install
- name: Run erb-formatter
run: erb-formatter --write --print-width 120 app/views/**/*.html.erb
- name: Check for changes
run: git diff --quiet app/views