prettier icon indicating copy to clipboard operation
prettier copied to clipboard

add a glob example for how to ignore node_modules

Open Haroenv opened this issue 9 years ago • 11 comments

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!

Haroenv avatar Apr 20 '17 09:04 Haroenv

What about prettier "{,!(node_modules)/**/}*.js"?

lydell avatar Apr 20 '17 10:04 lydell

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

Haroenv avatar Apr 20 '17 10:04 Haroenv

I find find . -name "*js" ! -name "*node_modules*" | xargs prettier to be a little cleaner

eliaslfox avatar Apr 23 '17 07:04 eliaslfox

Maybe some way that prettier would ignore node_modules by default

Haroenv avatar Apr 23 '17 08:04 Haroenv

That seems like it could cause some unexpected behavior, plus it doesn't really align with the unix philosophy.

eliaslfox avatar Apr 23 '17 08:04 eliaslfox

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

gwing33 avatar Apr 27 '17 19:04 gwing33

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"
   }
}

SEAPUNK avatar Apr 28 '17 21:04 SEAPUNK

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.

sondr3 avatar May 02 '17 16:05 sondr3

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]

obartra avatar May 08 '17 21:05 obartra

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

vvo avatar May 11 '17 07:05 vvo

@SEAPUNK, does this help?

prettier --config .prettierrc --write "{,!(excluded|directories)/**/}*.{js,json,jsx,md}"

ryanpcmcquen avatar Jan 03 '18 22:01 ryanpcmcquen