Skip to content

Commit 10ea5fa

Browse files
committed
fix(config): auto-install hooks when called from postinstall lifecycle
Projects like npmx.dev call `vp config` from `postinstall` instead of `prepare`. Extend the lifecycle detection to also recognize `postinstall` so hooks are auto-installed without prompting in both cases. Closes #1154
1 parent 9517aef commit 10ea5fa

6 files changed

Lines changed: 39 additions & 7 deletions

File tree

packages/cli/snap-tests-global/command-config-auto-hooks/snap.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { defineConfig } from 'vite-plus';
1111

1212
export default defineConfig({
1313
staged: {
14-
"*": "vp check --fix"
14+
'*': 'vp check --fix',
1515
},
16-
1716
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "command-config-postinstall-auto-hooks"
3+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
> git init
2+
> vp config # should install hooks automatically without prompting
3+
> git config --local core.hooksPath # should be .vite-hooks/_
4+
.vite-hooks/_
5+
6+
> cat .vite-hooks/pre-commit # should have vp staged
7+
vp staged
8+
9+
> cat vite.config.ts # should have staged config
10+
import { defineConfig } from 'vite-plus';
11+
12+
export default defineConfig({
13+
staged: {
14+
"*": "vp check --fix"
15+
},
16+
17+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"env": {
3+
"npm_lifecycle_event": "postinstall"
4+
},
5+
"commands": [
6+
{ "command": "git init", "ignoreOutput": true },
7+
"vp config # should install hooks automatically without prompting",
8+
"git config --local core.hooksPath # should be .vite-hooks/_",
9+
"cat .vite-hooks/pre-commit # should have vp staged",
10+
"cat vite.config.ts # should have staged config"
11+
]
12+
}

packages/cli/src/config/bin.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ async function main() {
5454
const dir = args['hooks-dir'] as string | undefined;
5555
const hooksOnly = args['hooks-only'] as boolean;
5656
const interactive = defaultInteractive();
57-
const isPrepareScript = process.env.npm_lifecycle_event === 'prepare';
57+
const lifecycleEvent = process.env.npm_lifecycle_event;
58+
const isLifecycleScript = lifecycleEvent === 'prepare' || lifecycleEvent === 'postinstall';
5859
const root = process.cwd();
5960

6061
// --- Step 1: Hooks setup ---
@@ -66,11 +67,11 @@ async function main() {
6667
interactive &&
6768
isFirstHooksRun &&
6869
!dir &&
69-
!isPrepareScript &&
70+
!isLifecycleScript &&
7071
!hasStagedConfigInViteConfig(root)
7172
) {
7273
// --hooks-dir implies agreement; only prompt when using default dir on first run
73-
// prepare script implies the project opted into hooks — install automatically
74+
// lifecycle script (prepare/postinstall) implies the project opted into hooks — install automatically
7475
// existing staged config in vite.config.ts implies the project already opted in
7576
shouldSetupHooks = await promptGitHooks({ interactive });
7677
}

rfcs/config-and-staged-commands.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ vp config
296296
│ • Interactive terminal (not CI, not piped)
297297
│ • First run (hook shims don't exist yet)
298298
│ • No --hooks-dir flag
299-
│ • Not running from prepare lifecycle
299+
│ • Not running from lifecycle script (prepare/postinstall)
300300
│ • No staged config in vite.config.ts ← NEW (#1154)
301301
302302
│ YES → Prompt "Set up pre-commit hooks?"
@@ -317,7 +317,7 @@ vp config
317317

318318
| Caller | Prompts? | Why |
319319
| ------------------------------------- | -------- | ------------------------------------------------ |
320-
| `npm install` → prepare | No | prepare lifecycle = auto-install |
320+
| `npm install` → prepare/postinstall | No | lifecycle script = auto-install |
321321
| Manual, project has `staged` config | No | staged config = already opted in **(#1154 fix)** |
322322
| Manual, no `staged` config, first run | **Yes** | No signal that project wants hooks |
323323
| Manual, already ran before | No | Hook shims exist = not first run |

0 commit comments

Comments
 (0)