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

XO --fix on pre-commit hook without blocking commit #494

Closed
daartv opened this issue Aug 11, 2020 · 8 comments
Closed

XO --fix on pre-commit hook without blocking commit #494

daartv opened this issue Aug 11, 2020 · 8 comments

Comments

@daartv
Copy link

daartv commented Aug 11, 2020

Hello!

My issue is the following:
I have sucessfully added xo --fix to my pre-commit hooks and it's fixing all autofixable issues, but erroring out with non-autofixable ones, we're in the middle of integrating it so it's a gradual rollout, is there a way to run XO in silent mode so that it shows the error but doesn't block the commit?

I'm using Husky and lint-staged to run the hook.

@daartv daartv changed the title XO --fix on pre-commit hook without blocking i XO --fix on pre-commit hook without blocking commit Aug 11, 2020
@sindresorhus
Copy link
Member

You can probably do it with Bash: xo --fix || true. Not sure how to do that cross-platform though. It's not something I intend to support built-in.

@papb
Copy link

papb commented Aug 11, 2020

By the way, I can confirm that the foobar || true syntax works fine on windows, so that's probably cross-platform, unless it doesn't work on Mac 😅

@daartv
Copy link
Author

daartv commented Aug 12, 2020

Doesn’t seem to work for me on Mac 😅

@devinrhode2

This comment has been minimized.

@devinrhode2
Copy link
Contributor

devinrhode2 commented Oct 18, 2021

Ok, so lint-staged v11.2.0 and husky v7 will take:

// package.json
"*.js,*.jsx,*.ts,*.tsx": [
  "xo --fix"
]

And transform it into:

xo --fix foo.ts bar.ts baz.ts

I haven't investigated exactly how it may respond to seeing xo --fix || true, but I'm assuming it will just append file list to the end.

I did suggest the idea of allowing file replacements, which could allow us to do something like this:

"*.js,*.jsx,*.ts,*.tsx": [
  "xo --fix $staged_files || true"
]

In the short term, you could create a script:

"*.js,*.jsx,*.ts,*.tsx": [
  "yarn run process_files"
]

In package.json scripts:

"process_files": "node ./scripts/process_files.js"

Inside ./scripts/process_files.js:

#!/idk/what/it/node

const stagedFilesList = "...parse process.env.argv..."

execa('yarn xo --fix ' + stagedFilesList)

If you have small enough commits, you hopefully won't have memory issues when running XO pre-commit.

If you are having memory issues elsewhere, follow: #599 (comment)

@devinrhode2

This comment has been minimized.

@devinrhode2
Copy link
Contributor

Ok, turns out that didn't quite work that well. This works:

  "scripts": {
    "xo": "/usr/bin/time node --max-old-space-size=16384 --stack-trace-limit=25 ./node_modules/.bin/xo",
  },
  "lint-staged": {
    "!*.{ts,tsx}": "prettier --write --ignore-unknown",
    "*.{ts,tsx}": "forwardArgs () { echo \"xo cli args: $*\"; yarn xo --fix \"$*\" || true; }; forwardArgs"
  },
// .husky/pre-commit:
yarn lint-staged --concurrent 1 --allow-empty --shell "/bin/bash"

@lkraav
Copy link

lkraav commented Mar 17, 2024

Nice idea. https://stackoverflow.com/a/24538676/35946 is an elegant IIFE-ish shell solution, too.

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

5 participants