-
Notifications
You must be signed in to change notification settings - Fork 182
feat(cli): Initialize a git repository and create an initial commit on scaffold #1484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,13 +29,15 @@ import { | |
| writeAgentInstructions, | ||
| } from '../utils/agent.ts'; | ||
| import { detectExistingEditors, selectEditors, writeEditorConfigs } from '../utils/editor.ts'; | ||
| import { createInitialCommit, initGitRepository } from '../utils/git.ts'; | ||
| import { renderCliDoc } from '../utils/help.ts'; | ||
| import { displayRelative } from '../utils/path.ts'; | ||
| import { | ||
| type CommandRunSummary, | ||
| defaultInteractive, | ||
| downloadPackageManager, | ||
| promptGitHooks, | ||
| promptGitInit, | ||
| runViteFmt, | ||
| runViteInstall, | ||
| selectPackageManager, | ||
|
|
@@ -106,6 +108,8 @@ const helpMessage = renderCliDoc({ | |
| label: '--editor NAME', | ||
| description: 'Write editor config files for the specified editor.', | ||
| }, | ||
| { label: '--git', description: 'Initialize a git repository with an initial commit' }, | ||
| { label: '--no-git', description: 'Skip git repository initialization' }, | ||
| { | ||
| label: '--hooks', | ||
| description: 'Set up pre-commit hooks (default in non-interactive mode)', | ||
|
|
@@ -235,11 +239,12 @@ function parseArgs() { | |
| verbose?: boolean; | ||
| agent?: string | string[] | false; | ||
| editor?: string; | ||
| git?: boolean; | ||
| hooks?: boolean; | ||
| 'package-manager'?: string; | ||
| }>(viteArgs, { | ||
| alias: { h: 'help' }, | ||
| boolean: ['help', 'list', 'all', 'interactive', 'hooks', 'verbose'], | ||
| boolean: ['help', 'list', 'all', 'interactive', 'hooks', 'verbose', 'git'], | ||
| string: ['directory', 'agent', 'editor', 'package-manager'], | ||
| default: { interactive: defaultInteractive() }, | ||
| }); | ||
|
|
@@ -256,6 +261,7 @@ function parseArgs() { | |
| verbose: parsed.verbose || false, | ||
| agent: parsed.agent, | ||
| editor: parsed.editor, | ||
| git: parsed.git, | ||
| hooks: parsed.hooks, | ||
| packageManager: parsed['package-manager'], | ||
| } as Options, | ||
|
|
@@ -747,6 +753,7 @@ Use \`vp create --list\` to list all available templates, or run \`vp create --h | |
| onCancel: () => cancelAndExit(), | ||
| })); | ||
|
|
||
| const shouldSetupGit = await promptGitInit(options); | ||
| if (!isMonorepo) { | ||
| shouldSetupHooks = await promptGitHooks(options); | ||
| } | ||
|
|
@@ -902,6 +909,10 @@ Use \`vp create --list\` to list all available templates, or run \`vp create --h | |
| workspaceInfo.rootDir = fullPath; | ||
| updateCreateProgress('Integrating monorepo'); | ||
| rewriteMonorepo(workspaceInfo, undefined, compactOutput); | ||
| if (shouldSetupGit) { | ||
| updateCreateProgress('Initializing git repository'); | ||
| await initGitRepository(fullPath); | ||
| } | ||
| if (bundled?.monorepo) { | ||
| // Wire `create.defaultTemplate: '<scope>'` into the new workspace's | ||
| // vite.config.ts so a bare `vp create` from inside it opens the | ||
|
|
@@ -922,6 +933,10 @@ Use \`vp create --list\` to list all available templates, or run \`vp create --h | |
| }); | ||
| updateCreateProgress('Formatting code'); | ||
| await runViteFmt(fullPath, options.interactive, undefined, { silent: compactOutput }); | ||
| if (shouldSetupGit) { | ||
| updateCreateProgress('Creating initial commit'); | ||
| await createInitialCommit(fullPath); | ||
| } | ||
| clearCreateProgress(); | ||
| showCreateSummary({ | ||
| description: describeScaffold(selectedTemplateName, selectedTemplateArgs), | ||
|
|
@@ -1137,6 +1152,11 @@ Use \`vp create --list\` to list all available templates, or run \`vp create --h | |
| await runViteFmt(workspaceInfo.rootDir, options.interactive, [projectDir], { | ||
| silent: compactOutput, | ||
| }); | ||
| if (shouldSetupGit) { | ||
| updateCreateProgress('Creating initial commit'); | ||
| await initGitRepository(workspaceInfo.rootDir); | ||
| await createInitialCommit(workspaceInfo.rootDir); | ||
|
Comment on lines
+1157
to
+1158
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In an existing monorepo, opting into Useful? React with 👍 / 👎.
Comment on lines
+1157
to
+1158
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In an existing monorepo, opting into Useful? React with 👍 / 👎. |
||
| } | ||
| } else { | ||
| if (shouldMigrateLintFmtTools) { | ||
| await installAndMigrate(fullPath); | ||
|
|
@@ -1148,6 +1168,10 @@ Use \`vp create --list\` to list all available templates, or run \`vp create --h | |
| addFrameworkShim(fullPath, framework); | ||
| } | ||
| } | ||
| if (shouldSetupGit) { | ||
| updateCreateProgress('Initializing git repository'); | ||
| await initGitRepository(fullPath); | ||
| } | ||
|
Comment on lines
+1171
to
+1174
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For standalone scaffolds, Useful? React with 👍 / 👎. |
||
| if (shouldSetupHooks) { | ||
| installGitHooks(fullPath, compactOutput); | ||
| } | ||
|
|
@@ -1159,6 +1183,10 @@ Use \`vp create --list\` to list all available templates, or run \`vp create --h | |
| }); | ||
| updateCreateProgress('Formatting code'); | ||
| await runViteFmt(fullPath, options.interactive, undefined, { silent: compactOutput }); | ||
| if (shouldSetupGit) { | ||
| updateCreateProgress('Creating initial commit'); | ||
| await createInitialCommit(fullPath); | ||
|
Comment on lines
+1186
to
+1188
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with 👍 / 👎.
Comment on lines
1185
to
+1188
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The initial commit is created even when the preceding install or formatter step failed; Useful? React with 👍 / 👎. |
||
| } | ||
| } | ||
|
|
||
| clearCreateProgress(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { runCommandSilently } from './command.ts'; | ||
|
|
||
| export async function initGitRepository(cwd: string): Promise<boolean> { | ||
| const result = await runCommandSilently({ | ||
| command: 'git', | ||
| args: ['init'], | ||
| cwd, | ||
| envs: process.env, | ||
| }); | ||
| return result.exitCode === 0; | ||
| } | ||
|
|
||
| export async function createInitialCommit(cwd: string): Promise<boolean> { | ||
| await runCommandSilently({ | ||
| command: 'git', | ||
| args: ['add', '-A'], | ||
| cwd, | ||
| envs: process.env, | ||
| }); | ||
| const result = await runCommandSilently({ | ||
| command: 'git', | ||
| args: ['commit', '-m', 'Initial commit from Vite+'], | ||
| cwd, | ||
| envs: process.env, | ||
| }); | ||
| return result.exitCode === 0; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
promptGitInit(options)returnsfalseforvp create vite:monorepo --no-git, but the monorepo-template path still uses its separateshouldInitGitdefault and callsgit initlater. In the default compact mode this happens without another prompt, so the newly documented--no-gitoption does not actually skip repository initialization for monorepo scaffolds.Useful? React with 👍 / 👎.