summaryrefslogtreecommitdiff
path: root/packages/create-astro/src/actions/git.ts
blob: c2a59b0b32f14b9a0fabbab6dea367c4c5ba7cf7 (plain) (blame)
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
import fs from 'node:fs';
import path from 'node:path';
import type { Context } from './context';

import { color } from '@astrojs/cli-kit';
import { error, info, spinner, title } from '../messages.js';
import { shell } from '../shell.js';

export async function git(ctx: Pick<Context, 'cwd' | 'git' | 'yes' | 'prompt' | 'dryRun'>) {
	if (fs.existsSync(path.join(ctx.cwd, '.git'))) {
		await info('Nice!', `Git has already been initialized`);
		return;
	}
	let _git = ctx.git ?? ctx.yes;
	if (_git === undefined) {
		({ git: _git } = await ctx.prompt({
			name: 'git',
			type: 'confirm',
			label: title('git'),
			message: `Initialize a new git repository?`,
			hint: 'optional',
			initial: true,
		}));
	}

	if (ctx.dryRun) {
		await info('--dry-run', `Skipping Git initialization`);
	} else if (_git) {
		await spinner({
			start: 'Git initializing...',
			end: 'Git initialized',
			while: () =>
				init({ cwd: ctx.cwd }).catch((e) => {
					error('error', e);
					process.exit(1);
				}),
		});
	} else {
		await info(
			ctx.yes === false ? 'git [skip]' : 'Sounds good!',
			`You can always run ${color.reset('git init')}${color.dim(' manually.')}`
		);
	}
}

async function init({ cwd }: { cwd: string }) {
	try {
		await shell('git', ['init'], { cwd, stdio: 'ignore' });
		await shell('git', ['add', '-A'], { cwd, stdio: 'ignore' });
		await shell(
			'git',
			[
				'commit',
				'-m',
				'Initial commit from Astro',
				'--author="houston[bot] <astrobot-houston@users.noreply.github.com>"',
			],
			{ cwd, stdio: 'ignore' }
		);
	} catch (e) {}
}