Skip to content

Commit a5c36f5

Browse files
authored
Add simplified Chinese documentation (#1290)
* build: update vitepress to 1.0.0-beta.6 version * docs: add simplified Chinese translation
1 parent 3dd4ea2 commit a5c36f5

File tree

8 files changed

+1345
-305
lines changed

8 files changed

+1345
-305
lines changed

docs/.vitepress/config.ts

+20
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,24 @@ export default defineConfig({
2222
{ text: 'Migrating from v4', link: '/migrating-from-v4' },
2323
],
2424
},
25+
locales: {
26+
root: {
27+
label: 'English',
28+
lang: 'en',
29+
},
30+
zh: {
31+
label: '简体中文',
32+
lang: 'zh-CN',
33+
link: '/zh/',
34+
themeConfig: {
35+
sidebar: [
36+
{ text: '介绍', link: '/zh/' },
37+
{ text: '开始使用', link: '/zh/getting-started' },
38+
{ text: '指南', link: '/zh/guide' },
39+
{ text: '疑难解答', link: '/zh/troubleshooting' },
40+
{ text: '从 v4 迁移', link: '/zh/migrating-from-v4' },
41+
],
42+
},
43+
}
44+
}
2545
})

docs/zh/getting-started.md

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
# 开始使用
2+
3+
## 自动 <Badge type="tip" text="推荐" />
4+
5+
`husky-init` 是一个一次性命令,用于快速初始化一个带有 husky 的项目。
6+
7+
::: code-group
8+
9+
```shell [npm]
10+
npx husky-init && npm install
11+
```
12+
13+
```shell [pnpm]
14+
pnpm dlx husky-init && pnpm install
15+
```
16+
17+
```shell [yarn]
18+
yarn dlx husky-init --yarn2 && yarn
19+
```
20+
21+
:::
22+
23+
它将会:
24+
25+
1. 添加 `prepare` 脚本到 `package.json`
26+
2. 创建一个你能够编辑的 `pre-commit` hook 示例(默认情况下,`npm test` 将在你提交时运行)
27+
3. 配置 Git hooks 路径
28+
29+
要添加另一个 hook,请使用 `husky add`。例如:
30+
31+
```shell
32+
npx husky add .husky/commit-msg 'npx --no -- commitlint --edit "$1"'
33+
```
34+
35+
::: info 注意
36+
对于 Windows 用户,如果在运行 `npx husky add ...` 时看到帮助信息,请尝试使用 `node node_modules/husky/lib/bin add ...`。这不是 husky 代码的问题。
37+
:::
38+
39+
## 手动
40+
41+
### 安装
42+
43+
1. 安装 `husky`
44+
45+
```shell
46+
npm install husky --save-dev
47+
```
48+
49+
2. 启用 Git hooks
50+
51+
```shell
52+
npx husky install
53+
```
54+
55+
3. 要在安装后自动启用 Git hooks,请编辑 `package.json`
56+
57+
```shell
58+
npm pkg set scripts.prepare="husky install"
59+
```
60+
61+
你应该这样做:
62+
63+
::: code-group
64+
65+
```json [package.json]
66+
{
67+
"scripts": {
68+
"prepare": "husky install" // [!code hl]
69+
}
70+
}
71+
```
72+
73+
:::
74+
75+
::: info 注意
76+
Yarn 2+ 不支持编写生命周期脚本,所以 husky 需要以不同的方式安装(但这不适用于 Yarn1)。参见 [Yarn 2+ 安装](#yarn-2)
77+
:::
78+
79+
## 创建一个 hook
80+
81+
要向 hook 添加命令或创建新的 hook,可以使用 `husky add <file> [cmd]`(再此之前不要忘记执行 `husky install`)。
82+
83+
```shell
84+
npx husky add .husky/pre-commit "npm test"
85+
git add .husky/pre-commit
86+
```
87+
88+
尝试提交
89+
90+
```shell
91+
git commit -m "Keep calm and commit"
92+
```
93+
94+
如果 `npm test` 命令执行失败,你的提交会被自动终止。
95+
96+
::: warning 警告
97+
**想要使用 Yarn 运行命令?Git Bash 在 Windows 上有个问题,请参阅 [在 Windows 上使用 Yarn](./troubleshooting.md#在-windows-上使用-yarn)**
98+
:::
99+
100+
_对于 Windows 用户,如果在运行 `npx husky add ...` 时看到帮助信息,请尝试使用 `node node_modules/husky/lib/bin add ...`。这不是 husky 代码的问题,在 npm 8 的最新版本已经修复了。_
101+
102+
### 卸载
103+
104+
```shell
105+
npm uninstall husky && git config --unset core.hooksPath
106+
```
107+
108+
## Yarn 2
109+
110+
### 安装
111+
112+
1. 安装 `husky`
113+
114+
```shell
115+
yarn add husky --dev
116+
yarn add pinst --dev # ONLY if your package is not private
117+
```
118+
119+
2. 启用 Git hooks
120+
121+
```shell
122+
yarn husky install
123+
```
124+
125+
3. 要在安装后自动启用 Git hooks,请编辑 `package.json`
126+
127+
::: code-group
128+
129+
```js [package.json]
130+
{
131+
"private": true, // ← 你的 package 是私有的,你只需要 postinstall
132+
"scripts": {
133+
"postinstall": "husky install"
134+
}
135+
}
136+
```
137+
138+
:::
139+
140+
::: tip 提示
141+
如果您的软件包不是私有的,并且您正在像 [npmjs.com](https://npmjs.com) 这样的仓库上发布它,那么您需要使用 [pinst](https://github.com/typicode/pinst)\*\* 禁用 postinstall 脚本。否则,当有人安装你的软件包并导致错误时,将运行 postinstall。
142+
:::
143+
144+
::: code-group
145+
146+
```js [package.json]
147+
{
148+
"private": false, // ← 你的 package 是共有的
149+
"scripts": {
150+
"postinstall": "husky install",
151+
"prepack": "pinst --disable",
152+
"postpack": "pinst --enable"
153+
}
154+
}
155+
```
156+
157+
:::
158+
159+
### 卸载
160+
161+
`package.json` 中移除 `"postinstall": "husky install"`,并执行:
162+
163+
```shell
164+
yarn remove husky && git config --unset core.hooksPath
165+
```

docs/zh/guide.md

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# 指南
2+
3+
## Monorepo
4+
5+
建议在根目录下的 `package.json` 中添加 husky。你可以使用诸如 [lerna](https://github.com/lerna/lerna) 和 filter 之类的工具来仅在已更改的包中运行脚本。
6+
7+
## 自定义目录
8+
9+
如果你想在另一个目录安装 husky,比如 `.config` 目录,你可以在 `install` 命令后面添加参数。示例如下:
10+
11+
::: code-group
12+
13+
```js [package.json]
14+
{
15+
"scripts": {
16+
"prepare": "husky install .config/husky"
17+
}
18+
}
19+
```
20+
21+
:::
22+
23+
另一种情况,如果你的 `package.json` 文件和 `.git` 目录不在同一级目录。例如,`project/.git``project/front/package.json`
24+
25+
设计上,`husky install` 必须运行在于 `.git` 相同的目录,但是你可以通过在 `prepare` 脚本中传入一个子目录来改变目录:
26+
27+
::: code-group
28+
29+
```js [package.json]
30+
{
31+
"scripts": {
32+
"prepare": "cd .. && husky install front/.husky"
33+
}
34+
}
35+
```
36+
37+
:::
38+
39+
在你的 hooks 中,你也需要去更改目录:
40+
41+
::: code-group
42+
43+
```shell [.husky/pre-commit]
44+
# ...
45+
cd front
46+
npm test
47+
```
48+
49+
:::
50+
51+
## 绕过 hooks
52+
53+
你能使用 Git命令的 `-n/--no-verify` 选项来绕过 `pre-commit``commit-msg` hooks:
54+
55+
```shell
56+
git commit -m "yolo!" --no-verify
57+
```
58+
59+
对于没有使用 `--no-verify` 选项的 Git 命令,你可以使用 `HUSKY` 环境变量:
60+
61+
```shell
62+
HUSKY=0 git push # yolo!
63+
```
64+
65+
## 在 CI/Docker/Prod 中禁用 husky
66+
67+
在 CI/Docker/Prod上下文中禁用 husky 没有对错之分,这在很大程度上 **取决于你的使用情况**
68+
69+
### 使用 npm
70+
71+
如果你想阻止 husky 完全安装
72+
73+
```shell
74+
npm ci --omit=dev --ignore-scripts
75+
```
76+
77+
或者,你也能明确地禁用 `prepare` 脚本
78+
79+
```shell
80+
npm pkg delete scripts.prepare
81+
npm ci --omit=dev
82+
```
83+
84+
### 使用自定义脚本
85+
86+
您可以创建一个自定义 JS 脚本,有条件地要求使用 husky 和安装 hooks。
87+
88+
::: code-group
89+
90+
```json [package.json]
91+
"prepare": "node ./prepare.js"
92+
```
93+
94+
```js [prepare.js]
95+
const isCi = process.env.CI !== undefined
96+
if (!isCi) {
97+
require('husky').install()
98+
}
99+
```
100+
101+
:::
102+
103+
或者在未安装 husky 的情况下,让 `prepare` 脚本无声地失败:
104+
Or make `prepare` script fail silently if husky is not installed:
105+
106+
```json [package.json]
107+
"prepare": "node -e \"try { require('husky').install() } catch (e) {if (e.code !== 'MODULE_NOT_FOUND') throw e}\""
108+
```
109+
110+
### 使用环境变量
111+
112+
你可以在你的 CI 配置文件中,将 `HUSKY` 环境变量设置为 `0`,来禁用 hooks 安装。
113+
114+
另外,大多数持续集成服务器都会设置一个 `CI` 环境变量。你可以在钩子中使用它来检测是否在 CI 中运行。
115+
116+
::: code-group
117+
118+
```shell [.husky/pre-commit]
119+
# ...
120+
[ -n "$CI" ] && exit 0
121+
```
122+
123+
:::
124+
125+
### 使用 is-ci
126+
127+
您还可以在 `prepare` 脚本中使用 [is-ci](https://github.com/watson/is-ci),有条件地安装 husky
128+
129+
```shell
130+
npm install is-ci --save-dev
131+
```
132+
133+
::: code-group
134+
135+
```js [package.json]
136+
{
137+
"scripts": {
138+
"prepare": "is-ci || husky install"
139+
}
140+
}
141+
```
142+
143+
:::
144+
145+
## 测试 hooks
146+
147+
如果要测试 hook,可以在脚本末尾添加 `exit 1` 来终止 git 命令。
148+
149+
::: code-group
150+
151+
```shell [.husky/pre-commit]
152+
# ...
153+
exit 1 # Commit will be aborted
154+
```
155+
156+
:::
157+
158+
## Git-flow
159+
160+
如果使用 [git-flow](https://github.com/petervanderdoes/gitflow-avh/),需要确保 git-flow hooks 目录设置为使用 husky(默认为 `.husky`)。
161+
162+
```shell
163+
git config gitflow.path.hooks .husky
164+
```
165+
166+
**注意:**
167+
168+
- 如果在安装 husky 之后配置 git-flow,那么 git-flow 设置过程将正确地建议使用 `.husky` 目录。
169+
- 如果您已经为 husky 设置了一个 [自定义目录](#自定义目录),那么您需要指定这个目录(比如 `git config gitflow. path.hooks. config/husky`)
170+
171+
要将 Git-flow hook 目录 **恢复** 到默认目录,需要重置配置,使其指向默认的 Git hook 目录。
172+
173+
```shell
174+
git config gitflow.path.hooks .git/hooks
175+
```

0 commit comments

Comments
 (0)