Skip to content

Commit f4f9414

Browse files
committed
refactor(config): simplify pre-commit hook setup with ensurePreCommitHook
- Extract ensurePreCommitHook() to encapsulate staged config + hook creation, replacing 3 separate imported functions - Guard hook creation on install() success to skip when hooks are disabled or git is unavailable - Extract DEFAULT_STAGED_CONFIG constant to deduplicate the default - Remove redundant existsSync guard (createPreCommitHook handles it)
1 parent 5cc9909 commit f4f9414

2 files changed

Lines changed: 20 additions & 13 deletions

File tree

packages/cli/src/config/bin.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ 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';
13+
import { ensurePreCommitHook } from '../migration/migrator.js';
1814
import { updateExistingAgentInstructions } from '../utils/agent.js';
1915
import { renderCliDoc } from '../utils/help.js';
2016
import { defaultInteractive, promptGitHooks } from '../utils/prompts.js';
@@ -81,13 +77,10 @@ async function main() {
8177
}
8278
}
8379

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);
80+
// Only create pre-commit hook when install() succeeded (empty message).
81+
// Skip when hooks were disabled or git is unavailable.
82+
if (!message) {
83+
ensurePreCommitHook(root, hooksDir);
9184
}
9285
}
9386

packages/cli/src/migration/migrator.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1689,7 +1689,7 @@ export function setupGitHooks(
16891689
const pkgData = readJsonFile<{ 'lint-staged'?: Record<string, string | string[]> }>(
16901690
packageJsonPath,
16911691
);
1692-
const stagedConfig = pkgData?.['lint-staged'] ?? { '*': 'vp check --fix' };
1692+
const stagedConfig = pkgData?.['lint-staged'] ?? DEFAULT_STAGED_CONFIG;
16931693
const updated = rewriteScripts(JSON.stringify(stagedConfig), readRulesYaml());
16941694
const finalConfig: Record<string, string | string[]> = updated
16951695
? JSON.parse(updated)
@@ -1822,6 +1822,20 @@ const STALE_LINT_STAGED_PATTERNS = [
18221822
/^((?:[A-Z_][A-Z0-9_]*(?:=\S*)?\s+)*)lint-staged\b/,
18231823
];
18241824

1825+
const DEFAULT_STAGED_CONFIG: Record<string, string> = { '*': 'vp check --fix' };
1826+
1827+
/**
1828+
* Ensure the pre-commit hook exists with `vp staged`, and that
1829+
* vite.config.ts contains a `staged` block (using the default config
1830+
* if none is present). Called by `vp config` after hook installation.
1831+
*/
1832+
export function ensurePreCommitHook(projectPath: string, dir = '.vite-hooks'): void {
1833+
if (!hasStagedConfigInViteConfig(projectPath)) {
1834+
mergeStagedConfigToViteConfig(projectPath, DEFAULT_STAGED_CONFIG, true);
1835+
}
1836+
createPreCommitHook(projectPath, dir);
1837+
}
1838+
18251839
export function createPreCommitHook(projectPath: string, dir = '.vite-hooks'): void {
18261840
const huskyDir = path.join(projectPath, dir);
18271841
fs.mkdirSync(huskyDir, { recursive: true });

0 commit comments

Comments
 (0)