add a glob example for how to ignore node_modules
I tried some globs, but none work, so adding this as an example somewhere will make my day!
**/*.js // matches node_modules too
!**/node_modules/*.js // matches non-.js files too
!(**/node_modules|**)/*.js // matches everything
http://globtester.com was useful, but not enough 😄
Thanks a lot!
What about prettier "{,!(node_modules)/**/}*.js"?
That works to ignore when node_modules only exists in one folder, which is useful already, but I'd like to find a solution without xargs preprocessing for the case when the node_modules folder can be in a subfolder.
find . -name "*.js" | grep -v node_modules | xargs prettier --write --single-quote --trailing-comma es5
is what I found now
I find find . -name "*js" ! -name "*node_modules*" | xargs prettier to be a little cleaner
Maybe some way that prettier would ignore node_modules by default
That seems like it could cause some unexpected behavior, plus it doesn't really align with the unix philosophy.
For lint-staged setup, using "{,!(node_modules)/}**/*.{js,jsx}" is what is seeming to work for me.
For reference, our setup has node_modules in git versioning...which is why I needed this. Abnormal use case. facepalm
I'm trying to lint all files ending in .js in my current directory, but I can't seem to create a single glob pattern that matches ALL files in the current directory, excluding multiple directories (node_modules, dist), and manually including some dot directories (storybook/.storybook/).
There has to be a way that's both performant and not limiting.
In the meantime, I ended up writing a script that generates a glob based on the directories in my project folder, including and excluding the files I want:
// Generates a glob string for prettier
const fs = require('fs');
const path = require('path');
// EXCLUDE and INCLUDE must be directories, and not contain trailing slashes
// This script does not create globs that ignore individual files
const EXCLUDE = ['node_modules', 'dist'];
const INCLUDE = ['storybook/.storybook'];
let dirs = fs
.readdirSync(path.join(__dirname, '../../'))
.filter(path => fs.statSync(path).isDirectory())
.filter(dir => dir.indexOf('.') !== 0)
.filter(dir => EXCLUDE.indexOf(dir) === -1);
dirs = dirs.concat(INCLUDE).map(dir => `${dir}/**/`);
const glob = `{,${dirs.join(',')}}*.js`;
process.stdout.write(`${glob}`);
and my package.json:
{
// ...
"scripts": {
// ...
"prettier": "prettier $(node genPrettierGlob.js) --write"
}
}
Sort of related, but I wish there was a way to configure Prettier to ignore specific directories. I've previously used XO, but like Prettier more but I use it with a Yeoman generator and each directory has a template subdirectory that is templated with EJS and Prettier hangs itself up on these files each time. Being able to configure which directories to ignore would be nice, otherwise I end up doing prettier 'generators/app/!(templates)' for each subgenerator. Would be nice to just set this in package.json so I can just run prettier --write instead of having the long string of ignored directories.
Building on @Haroenv's answer, I found the following useful to exclude all .gitignore'd paths:
find . -name \"*.js\" | grep -v -f .gitignore | xargs prettier [opts]
Given the complexity of those globs and the fact that in 99% of situations you will never want to go though node_modules, could we ignore them by default?
Like proposed by @jlongster here: https://github.com/prettier/prettier/pull/1048#issuecomment-287823726
@SEAPUNK, does this help?
prettier --config .prettierrc --write "{,!(excluded|directories)/**/}*.{js,json,jsx,md}"