3

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}\""
mcool
  • 457
  • 4
  • 29

1 Answers1

1

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.

Feathercrown
  • 2,547
  • 1
  • 16
  • 30
  • 1
    Your solution worked perfect. I struggled with the glob for countless hours. Different glob testers would even show me different results so I'm happy to use .prettierignore instead. thanks! – mcool Oct 20 '22 at 21:43
  • 1
    @mcool No problem, I'm glad I could help! Good luck with the project! :) – Feathercrown Oct 21 '22 at 17:26