Skip to content

Commit

Permalink
feat: add git init prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
BlankParticle committed Nov 22, 2024
1 parent e725f28 commit a438004
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
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
Expand Up @@ -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> = {
Expand All @@ -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>;
Expand All @@ -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;
Expand All @@ -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')}`,
Expand Down Expand Up @@ -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;
}

0 comments on commit a438004

Please sign in to comment.