Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a glob example for how to ignore node_modules #1358

Closed
Haroenv opened this issue Apr 20, 2017 · 11 comments · Fixed by #1683
Closed

add a glob example for how to ignore node_modules #1358

Haroenv opened this issue Apr 20, 2017 · 11 comments · Fixed by #1683
Labels
locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:docs Issues about adding or improving documentation

Comments

@Haroenv
Copy link
Contributor

Haroenv commented Apr 20, 2017

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!

@lydell
Copy link
Member

lydell commented Apr 20, 2017

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

@Haroenv
Copy link
Contributor Author

Haroenv commented Apr 20, 2017

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

@vjeux vjeux added the type:docs Issues about adding or improving documentation label Apr 21, 2017
@eliaslfox
Copy link

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

@Haroenv
Copy link
Contributor Author

Haroenv commented Apr 23, 2017

Maybe some way that prettier would ignore node_modules by default

@eliaslfox
Copy link

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

@gwing33
Copy link

gwing33 commented Apr 27, 2017

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

@SEAPUNK
Copy link

SEAPUNK commented Apr 28, 2017

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

@sondr3
Copy link

sondr3 commented May 2, 2017

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.

@obartra
Copy link

obartra commented May 8, 2017

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]

@vvo
Copy link
Contributor

vvo commented May 11, 2017

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: #1048 (comment)

@ryanpcmcquen
Copy link

ryanpcmcquen commented Jan 3, 2018

@SEAPUNK, does this help?

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

@lock lock bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Jul 6, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Jul 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:docs Issues about adding or improving documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants