SuperAPI AI Directory

init-prettier-git-hooks

Coding & development

View repository

Updated 10-16 09:59

About this skill

Initialize Prettier + Git Hooks

This skill is audited by AI and then modified file by file at specific points in the target project; it distributes only operational instructions, six configuration templates, and historical references. It must not create or invoke a migration CLI, runtime code, or bulk overwrite tools. First confirm configuration ownership and user changes, then perform the minimal modification.

Highest-Priority Contract

  1. prettier-plugin-lint-md Must precisely use 1.0.1:package.json The declared version, the lockfile-resolved version, and the runtime-resolved version must be consistent. Prohibit ^1.0.1、~1.0.1 or 1.0.3。

  2. Use a string at the top level of the single effective Prettier configuration:

    plugins: ["prettier-plugin-lint-md"],
    

    Prohibit top-level object imports of lint-md, and prohibit registering only in a Markdown override. Do not use a single entry point partially passing to overturn these two contracts.

For the reasons and deprecated approaches, see references/version-matrix.md、references/runtime-loading-model.md and references/decision-evolution.md。

1. Confirm the operation root, configuration ownership, and user boundaries

  1. Locate the root from the current directory upward package.json. For a pnpm monorepo, use the directory containing pnpm-workspace.yaml as the root; for an npm/yarn workspace, use the directory containing the workspaces that declares package.json as the root.
  2. Read the root package.json, the lockfile, and the five target configurations, and use git status --short、git diff、git diff --cached to check Git status. Existing changes belong to the user by default; do not overwrite, roll back, stage, or include unrelated modifications.
  3. Scan and read one by one the root package.json field, prettier ,prettier.config.* and .prettierrc*. If multiple active sources are found, the inheritance relationship is unclear, or a single effective configuration cannot be determined, stop and ask the user to decide; do not create a new dual configuration.
  4. Before modifying, list the files that will be created or modified at specific points. Actions that modify package, lockfile,git add --renormalize . , or the staging area, such as dependency installation, hook installation, and.git/hooks , must obtain the user's explicit authorization.

2. Six Templates

  • templates/.editorconfig
  • templates/.gitattributes
  • templates/prettier.config.mjs
  • templates/lint-staged.config.mjs
  • templates/simple-git-hooks.mjs
  • templates/cloud-pr-prettier.yml(the GitHub PR cloud formatting workflow that must be installed)

When a target file is missing, the corresponding template may be copied; when it exists, it must be merged file by file at specific points, preserving project-specific rules and incident explanation comments, and the entire file must not be overwritten. In particular, the complete JSDoc above the top-level lint-md prettier.config.mjs in plugins is a protected knowledge block: it records version drift, the object approach, the override approach, and the corrections for the three entry points. It must not be deleted, compressed, moved to an irrelevant location, or changed into ordinary line comments; when adjustments are needed, first update the corresponding references/, then keep the JSDoc consistent with the current contract.

3. Precise dependencies and active commands

At the root devDependencies check prettier、@prettier/plugin-oxc、prettier-plugin-lint-md、lint-staged、simple-git-hooks. Only when the project already has a valid commitlint configuration, check and use @commitlint/cli; otherwise do not generate commit-msg。

Only after user authorization, use precise dependency installation commands:

pnpm add -Dw prettier @prettier/plugin-oxc prettier-plugin-lint-md@1.0.1 lint-staged simple-git-hooks

After installation, do not look only at dependency fields: read the lockfile, and resolve from the project execution root prettier-plugin-lint-md/package.json; all three places must be 1.0.1. The "package not found" issue under pnpm's strict isolation and the version entry point error are different problems; route them according to references/pnpm-resolution.md .

The ordinary CLI, the experimental CLI, and VSCode are three different loading entry points:

  • Ordinary CLI: used for basic formatting and string plugin discovery.
  • experimental CLI: the active command retains one and only one --no-parallel。
  • VSCode: use the workspace Prettier and the top-level resolveConfig().plugins; after modifying dependencies or resolution rules, the extension window must be restarted.

package.json The experimental active command form is as follows; when prepare already exists, keep the original command and concatenate simple-git-hooks:

{
	"scripts": {
		"format": "prettier --experimental-cli --write --no-parallel .",
		"prepare": "simple-git-hooks"
	}
}

The default active command does not repeatedly pass in --plugin: A/B experiments prove that after the experimental CLI discovers the config upward from the root cwd, the top-level string can already automatically load lint-md. Explicit --plugin serves only as a diagnostic/isolation verification means for plugin resolution failures, not as a more robust production configuration. For command boundaries, see references/runtime-loading-model.md。

4. Per-file processing

Prettier

Preserve the existing project style and overrides, ensuring endOfLine: "lf". lint-md must be the only effective top-level string element of the configuration:

export default {
	plugins: ["prettier-plugin-lint-md"],
	endOfLine: "lf",
};

Only when the top-level plugins of the sole effective configuration is a locatable static array may AI migrate the exact object import or string within it into the aforementioned string element. During migration, handle only lint-md, preserving other literal plugins, the original order, and comments. For dynamic plugins, spread, computed keys, variable indirection, multiple configurations, or when the top-level configuration cannot be uniquely located, stop and ask the user to decide; do not use regex to guess the migration.

package.json#prettier、JSON/JSONC/YAML .prettierrc* or CJS configuration can likewise express a string plugin; if the sole effective configuration does not have a clear and safe pinpoint modification location, stop and ask the user to decide whether to migrate to prettier.config.mjs. Do not migrate the configuration to ESM just for an object import.

For JSON with comments, add parser: "jsonc" override only for the exact file list; it is forbidden to change all **/*.json to JSONC, and the incident explanation comments must not be deleted to bypass formatting issues.

LF three layers

  1. .gitattributes global text rules converge to * text=auto eol=lf, preserving binary and path-specific rules.
  2. .editorconfig the [*] block converges to end_of_line = lf, preserving other blocks.
  3. prettier.config.mjs converges to endOfLine: "lf"。

Changing only .gitattributes will not refresh the Git index.git add --renormalize . will modify the staging area; the impact must be shown and authorization obtained, and multiple branches must be handled separately. Do not conflate the CRLF issue with lint-md not being loaded as the same root cause.

lint-staged and Hooks

lint-staged.config.mjs uses prettier --experimental-cli --write --no-parallel。simple-git-hooks.mjs by default only configures pre-commit; only after confirming an existing valid commitlint configuration may commit-msgbe retained or added. When encountering Husky, lefthook, custom core.hooksPath , or other hook managers, stop and ask the user to decide; do not overwrite.

The optional post-commit incident comment in the template must be preserved; this command may overwrite unstaged changes to the same file and must not be enabled by default.

5. Verification

Do not run only prettier --check; it may still pass when the plugin is not loaded. Verify with a temporary directory or temporary sample, then clean up:

# 版本三层一致性:依赖声明、lockfile、运行时解析均为 1.0.1
pnpm exec prettier --write <Markdown 样例>
pnpm exec prettier --experimental-cli --no-parallel --write <Markdown 样例>
node --check lint-staged.config.mjs
git diff --check
git status --short

Both CLIs must prove that the same mixed Chinese-English and numeric Markdown sample actually undergoes lint-md rule transformation. VSCode acceptance must confirm that the extension uses the workspace Prettier,resolveConfig(真实文件路径).plugins contains a top-level string, and editor formatting of the same sample produces the same result. For the complete script, see references/verification-playbook.md。

pnpm exec lint-staged --debug is not a read-only check: it executes lint-staged tasks and may rewrite files, the staging area, and trigger the stash flow. Only after user authorization, run this command,pnpm exec simple-git-hooks, real commit verification, or git add --renormalize .; before and after execution, git status --short、git diff and git diff --cached。

6. GitHub PR cloud formatting (unconditional installation)

Copy templates/cloud-pr-prettier.yml unconditionally to the target project's .github/workflows/; do not treat this template as an optional installation item. In the security job for same-repository PRs, after the template checks out the PR head it must unconditionally run pnpm install --frozen-lockfile, and must not rely on setup-js 's auto-install or skip dependency installation via a condition; fork PRs still skip the write-back job according to the security boundary below. This workflow must satisfy the following boundaries:

  1. Use the pull_request event (opened、synchronize、reopened、ready_for_review) and concurrency keyed by PR number; do not switch to pull_request_targetin order to obtain write permissions, and do not execute code from a fork without checking repository ownership.
  2. Commits and pushes are allowed only when github.event.pull_request.head.repo.full_name == github.repository . Fork PRs may only run read-only checks, and untrusted code and write permissions must not be placed in the same job.
  3. Do not reference the target repository's local composite actions or monorepo-specific initialization actions. Environment initialization follows sxzz setup-js 's portable order: checkout PR head → pnpm/action-setup → actions/setup-node(Node version, pnpm cache) → pnpm install. Use --frozen-lockfileby default; only when there is no lockfile or the project explicitly requires an exception may the project maintainer explicitly disable it.
  4. The workflow should identify the Node/pnpm version from package.json#packageManager or the repository's existing configuration; do not write a particular monorepo's private package names, filters, scripts, or action paths into the general template. Action versions should be pinned to reviewed versions (production repositories should prefer immutable SHAs and retain version comments).
  5. Obtain only the formattable files between base.sha...head , and invoke the project-local pnpm exec prettier --experimental-cli --no-parallel --writeon each one; do not format the entire repository, and do not add lockfiles, generated artifacts, or files the user did not modify to the commit.
  6. After formatting, first run git diff --cached --checkon the exact file list. If there are no changes, do not commit; if there are changes, commit using the github-actions[bot] identity and push only the PR head branch.permissions At minimum, explicitly declare contents: write、pull-requests: read, and keep the job-level same-repository condition.

For the general template and security/portability notes, see references/pr-workflow.md。