Skip to content

Commit 9a3a310

Browse files
committed
refactor(hooks): extract shared promptGitHooks and clean up
- Extract duplicate `promptGitHooks()` from create/bin.ts and migration/bin.ts into utils/prompts.ts with a minimal interface - Remove trivial `assert!(true)` tests from prepare.rs and lint_staged.rs - Use `readJsonFile()` instead of raw `JSON.parse(fs.readFileSync())` in setupGitHooks for consistency
1 parent 023a24c commit 9a3a310

6 files changed

Lines changed: 35 additions & 65 deletions

File tree

crates/vite_global_cli/src/commands/lint_staged.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,3 @@ use crate::error::Error;
1010
pub async fn execute(cwd: AbsolutePathBuf, args: &[String]) -> Result<ExitStatus, Error> {
1111
super::delegate::execute(cwd, "lint-staged", args).await
1212
}
13-
14-
#[cfg(test)]
15-
mod tests {
16-
#[test]
17-
fn test_lint_staged_command_module_exists() {
18-
// Basic test to ensure the module compiles
19-
assert!(true);
20-
}
21-
}

crates/vite_global_cli/src/commands/prepare.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,3 @@ use crate::error::Error;
1010
pub async fn execute(cwd: AbsolutePathBuf, args: &[String]) -> Result<ExitStatus, Error> {
1111
super::delegate::execute(cwd, "prepare", args).await
1212
}
13-
14-
#[cfg(test)]
15-
mod tests {
16-
#[test]
17-
fn test_prepare_command_module_exists() {
18-
// Basic test to ensure the module compiles
19-
assert!(true);
20-
}
21-
}

packages/cli/src/create/bin.ts

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { displayRelative } from '../utils/path.js';
2121
import {
2222
defaultInteractive,
2323
downloadPackageManager,
24+
promptGitHooks,
2425
runViteInstall,
2526
selectPackageManager,
2627
} from '../utils/prompts.js';
@@ -70,7 +71,10 @@ const helpMessage = renderCliDoc({
7071
label: '--agent NAME',
7172
description: 'Create an agent instructions file for the specified agent.',
7273
},
73-
{ label: '--hooks', description: 'Set up pre-commit hooks (default in non-interactive mode)' },
74+
{
75+
label: '--hooks',
76+
description: 'Set up pre-commit hooks (default in non-interactive mode)',
77+
},
7478
{ label: '--no-hooks', description: 'Skip pre-commit hooks setup' },
7579
{ label: '--no-interactive', description: 'Run in non-interactive mode' },
7680
{ label: '--list', description: 'List all available templates' },
@@ -694,27 +698,6 @@ async function showAvailableTemplates() {
694698
log(listTemplatesMessage);
695699
}
696700

697-
async function promptGitHooks(options: Options): Promise<boolean> {
698-
if (options.hooks === false) {
699-
return false;
700-
}
701-
if (options.hooks === true) {
702-
return true;
703-
}
704-
if (options.interactive) {
705-
const selected = await prompts.confirm({
706-
message: 'Set up pre-commit hooks to run format, lint, and type checks with auto-fix?',
707-
initialValue: true,
708-
});
709-
if (prompts.isCancel(selected)) {
710-
cancelAndExit();
711-
return false;
712-
}
713-
return selected;
714-
}
715-
return true; // non-interactive default
716-
}
717-
718701
main().catch((err) => {
719702
prompts.log.error(err.message);
720703
console.error(err);

packages/cli/src/migration/bin.ts

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
cancelAndExit,
1515
defaultInteractive,
1616
downloadPackageManager,
17+
promptGitHooks,
1718
runViteInstall,
1819
selectPackageManager,
1920
upgradeYarn,
@@ -246,27 +247,6 @@ async function main() {
246247
prompts.outro(green('✔ Migration completed!'));
247248
}
248249

249-
async function promptGitHooks(options: MigrationOptions): Promise<boolean> {
250-
if (options.hooks === false) {
251-
return false;
252-
}
253-
if (options.hooks === true) {
254-
return true;
255-
}
256-
if (options.interactive) {
257-
const selected = await prompts.confirm({
258-
message: 'Set up pre-commit hooks to run format, lint, and type checks with auto-fix?',
259-
initialValue: true,
260-
});
261-
if (prompts.isCancel(selected)) {
262-
cancelAndExit();
263-
return false;
264-
}
265-
return selected;
266-
}
267-
return true; // non-interactive default
268-
}
269-
270250
main().catch((err) => {
271251
prompts.log.error(err.message);
272252
console.error(err);

packages/cli/src/migration/migrator.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
VITE_PLUS_OVERRIDE_PACKAGES,
2020
VITE_PLUS_VERSION,
2121
} from '../utils/constants.js';
22-
import { editJsonFile, isJsonFile } from '../utils/json.js';
22+
import { editJsonFile, isJsonFile, readJsonFile } from '../utils/json.js';
2323
import { detectPackageMetadata } from '../utils/package.js';
2424
import { displayRelative, rulesDir } from '../utils/path.js';
2525
import { editYamlFile, scalarString, type YamlDocument } from '../utils/yaml.js';
@@ -662,11 +662,12 @@ export function setupGitHooks(projectPath: string): void {
662662
return;
663663
}
664664

665-
const pkgContent = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
666-
667665
// Check for other hook tools → warn and skip
666+
const pkgContent = readJsonFile(packageJsonPath);
668667
for (const tool of OTHER_HOOK_TOOLS) {
669-
if (pkgContent.devDependencies?.[tool] || pkgContent.dependencies?.[tool] || pkgContent[tool]) {
668+
const deps = pkgContent.devDependencies as Record<string, string> | undefined;
669+
const prodDeps = pkgContent.dependencies as Record<string, string> | undefined;
670+
if (deps?.[tool] || prodDeps?.[tool] || pkgContent[tool]) {
670671
prompts.log.warn(
671672
`⚠ Detected ${tool} — skipping git hooks setup. Please configure git hooks manually.`,
672673
);

packages/cli/src/utils/prompts.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,30 @@ export async function upgradeYarn(cwd: string, interactive?: boolean) {
9191
}
9292
}
9393

94+
export async function promptGitHooks(options: {
95+
hooks?: boolean;
96+
interactive: boolean;
97+
}): Promise<boolean> {
98+
if (options.hooks === false) {
99+
return false;
100+
}
101+
if (options.hooks === true) {
102+
return true;
103+
}
104+
if (options.interactive) {
105+
const selected = await prompts.confirm({
106+
message: 'Set up pre-commit hooks to run format, lint, and type checks with auto-fix?',
107+
initialValue: true,
108+
});
109+
if (prompts.isCancel(selected)) {
110+
cancelAndExit();
111+
return false;
112+
}
113+
return selected;
114+
}
115+
return true; // non-interactive default
116+
}
117+
94118
export function defaultInteractive() {
95119
// If CI environment, use non-interactive mode by default
96120
return !process.env.CI && process.stdin.isTTY;

0 commit comments

Comments
 (0)