node.jsglobprettier-eslint

Why is my glob in my npm script not working?


I am trying to create a regex glob for an NPM script. This is what I have:

"format": "prettier-eslint --write \"{,!(node_modules|cypressTests)/**/}*.{js,json,vue}\""

And this currently formats all .js, .json, and .vue files that are NOT in the node_modules folder or in the cypressTests folder.

The below is my problem:

The cypressTests folder ALSO contains a node_modules folder that I do not want to format. How can I exclude ./cypressTests/node_modules just like I am currently doing for the folder ./node_modules?

I tried like this and this does not work. It then excludes pretty much everything in the entire project for some reason:

"format": "prettier-eslint --write \"{,!(node_modules|cypressTests/node_modules)/**/}*.{js,json,vue}\""

Solution

  • Prettier has an easier solution to ignore files. You can make a .prettierignore file (which uses .gitignore syntax) to ignore files when prettifying. The file should be placed in the root directory of your project. I believe the syntax inside the file in your case would be:

    **/node_modules
    

    Which is actually what prettier ignores by default (among some other source-control folder exclusions)-- so unless you already have a .prettierignore file, all node_modules folders should already be excluded from prettification.

    The command would then simply be:

    "format": "prettier-eslint --write \"**/*.{js,json,vue}\""
    

    or if you want your .prettierignore file to be somewhere else:

    "format": "prettier-eslint --ignore-path <path-to-ignore-file> --write \"**/*.{js,json,vue}\""
    

    If this solution doesn't work for you and you need to use the glob method, I wasn't quite able to figure that out, but this website was handy for testing globs.