Skip to content

Commit 410cf58

Browse files
committed
fix(config): auto-install hooks without prompting in prepare script
When `vp config` runs from a package.json prepare script (npm_lifecycle_event=prepare) in an interactive terminal, it was incorrectly prompting the user for hooks setup confirmation. The prepare script implies the project opted into hooks, so they should be installed automatically.
1 parent 6644144 commit 410cf58

4 files changed

Lines changed: 20 additions & 1 deletion

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "command-config-prepare-auto-hooks"
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
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/_
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"env": {
3+
"npm_lifecycle_event": "prepare"
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+
]
10+
}

packages/cli/src/config/bin.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,17 @@ async function main() {
5353
const dir = args['hooks-dir'] as string | undefined;
5454
const hooksOnly = args['hooks-only'] as boolean;
5555
const interactive = defaultInteractive();
56+
const isPrepareScript = process.env.npm_lifecycle_event === 'prepare';
5657
const root = process.cwd();
5758

5859
// --- Step 1: Hooks setup ---
5960
const hooksDir = dir ?? '.vite-hooks';
6061
const isFirstHooksRun = !existsSync(join(root, hooksDir, '_', 'pre-commit'));
6162

6263
let shouldSetupHooks = true;
63-
if (interactive && isFirstHooksRun && !dir) {
64+
if (interactive && isFirstHooksRun && !dir && !isPrepareScript) {
6465
// --hooks-dir implies agreement; only prompt when using default dir on first run
66+
// prepare script implies the project opted into hooks — install automatically
6567
shouldSetupHooks = await promptGitHooks({ interactive });
6668
}
6769

0 commit comments

Comments
 (0)