-
Notifications
You must be signed in to change notification settings - Fork 184
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
8a13436
6232ecf
bd2a84e
9ef2832
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 |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| { | ||
| "commands": [ | ||
| "node $SNAP_CASES_DIR/.shared/mock-npm-registry.mjs -- vp create @your-org:workspace --no-interactive --directory my-mono # bundled monorepo: extract tarball, scaffold, inject create.defaultTemplate", | ||
| "node $SNAP_CASES_DIR/.shared/mock-npm-registry.mjs -- vp create @your-org:workspace --no-interactive --directory my-mono --git # bundled monorepo: extract tarball, scaffold, inject create.defaultTemplate", | ||
| "cat my-mono/vite.config.ts # create.defaultTemplate auto-set to @your-org", | ||
| "cat my-mono/pnpm-workspace.yaml # workspace markers preserved", | ||
| "test -d my-mono/.git && echo 'Git initialized' || echo 'No git' # git-init prompt covers bundled monorepo path", | ||
| "test -d my-mono/.git && echo 'Git initialized' # git-init prompt covers bundled monorepo path", | ||
| "cat my-mono/.gitignore # node_modules excluded even though tarball shipped no .gitignore" | ||
| ] | ||
| } |
| 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); | ||
| } | ||
|
|
@@ -827,7 +834,7 @@ Use \`vp create --list\` to list all available templates, or run \`vp create --h | |
| // #region Handle monorepo template | ||
| if (templateInfo.command === BuiltinTemplate.monorepo || isBundledMonorepo) { | ||
| // Ask up-front so the prompt isn't buried under scaffold output. | ||
| let shouldInitGit = true; | ||
| let shouldInitGit = shouldSetupGit; | ||
| if (options.interactive && !compactOutput) { | ||
| pauseCreateProgress(); | ||
| const selected = await prompts.confirm({ | ||
|
|
@@ -841,7 +848,7 @@ Use \`vp create --list\` to list all available templates, or run \`vp create --h | |
| } else { | ||
| shouldInitGit = selected; | ||
| } | ||
| } else if (!compactOutput) { | ||
| } else if (shouldInitGit && !compactOutput) { | ||
| prompts.log.info('Initializing git repository (default: yes)'); | ||
| } | ||
|
|
||
|
|
@@ -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,13 @@ 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'); | ||
| const committed = await createInitialCommit(fullPath); | ||
| if (!committed) { | ||
| prompts.log.warn('Initial commit failed. Check your git user.name/user.email config'); | ||
| } | ||
| } | ||
| clearCreateProgress(); | ||
| showCreateSummary({ | ||
| description: describeScaffold(selectedTemplateName, selectedTemplateArgs), | ||
|
|
@@ -1137,6 +1155,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
+1160
to
+1161
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 👍 / 👎.
Author
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. I think that no action is required about this either, for the reasons below.
fengmk2 marked this conversation as resolved.
|
||
| } | ||
| } else { | ||
| if (shouldMigrateLintFmtTools) { | ||
| await installAndMigrate(fullPath); | ||
|
|
@@ -1148,6 +1171,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); | ||
| } | ||
|
fengmk2 marked this conversation as resolved.
|
||
| if (shouldSetupHooks) { | ||
| installGitHooks(fullPath, compactOutput); | ||
| } | ||
|
|
@@ -1159,6 +1186,13 @@ 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'); | ||
| const committed = await createInitialCommit(fullPath); | ||
| if (!committed) { | ||
| prompts.log.warn('Initial commit failed. Check your git user.name/user.email config'); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| 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; | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.