-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathbin.ts
More file actions
93 lines (81 loc) · 3 KB
/
bin.ts
File metadata and controls
93 lines (81 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Unified `vp config` command — hooks setup + agent instruction updates.
//
// Hooks: interactive mode prompts on first run; non-interactive installs by default.
// Agent instructions: silently updates existing files with Vite+ markers.
// Never creates new agent files. Same behavior for prepare and manual runs.
import { existsSync } from 'node:fs';
import { join } from 'node:path';
import mri from 'mri';
import { vitePlusHeader } from '../../binding/index.js';
import { ensurePreCommitHook } from '../migration/migrator.js';
import { updateExistingAgentInstructions } from '../utils/agent.js';
import { renderCliDoc } from '../utils/help.js';
import { defaultInteractive, promptGitHooks } from '../utils/prompts.js';
import { log } from '../utils/terminal.js';
import { install } from './hooks.js';
async function main() {
const args = mri(process.argv.slice(3), {
boolean: ['help', 'hooks-only'],
string: ['hooks-dir'],
alias: { h: 'help' },
});
if (args.help) {
const helpMessage = renderCliDoc({
usage: 'vp config [OPTIONS]',
summary: 'Configure Vite+ for the current project (hooks + agent integration).',
documentationUrl: 'https://viteplus.dev/guide/commit-hooks',
sections: [
{
title: 'Options',
rows: [
{
label: '--hooks-dir <path>',
description: 'Custom hooks directory (default: .vite-hooks)',
},
{ label: '-h, --help', description: 'Show this help message' },
],
},
{
title: 'Environment',
rows: [{ label: 'VITE_GIT_HOOKS=0', description: 'Skip hook installation' }],
},
],
});
log(vitePlusHeader() + '\n');
log(helpMessage);
return;
}
const dir = args['hooks-dir'] as string | undefined;
const hooksOnly = args['hooks-only'] as boolean;
const interactive = defaultInteractive();
const isPrepareScript = process.env.npm_lifecycle_event === 'prepare';
const root = process.cwd();
// --- Step 1: Hooks setup ---
const hooksDir = dir ?? '.vite-hooks';
const isFirstHooksRun = !existsSync(join(root, hooksDir, '_', 'pre-commit'));
let shouldSetupHooks = true;
if (interactive && isFirstHooksRun && !dir && !isPrepareScript) {
// --hooks-dir implies agreement; only prompt when using default dir on first run
// prepare script implies the project opted into hooks — install automatically
shouldSetupHooks = await promptGitHooks({ interactive });
}
if (shouldSetupHooks) {
const { message, isError } = install(dir);
if (message) {
log(message);
if (isError) {
process.exit(1);
}
}
// Only create pre-commit hook when install() succeeded (empty message).
// Skip when hooks were disabled or git is unavailable.
if (!message) {
ensurePreCommitHook(root, hooksDir);
}
}
// --- Step 2: Update agent instructions if Vite+ header exists and is outdated ---
if (!hooksOnly) {
updateExistingAgentInstructions(root);
}
}
void main();