Skip to content
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

feat: add git init prompt #325

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/beige-dancers-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'sv': patch
---

add prompt to setup git repo if already not initialized
26 changes: 22 additions & 4 deletions packages/cli/commands/create.ts
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@ import {
installDependencies,
packageManagerPrompt
} from '../utils/package-manager.ts';
import { gitInitPrompt, initGitRepo } from '../utils/git.ts';

const langs = ['ts', 'jsdoc'] as const;
const langMap: Record<string, LanguageType | undefined> = {
@@ -40,7 +41,8 @@ const OptionsSchema = v.strictObject({
),
addOns: v.boolean(),
install: v.boolean(),
template: v.optional(v.picklist(templateChoices))
template: v.optional(v.picklist(templateChoices)),
git: v.boolean()
});
type Options = v.InferOutput<typeof OptionsSchema>;
type ProjectPath = v.InferOutput<typeof ProjectPathSchema>;
@@ -53,12 +55,16 @@ export const create = new Command('create')
.option('--no-types')
.option('--no-add-ons', 'skips interactive add-on installer')
.option('--no-install', 'skip installing dependencies')
.option('--no-git', 'skip initializing a git repository')
.configureHelp(common.helpConfig)
.action((projectPath, opts) => {
const cwd = v.parse(ProjectPathSchema, projectPath);
const options = v.parse(OptionsSchema, opts);
common.runCommand(async () => {
const { directory, addOnNextSteps, packageManager } = await createProject(cwd, options);
const { directory, addOnNextSteps, packageManager, gitInit } = await createProject(
cwd,
options
);
const highlight = (str: string) => pc.bold(pc.cyan(str));

let i = 1;
@@ -75,10 +81,13 @@ export const create = new Command('create')
initialSteps.push(`${i++}: ${highlight(`${pm} install`)}`);
}

if (gitInit) {
initialSteps.push(`${i++}: ${highlight('git commit -m "Initial commit"')}`);
}

const pmRun = pm === 'npm' ? 'npm run dev --' : `${pm} dev`;
const steps = [
...initialSteps,
`${i++}: ${highlight('git init && git add -A && git commit -m "Initial commit"')} (optional)`,
`${i++}: ${highlight(`${pmRun} --open`)}`,
'',
`To close the dev server, hit ${highlight('Ctrl-C')}`,
@@ -184,5 +193,14 @@ async function createProject(cwd: ProjectPath, options: Options) {
await installDeps();
}

return { directory: projectPath, addOnNextSteps, packageManager };
let gitInit = false;
if (options.git) {
const shouldInit = await gitInitPrompt(projectPath);
if (shouldInit) {
initGitRepo(projectPath);
gitInit = true;
}
}

return { directory: projectPath, addOnNextSteps, packageManager, gitInit };
}
48 changes: 48 additions & 0 deletions packages/cli/utils/git.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { execSync } from 'node:child_process';
import * as p from '@sveltejs/clack-prompts';

function hasGitInstalled() {
try {
execSync('git --version', { stdio: 'ignore' });
return true;
} catch {
return false;
}
}

function isGitRepo(cwd: string) {
try {
return (
execSync('git rev-parse --is-inside-work-tree', { cwd, stdio: 'pipe' }).toString().trim() ===
'true'
);
} catch {
return false;
}
}

export async function initGitRepo(cwd: string) {
execSync('git init', { cwd, stdio: 'ignore' });
execSync('git add -A', { cwd, stdio: 'ignore' });
}

export async function gitInitPrompt(cwd: string): Promise<boolean> {
const hasGit = hasGitInstalled();
if (!hasGit) return false;
const alreadyGitRepo = isGitRepo(cwd);
if (alreadyGitRepo) return false;

const shouldInit = await p.confirm({
message: 'Do you want to initialize a git repository?',
active: 'yes',
inactive: 'no',
initialValue: true
});

if (p.isCancel(shouldInit)) {
p.cancel('Operation cancelled.');
process.exit(1);
}

return shouldInit;
}