简体   繁体   English

如何只在我想要提交的文件上运行 Prettier?

[英]How Do I Run Prettier Only on Files That I Want to Commit?

Using Husky , I've set up my package.json with a precommit hook so that my JavaScript code is formatted using Prettier before every commit:使用Husky ,我使用 precommit 钩子设置了我的package.json ,以便在每次提交之前使用Prettier格式化我的 JavaScript 代码:

{
  "name": "prettier-demo",
  "scripts": {
    "precommit": "prettier --write **/*.js && git add ."
  },
  "devDependencies": {
    "husky": "^0.14.3",
    "prettier": "^1.8.2"
  }
}

This works fine, but there are two drawbacks:这很好用,但有两个缺点:

  1. If I have a large project with thousands of JavaScript files, I have to wait for Prettier to process all of them, even if only a few have changed;如果我有一个包含数千个 JavaScript 文件的大型项目,我必须等待 Prettier 处理所有这些文件,即使只有少数发生了变化; this can take very long and gets on my nerves quickly when it is done with every commit这可能需要很长时间,并且在每次提交完成后都会很快让我感到紧张

  2. Sometimes I want to stage just a couple of files for committing, leaving other changes out of the commit;有时我只想暂存几个文件以进行提交,而将其他更改排除在提交之外; because I do a git add .因为我做了一个git add . after running Prettier, all my changes will always end up in the commit运行 Prettier 之后,我所有的更改都将始终在提交中结束

How can I run Prettier before every commit only on the files that have been staged , ignoring unstaged or unchanged files?如何在每次提交之前仅在已暂存的文件上运行 Prettier,而忽略未暂存或未更改的文件?

You can do that using lint-staged :您可以使用lint-staged来做到这一点:

Linting makes more sense when running before committing your code.在提交代码之前运行时,Linting 更有意义。 By doing that you can ensure no errors are going into repository and enforce code style.通过这样做,您可以确保没有错误进入存储库并强制执行代码样式。 But running a lint process on a whole project is slow and linting results can be irrelevant.但是在整个项目上运行 lint 过程很慢,而且 lint 结果可能无关紧要。 Ultimately you only want to lint files that will be committed.最终,您只想对将要提交的文件进行 lint。

This project contains a script that will run arbitrary npm and shell tasks with a list of staged files as an argument, filtered by a specified glob pattern.该项目包含一个脚本,该脚本将运行任意 npm 和 shell 任务,并将暂存文件列表作为参数,并按指定的 glob 模式进行过滤。

Install lint-staged and husky , which is required for pre-commit hooks, with this command:使用以下命令安装预提交挂钩所需的lint-stagedhusky

npm install --save-dev lint-staged husky

Change your package.json as follows:更改您的 package.json 如下:

{
  "scripts": {
    "precommit": "lint-staged"
  },
  "lint-staged": {
    "*.js": [
      "prettier --write",
      "git add"
    ]
  }
}

I found that just running:我发现刚刚运行:

prettier --write $(git diff --name-only --diff-filter d | grep '\.js$' | xargs)

was quite enough for my needs, just made an alias and used that.足以满足我的需要,只是做了一个别名并使用它。

If you don't want to add the devDependency lint-staged you can also do the same with a Bash script:如果您不想添加 devDependency lint-staged ,您也可以使用 Bash 脚本执行相同操作:

#!/usr/bin/env bash
# chmod +x this and save in your PATH. Assumes `prettier` is in your `devDependencies` already
BASE=$(git merge-base master HEAD) # change master to whatever your trunk branch is
FILES=$(git diff --name-only $BASE HEAD | xargs)

npx prettier --list-different $FILES

# Want eslint too?
# npx eslint --ignore-path=.prettierignore $FILES

I use this package pretty-quick我用这个包很快

add add a script in my package.json在我的package.json添加脚本

"pretty-quick": "pretty-quick" 

under scripts {}scripts {}

Then in my pre-commit hook under .husky/pre-commit然后在我的.husky/pre-commit下的pre-commit钩子中

I add我加

npm run pretty-quick

The prettier docs has a section on this.更漂亮的文档对此有一个部分

I use pretty-quick我用相当快

npx husky-init
npm install --save-dev pretty-quick
npx husky set .husky/pre-commit "npx pretty-quick --staged"

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

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