Skip to content

Commit 5cc9909

Browse files
committed
feat(config): auto-create pre-commit hook and staged config
When `vp config` installs hooks and `.vite-hooks/pre-commit` does not exist, automatically create it with `vp staged` and ensure the default staged config (`{ '*': 'vp check --fix' }`) exists in vite.config.ts. This mirrors the behavior of `vp create` and ensures the pre-commit hook is functional out of the box.
1 parent 410cf58 commit 5cc9909

4 files changed

Lines changed: 32 additions & 3 deletions

File tree

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,16 @@
22
> vp config # should install hooks automatically without prompting
33
> git config --local core.hooksPath # should be .vite-hooks/_
44
.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+
});

packages/cli/snap-tests-global/command-config-prepare-auto-hooks/steps.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"commands": [
66
{ "command": "git init", "ignoreOutput": true },
77
"vp config # should install hooks automatically without prompting",
8-
"git config --local core.hooksPath # should be .vite-hooks/_"
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"
911
]
1012
}

packages/cli/src/config/bin.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ import { join } from 'node:path';
1010
import mri from 'mri';
1111

1212
import { vitePlusHeader } from '../../binding/index.js';
13+
import {
14+
createPreCommitHook,
15+
hasStagedConfigInViteConfig,
16+
mergeStagedConfigToViteConfig,
17+
} from '../migration/migrator.js';
1318
import { updateExistingAgentInstructions } from '../utils/agent.js';
1419
import { renderCliDoc } from '../utils/help.js';
1520
import { defaultInteractive, promptGitHooks } from '../utils/prompts.js';
@@ -75,6 +80,15 @@ async function main() {
7580
process.exit(1);
7681
}
7782
}
83+
84+
// Create pre-commit hook if missing, and ensure staged config exists
85+
const preCommitPath = join(root, hooksDir, 'pre-commit');
86+
if (!existsSync(preCommitPath)) {
87+
if (!hasStagedConfigInViteConfig(root)) {
88+
mergeStagedConfigToViteConfig(root, { '*': 'vp check --fix' }, true);
89+
}
90+
createPreCommitHook(root, hooksDir);
91+
}
7892
}
7993

8094
// --- Step 2: Update agent instructions if Vite+ header exists and is outdated ---

packages/cli/src/migration/migrator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,7 +1394,7 @@ function mergeAndRemoveJsonConfig(
13941394
* Merge a staged config object into vite.config.ts as `staged: { ... }`.
13951395
* Writes the config to a temp JSON file, calls mergeJsonConfig NAPI, then cleans up.
13961396
*/
1397-
function mergeStagedConfigToViteConfig(
1397+
export function mergeStagedConfigToViteConfig(
13981398
projectPath: string,
13991399
stagedConfig: Record<string, string | string[]>,
14001400
silent = false,
@@ -1440,7 +1440,7 @@ function mergeStagedConfigToViteConfig(
14401440
/**
14411441
* Check if vite.config.ts already has a `staged` config key.
14421442
*/
1443-
function hasStagedConfigInViteConfig(projectPath: string): boolean {
1443+
export function hasStagedConfigInViteConfig(projectPath: string): boolean {
14441444
const configs = detectConfigs(projectPath);
14451445
if (!configs.viteConfig) {
14461446
return false;

0 commit comments

Comments
 (0)