Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the recommended work around for autoformatters like prettier? #614

Closed
oliv9286 opened this issue Jan 24, 2019 · 4 comments
Closed

Comments

@oliv9286
Copy link

Hello, we are using Overcommit to run some pre-commit hooks such as eslint. From what I understand, overcommit does not support any pre-commit hooks that perform side effects on files. I also saw issue #238 which sounds like the official support is not going to be available any time soon. Does anyone have recommended work arounds for how I can still run prettier --write ideally before any of the overcommit pre-commit hooks run?

Many thanks

@hatched-danny
Copy link

hatched-danny commented Feb 19, 2019

Side effects don't seem supported so I thought just ensuring Prettier was manually run (or automatically if you have text editor plugins) was good enough:

./.git-hooks/pre_commit/prettier.rb:

module Overcommit::Hook::PreCommit
  class Prettier < Base
    BOLD_RED_COLOR = '1;31'
    PRETTIER_WARNING = "Code style issues found in the above file(s). Forgot to run Prettier?"

    def run
      result = execute(command, args: applicable_files)
      output = result.stdout.chomp
      has_warning = output.split("\n").any?(PRETTIER_WARNING)

      if has_warning
        return :fail, warning_message(output)
      end

      :pass
    end

    private

    def warning_message(message)
      "\e[#{BOLD_RED_COLOR}m#{message}\e[0m"
    end
  end
end

overcommit.yml:

  Prettier:
    enabled: true
    command: ['npx', 'prettier']
    flags: ['-c']
    description: 'Ensure Prettier is used to format JS'
    include:
      - '**/*.js'
      - '**/*.json'

@oliv9286
Copy link
Author

thank you @hatched-danny, that's what I'll do

@blairanderson
Copy link

I couldn't that file above to work... instead adding an executable script

Prettier:
  enabled: true
  required_executable: './bin/prettirun'
#!/bin/sh

# grep for file changes, exit early for ruby folks
FILES=$(git diff --name-only --diff-filter=ACMR -- '*.js' '*.jsx' | sed 's| |\\ |g')
[ -z "$FILES" ] && exit 0


# Be nice: and run the yarn install for folks that haven't done it yet
if [[ ! -f ./node_modules/.bin/prettier ]]
then
    # yarn add --dev --exact prettier
    echo "Running Yarn install..."
    open "https://prettier.io/docs/en/editors.html"
    command yarn install
fi

# Prettier CHECK all selected files
echo "$FILES" | xargs ./node_modules/.bin/prettier --ignore-unknown --check

This will warn and output any diff'ed files that are not prettier

@tmaier
Copy link

tmaier commented Mar 3, 2022

@blairanderson I was able to fix the solution of @hatched-danny

# frozen_string_literal: true

module Overcommit
  module Hook
    module PreCommit
      class Prettier < Base
        BOLD_RED_COLOR = '1;31'
        PRETTIER_WARNING = '[warn] Code style issues found in the above file(s). Forgot to run Prettier?'

        def run
          result = execute(command, args: applicable_files)
          output = result.stderr.chomp
          has_warning = output.split("\n").any?(PRETTIER_WARNING)

          return :fail, warning_message(output) if has_warning

          :pass
        end

        private

        def warning_message(message)
          "\e[#{BOLD_RED_COLOR}m#{message}\e[0m"
        end
      end
    end
  end
end

notable changes:

  • PRETTIER_WARNING has changed
  • We are checking the output of stderr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants