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

Add pre-commit hook that runs prettier #1699

Merged
merged 1 commit into from
Oct 23, 2024

Conversation

emdash-ie
Copy link
Contributor

@emdash-ie emdash-ie commented Oct 16, 2024

What's changed?

As a convenience, this PR adds a hook which checks the formatting of your files, formatting any which it’s safe to.

Implementation notes

I’ve copied the one I implemented in https://github.com/guardian/datawrapper-import/pull/12 and tweaked it to account for the differences between prettier and scalafmt.

In particular, the hook is careful not to overwrite any unstaged changes: if a file has both staged and unstaged changes, running a formatter on it could (if something goes wrong) cause you to lose changes that git is unaware of, and they might be difficult to recover.

To Test

  • Run the setup script, or manually run git config --local core.hooksPath scripts/git-hooks.
  • Test the behaviour of the hook with combinations of:
    • typescript files with only staged changes which need formatting
    • typescript files with only unstaged changes which need formatting
    • typescript files with staged and unstaged changes, which need formatting
    • non-typescript files with staged or unstaged changes

As a convenience, this hook checks the formatting of your files,
formatting any for which it’s safe to do so.
@emdash-ie emdash-ie requested a review from a team as a code owner October 16, 2024 16:42
Copy link
Contributor

@fredex42 fredex42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😎 where did you get the script from originally?

@emdash-ie
Copy link
Contributor Author

emdash-ie commented Oct 23, 2024

😎 where did you get the script from originally?

Nowhere, I wrote it! It was my first time using comm. I think I ran shellcheck on the script too? Will double-check.

@emdash-ie
Copy link
Contributor Author

Ran shellcheck, no problems reported

@emdash-ie
Copy link
Contributor Author

Ok, ran shellcheck with -oall and got some extra complaints about masked return values in pipelines (e.g. of git and comm) – not sure whether I want to fix those or not.

@emdash-ie emdash-ie merged commit 4804b2a into main Oct 23, 2024
12 checks passed
@emdash-ie emdash-ie deleted the run-prettier-in-pre-commit-hook branch October 23, 2024 11:33
@prout-bot
Copy link

Seen on PROD (merged by @emdash-ie 6 minutes and 29 seconds ago) Please check your changes!

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

Successfully merging this pull request may close these issues.

3 participants