|
2 | 2 |
|
3 | 3 | ## Summary |
4 | 4 |
|
5 | | -Add `vp config` and `vp staged` as built-in commands. `vp config` is a `prepare`-lifecycle command that installs git hook shims (husky-compatible reimplementation, not a bundled dependency). `vp staged` bundles lint-staged and reads config from the `staged` key in `vite.config.ts`. Projects get a zero-config pre-commit hook that runs `vp check --fix` on staged files — no extra devDependencies needed. |
| 5 | +Add `vp config` and `vp staged` as built-in commands. `vp config` is a lifecycle command (`prepare` or `postinstall`) that installs git hook shims (husky-compatible reimplementation, not a bundled dependency). `vp staged` bundles lint-staged and reads config from the `staged` key in `vite.config.ts`. Projects get a zero-config pre-commit hook that runs `vp check --fix` on staged files — no extra devDependencies needed. |
6 | 6 |
|
7 | 7 | ## Motivation |
8 | 8 |
|
@@ -55,24 +55,27 @@ Flags: `--hooks` (force), `--no-hooks` (skip) |
55 | 55 |
|
56 | 56 | Flags: `--hooks` (force), `--no-hooks` (skip) |
57 | 57 |
|
58 | | -### Ongoing use: `vp config` (prepare lifecycle) |
| 58 | +### Ongoing use: `vp config` (lifecycle script) |
59 | 59 |
|
60 | | -`vp config` is the command that runs on every `npm install` via the `prepare` script. It reinstalls hook shims — it does **not** create the `staged` config or the pre-commit hook file. Those are created by `vp create`/`vp migrate`. |
| 60 | +`vp config` is the command that runs on every `npm install` via the `prepare` or `postinstall` script. It reinstalls hook shims — it does **not** create the `staged` config or the pre-commit hook file. Those are created by `vp create`/`vp migrate`. |
61 | 61 |
|
62 | 62 | ```json |
63 | 63 | { "scripts": { "prepare": "vp config" } } |
| 64 | +// or |
| 65 | +{ "scripts": { "postinstall": "vp config" } } |
64 | 66 | ``` |
65 | 67 |
|
66 | | -When `npm_lifecycle_event=prepare` (set by npm/pnpm/yarn during `npm install`), agent setup is skipped automatically — only hooks are reinstalled. |
| 68 | +When running from a lifecycle script (`npm_lifecycle_event` is `prepare` or `postinstall`), hooks are installed automatically without prompting. |
67 | 69 |
|
68 | 70 | ### Manual setup (without `vp create`/`vp migrate`) |
69 | 71 |
|
70 | 72 | For users who want to set up hooks manually, four steps are required: |
71 | 73 |
|
72 | | -1. **Add prepare script** to `package.json`: |
| 74 | +1. **Add lifecycle script** to `package.json`: |
73 | 75 | ```json |
74 | 76 | { "scripts": { "prepare": "vp config" } } |
75 | 77 | ``` |
| 78 | + Or use `postinstall` if `prepare` is not suitable for your project. |
76 | 79 | 2. **Add staged config** to `vite.config.ts`: |
77 | 80 | ```typescript |
78 | 81 | export default defineConfig({ |
@@ -105,9 +108,10 @@ Behavior: |
105 | 108 | 6. Exits 0 and skips hooks if `VITE_GIT_HOOKS=0` or `HUSKY=0` environment variable is set (backwards compatible) |
106 | 109 | 7. Exits 0 and skips hooks if `.git` directory doesn't exist (safe during `npm install` in consumer projects) |
107 | 110 | 8. Exits 1 on real errors (git command not found, `git config` failed) |
108 | | -9. Agent update runs uniformly in all modes (`prepare`, interactive, non-interactive). New agent file creation is handled by `vp create`/`vp migrate`. |
109 | | -10. Interactive mode: prompts on first run for hooks setup |
110 | | -11. Non-interactive mode: sets up hooks by default |
| 111 | +9. Agent update runs uniformly in all modes (lifecycle script, interactive, non-interactive). New agent file creation is handled by `vp create`/`vp migrate`. |
| 112 | +10. Lifecycle script mode (`prepare`/`postinstall`): sets up hooks automatically without prompting |
| 113 | +11. Interactive mode: prompts on first run — unless the project already has `staged` config in `vite.config.ts` (which implies prior opt-in) |
| 114 | +12. Non-interactive mode: sets up hooks by default |
111 | 115 |
|
112 | 116 | ### `vp staged` |
113 | 117 |
|
@@ -275,12 +279,54 @@ Husky <9.0.0 is not supported by auto migration — `vp migrate` detects unsuppo |
275 | 279 |
|
276 | 280 | ## Relationship to Existing Commands |
277 | 281 |
|
278 | | -| Command | Purpose | When | |
279 | | -| ---------------- | -------------------------------------- | --------------------------- | |
280 | | -| `vp check` | Format + lint + type check | Manual or CI | |
281 | | -| `vp check --fix` | Auto-fix format + lint issues | Manual or pre-commit | |
282 | | -| **`vp config`** | **Reinstall hook shims + agent setup** | **npm `prepare` lifecycle** | |
283 | | -| **`vp staged`** | **Run staged linters on staged files** | **Pre-commit hook** | |
| 282 | +| Command | Purpose | When | |
| 283 | +| ---------------- | -------------------------------------- | ------------------------------------------- | |
| 284 | +| `vp check` | Format + lint + type check | Manual or CI | |
| 285 | +| `vp check --fix` | Auto-fix format + lint issues | Manual or pre-commit | |
| 286 | +| **`vp config`** | **Reinstall hook shims + agent setup** | **npm lifecycle (`prepare`/`postinstall`)** | |
| 287 | +| **`vp staged`** | **Run staged linters on staged files** | **Pre-commit hook** | |
| 288 | + |
| 289 | +## `vp config` Hooks Setup Flow |
| 290 | + |
| 291 | +``` |
| 292 | +vp config |
| 293 | +│ |
| 294 | +├─ VITE_GIT_HOOKS=0 or HUSKY=0? ──→ Skip hooks (exit 0) |
| 295 | +│ |
| 296 | +├─ Not inside a git repo? ──→ Skip hooks (exit 0) |
| 297 | +│ |
| 298 | +├─ Should prompt user? |
| 299 | +│ Prompt ONLY when ALL of these are true: |
| 300 | +│ • Interactive terminal (not CI, not piped) |
| 301 | +│ • First run (hook shims don't exist yet) |
| 302 | +│ • No --hooks-dir flag |
| 303 | +│ • Not running from lifecycle script (prepare/postinstall) |
| 304 | +│ • No staged config in vite.config.ts |
| 305 | +│ |
| 306 | +│ YES → Prompt "Set up pre-commit hooks?" |
| 307 | +│ User declines → skip hooks |
| 308 | +│ NO → Auto-install hooks |
| 309 | +│ |
| 310 | +├─ core.hooksPath already set to a custom path? |
| 311 | +│ (not .vite-hooks/_, not .husky) |
| 312 | +│ └─ YES → Skip hooks, preserve custom config |
| 313 | +│ |
| 314 | +├─ Set core.hooksPath → .vite-hooks/_ |
| 315 | +├─ Create hook shims in .vite-hooks/_/ |
| 316 | +├─ Ensure staged config in vite.config.ts |
| 317 | +└─ Ensure .vite-hooks/pre-commit contains "vp staged" |
| 318 | +``` |
| 319 | + |
| 320 | +### When does the prompt appear? |
| 321 | + |
| 322 | +| Caller | Prompts? | Why | |
| 323 | +| ------------------------------------- | -------- | ---------------------------------- | |
| 324 | +| `npm install` → prepare/postinstall | No | lifecycle script = auto-install | |
| 325 | +| Manual, project has `staged` config | No | staged config = already opted in | |
| 326 | +| Manual, no `staged` config, first run | **Yes** | No signal that project wants hooks | |
| 327 | +| Manual, already ran before | No | Hook shims exist = not first run | |
| 328 | +| CI / non-interactive | No | Non-interactive = auto-install | |
| 329 | +| `--hooks-dir` flag | No | Explicit flag = intent to install | |
284 | 330 |
|
285 | 331 | ## Comparison with Other Tools |
286 | 332 |
|
|
0 commit comments