Agent skill
init-prettier-git-hooks
初始化基于 lint-staged + simple-git-hooks + prettier 的 git 提交前代码格式化流程。用于在任何 Node.js 项目中快速搭建代码格式化和 git 钩子配置。
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/other/init-prettier-git-hooks
Metadata
Additional technical details for this skill
- version
- 0.13.4
SKILL.md
初始化 Prettier + Git Hooks 格式化流程
本技能用于在任何 Node.js 项目中初始化基于 lint-staged + simple-git-hooks + prettier 的 git 提交前代码格式化流程。
1. 必须安装的依赖(devDependencies)
执行以下命令安装所有必要的依赖:
pnpm add -D prettier @prettier/plugin-oxc prettier-plugin-lint-md lint-staged simple-git-hooks commitlint
依赖说明:
| 依赖名称 | 说明 |
|---|---|
prettier |
核心代码格式化工具 |
@prettier/plugin-oxc |
使用 oxc 引擎解析 JS/TS,更快速 |
prettier-plugin-lint-md |
Markdown 文件格式化插件 |
lint-staged |
只对 git 暂存区文件执行 lint/format |
simple-git-hooks |
轻量级 git hooks 管理工具 |
commitlint |
git 提交信息规范校验 |
2. 必须创建/修改的配置文件
2.1. 创建 prettier.config.mjs
从模板复制:参见 templates/prettier.config.mjs
2.2. 创建 lint-staged.config.js
从模板复制:参见 templates/lint-staged.config.js
2.3. 创建 simple-git-hooks.mjs
从模板复制:参见 templates/simple-git-hooks.mjs
2.4. 更新 package.json
需要在 package.json 中添加/修改以下内容:
scripts 部分新增命令:
{
"scripts": {
"format": "prettier --experimental-cli --write .",
"prepare": "<原有的 prepare 命令> && simple-git-hooks"
}
}
说明:
| 命令 | 说明 |
|---|---|
format |
手动格式化整个项目的所有文件 |
prepare |
在 npm install 后自动初始化 git hooks |
注意: 如果项目原本没有 prepare 命令,则直接设置为 "prepare": "simple-git-hooks"。
3. 初始化 Git Hooks
配置文件创建完成后,需要执行以下命令来初始化 git hooks:
# 如果 simple-git-hooks 的 postinstall 脚本被阻止,需要先批准
pnpm approve-builds simple-git-hooks
# 执行初始化 git hooks
npx simple-git-hooks
成功后会看到类似输出:
[INFO] Successfully set the pre-commit with command: npx lint-staged
[INFO] Successfully set the commit-msg with command: npx --no-install commitlint --edit ${1}
[INFO] Successfully set all git hooks
4. 自检清单
完成初始化后,请逐项检查以下内容:
-
1. 依赖安装:
package.json的devDependencies中包含以下依赖:-
prettier -
@prettier/plugin-oxc -
prettier-plugin-lint-md -
lint-staged -
simple-git-hooks -
commitlint(或@commitlint/cli)
-
-
2. 配置文件存在:
-
prettier.config.mjs存在 -
lint-staged.config.js存在 -
simple-git-hooks.mjs存在
-
-
3. package.json scripts:
- 存在
format命令 -
prepare命令包含simple-git-hooks
- 存在
-
4. Git hooks 初始化:
- 执行
npx simple-git-hooks成功
- 执行
-
5. 功能验证:
- 执行
pnpm format能正常格式化代码 - git commit 时会自动触发 lint-staged 格式化
- 执行
5. 工作流程说明
当你执行 git commit 时,会自动触发以下流程:
- pre-commit 钩子触发 → 执行
npx lint-staged - lint-staged 对暂存区的文件执行
prettier --experimental-cli --write - commit-msg 钩子触发 → 执行
npx --no-install commitlint --edit ${1}校验提交信息格式
6. 注意事项
- 修改
simple-git-hooks.mjs后,务必重新执行npx simple-git-hooks命令来更新 git hooks。 - 首次安装依赖时,如果 pnpm 提示
simple-git-hooks的 postinstall 脚本被忽略,需要执行pnpm approve-builds simple-git-hooks来允许其运行。 - Prettier 使用
--experimental-cli参数,这是启用实验性 CLI 功能的必要参数。
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?