Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Husky/lint-staged is it possible to exclude/ignore file?

Is it possible to exclude/ignore a file when using Husky/lint-staged hooks?

Looking through the docs atm but not having any luck finding anything on this.

Was hoping there was something like an

/*ignore*/

tag that I could add.

To make lint-staged ignore certain files that were causing formatting issues.

Any thought on this greatly appreciated :)

like image 381
uidevthing Avatar asked Apr 01 '19 14:04

uidevthing


6 Answers

Ended up adding

.prettierignore 

file.

Not ideal but seems to do the job ok.

like image 183
uidevthing Avatar answered Oct 04 '22 11:10

uidevthing


While configuring lint-staged in package.json or If you're using any other IDE, in order to ignore/exclude files by lint-Staged and husky hooks, you can add an "ignore" key in the lint-staged object to make it ignore whatever packages or files you want to ignore. Use the following extensible syntax:

{
  "lint-staged": {
    "linters": {
      "src/**/*.js": ["formatter --write", "git add"],
    },
    "ignore": ["node_modules", "dist", "package-lock.json"]  }
}

Just add the target pattern to 'linters' object and all the ignored files which you might be adding previously to .prettierignore to "ignore" key of lint-Staged object. And there you go!

like image 24
Ayesha Iftikhar Avatar answered Oct 04 '22 12:10

Ayesha Iftikhar


I finally found out how to do this (at least as of lint-staged v11.1.2)

In package.json:

"lint-staged": {
  "!(path/to/excluded/dir/**/*)*.ts": [
    "eslint --fix",
    "prettier --write"
  ]
}

Note the globstar pattern is within the negation pattern and not outside it. This ensures that subdirectories are also excluded.

like image 24
Hans Fredric Waadeland Avatar answered Oct 04 '22 13:10

Hans Fredric Waadeland


If anyone still looking, take a look at this https://github.com/okonet/lint-staged#filtering-files It has good examples.

Filtering files

Linter commands work on a subset of all staged files, defined by a glob pattern. `lint-staged´ uses micromatch for matching files with the following rules:

  • If the glob pattern contains no slashes (/), micromatch's matchBase option will enabled, so globs match a file's basename regardless of directory:
    • "*.js" will match all JS files, like /test.js and /foo/bar/test.js
    • "!(*test).js". will match all JS files, except those ending in test.js, so foo.js but not foo.test.js
  • If the glob pattern does contain a slash (/), it will match for paths as well:
    • "./*.js" will match all JS files in the git repo root, so /test.js but not /foo/bar/test.js
    • "foo/**/\*.js" will match all JS files inside the/foodirectory, so/foo/bar/test.jsbut not/test.js
like image 27
Aamir Afridi Avatar answered Oct 04 '22 11:10

Aamir Afridi


So I've been trying to find an answer for this for an entire day and looking at all the forums suggested that they use minimatch for glob check which might have been correct for older versions but they use micromatch for new version and to solve this issue we can use their pattern to exclude certain directories So in your .lintstagedrc you can add the following pattern to avoid certain folders

{
  "*.{json,md,html,scss}": ["prettier --write", "git add"],
  ["**/!(folder1|folder2)/*.ts"]: ["tslint --project tsconfig.json -c tslint.commit.json --fix", "prettier --write", "git add"]
}

So the glob here is an actual array and make sure not to pass this array within a string else it won't recognize the patterns also do not include **/*.ts the reason being lint-staged automatically converts this into a matchBase comparision if it finds / in the pattern so including this will also match against your folder1|folder2 files.

like image 26
saNiks Avatar answered Oct 04 '22 11:10

saNiks


can fix in three ways:

  • .lintstagedrc.js
  • .prettierignore
  • lint-staged.config.js

more info : https://github.com/okonet/lint-staged/issues/797

like image 45
Nickbing Lao Avatar answered Oct 04 '22 13:10

Nickbing Lao