简体   繁体   English

如何告诉 prettier 递归解析文件?

[英]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.我想请prettier递归地解析我的 JavaScript 文件,而不是指定它应该检查文件的每个文件夹。

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?有什么建议么?

This issue from a few weeks back seems like it answers your question.几周前的这个问题似乎回答了您的问题。 TL;DR:特尔;博士:

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

Just run prettier --write .只需运行prettier --write .

And use a .prettierignore file to tell it what to ignore, but it ignores node_modules by default.并使用.prettierignore文件告诉它要忽略什么,但默认情况下它会忽略node_modules

If you're like me, you didn't recognize that the target files used globs format.如果你像我一样,你没有意识到目标文件使用了 globs 格式。

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如果您想了解更多信息,请在此处查看: 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/ .我想忽略package.jsonnode_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:就我而言,有 2 个:

1: *.js : target all JS files in the root where the command is executed 1: *.js : 定位执行命令的根目录下的所有JS文件

2: !(node*)**/*.js : target all the JS files in all the folders and subfolders (the recursive part is obtained by using the ** ) 2: !(node*)**/*.js : target所有文件夹和子文件夹中的所有JS文件(递归部分使用**获得)

For cut & pasters that want a straightforward example (no brackets, or s, etc), this could be a useful globs example:对于需要简单示例(没有括号or s 等)的剪切和粘贴,这可能是一个有用的 globs 示例:

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

Prettiers every *.ts file in the app directory and below.美化app目录及以下的每个*.ts文件。

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

The issue for me was that I had to add quotes我的问题是我必须添加引号

somewhere in your package.json package.json 的某处

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

Adding as npm script did the trick for me:添加为 npm 脚本对我有用:

  • package.json defined npm script package.json定义的 npm 脚本

It will recursively fix following files.它将递归修复以下文件。 ext with the following set of rules defined in prettierrc file. ext使用在prettierrc文件中定义的以下规则集。

Important: .<EXTENSION ||重要: .<扩展名 || MULTIPLE_EXTENSIONS> e.g'./src/**/*.{js,jsx,scss,md,json}' (Include all files and folders) MULTIPLE_EXTENSIONS> 例如'./src/**/*.{js,jsx,scss,md,json}'(包括所有文件和文件夹)

  • Don't miss the quotes -> ' PATH>.<EXTENSION ||不要错过引号-> ' PATH>.<EXTENSION || MULTIPLE_EXTENSIONS> ' 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 .prettierrc JSON 文件
{
  "trailingComma": "es5",
  "tabWidth": 4,
  "semi": false,
  "singleQuote": true
}
  • .prettierignore ignore folder/files from formatting .prettierignore从格式中忽略文件夹/文件
/config

# Dependency directories
node_modules/

# Publish Directory
build/

# Root level folders/files
/package.json

If you want the extended glob to work recursively in the package.json file, you need extra quotes.如果您希望扩展 glob 在package.json文件中递归工作,则需要额外的引号。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM