Paul
Paul

Reputation: 7187

How can I tell prettier to parse files recursively?

I'd like to ask prettier to parse my JavaScript files recursively instead of specifying each folder in which it should check the files.

I'd like to do something like:

prettier --write "all_js_files_except_node_modules_folder"

I don't find much into their documentation. Any suggestions?

Upvotes: 34

Views: 14982

Answers (8)

ruffin
ruffin

Reputation: 17473

How to run recursively (answering the question's title)

For cut & pasters who came here for a straightforward example (no brackets, ors, etc) that answers the question in the title, the following could be a useful globs example:

prettier --write "app/**/*.ts"

That command recursively runs prettier for all *.ts files under the app directory.


How to ignore folders [while running recursively] (answering the question's details)

As נשמה קשוחה (tough soul?) has noted, from the command line prettier already ignores node_modules folders:

Prettier CLI will ignore files located in node_modules directory. To opt out from this behavior, use --with-node-modules flag.

If you wanted to ignore another folder, soul again has the right idea by suggesting a .prettierignore file, which will be used by the CLI.

Include...

**/folderToIgnore

... in your .prettierignore (note leading . in .prettierignore!!!) file to ignore EVERY folder with the name folderToIgnore, regardless of path.

As long as .prettierignore is in the same folder or upstream from your files' path, you should be good, though look out for interactions if you have multiple .prettierignores.

Upvotes: 7

Filip Seman
Filip Seman

Reputation: 1744

If you want the extended glob to work recursively in the package.json file, you need extra quotes.

....
"scripts": {
    ....
    "format": "prettier './**/*.ts' --write --ignore-path ./.gitignore"
},

Upvotes: 1

Petros Kyriakou
Petros Kyriakou

Reputation: 5343

The issue for me was that I had to add quotes

somewhere in your package.json

"format": "prettier --write \"src/**/*.ts\""

Upvotes: 3

khizerrehandev
khizerrehandev

Reputation: 1535

Adding as npm script did the trick for me:

  • package.json defined npm script

It will recursively fix following files.ext with the following set of rules defined in prettierrc file.

Important: .<EXTENSION || MULTIPLE_EXTENSIONS> e.g'./src/**/*.{js,jsx,scss,md,json}' (Include all files and folders)

  • Don't miss the quotes -> ' PATH>.<EXTENSION || MULTIPLE_EXTENSIONS> '
{
    "name": "compentence-journey",
    "version": "0.1.0",
    "private": true,
    "scripts": {
    "prettier.fix": "prettier --check --write './src/**/*.{js,jsx,scss,md,json}'",
    },
   ...

}
  • .prettierrc JSON file
{
  "trailingComma": "es5",
  "tabWidth": 4,
  "semi": false,
  "singleQuote": true
}
  • .prettierignore ignore folder/files from formatting
/config

# Dependency directories
node_modules/

# Publish Directory
build/

# Root level folders/files
/package.json

Upvotes: 2

P Varga
P Varga

Reputation: 20269

Just run prettier --write .

And use a .prettierignore file to tell it what to ignore, but it ignores node_modules by default.

Upvotes: 21

jrnxf
jrnxf

Reputation: 974

This solution works best for me. It's the only method I've found that will hit every file regardless of how nested it is:

package.json

  "scripts": {
    "format": "prettier --write {,*/**/}*.{js,jsx,json}",
  },

.prettierignore

node_modules
build

Upvotes: 1

Paul
Paul

Reputation: 7187

If you're like me, you didn't recognize that the target files used globs format.

You can read more about it here. (Check the explanation about special chars). If you want to learn much more check here: http://tldp.org/LDP/abs/html/globbingref.html

My folder structure:

components/
pages/
node_modules/
package.json    
server.js

I want to ignore package.json and node_modules/. I solved my needs with this command:

prettier --write "{*.js,!(node*)**/*.js}"`

The {} is an expansion mechanism which allows me to use several filter criteria. In my case there were 2:

1: *.js: target all JS files in the root where the command is executed

2: !(node*)**/*.js: target all the JS files in all the folders and subfolders (the recursive part is obtained by using the **)

Upvotes: 8

Joe Clay
Joe Clay

Reputation: 35837

This issue from a few weeks back seems like it answers your question. TL;DR:

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

Upvotes: 33

Related Questions