forked from withastro/astro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.ts
62 lines (58 loc) · 1.57 KB
/
git.ts
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
62
import fs from 'node:fs';
import path from 'node:path';
import type { Context } from './context';
import { color } from '@astrojs/cli-kit';
import { execa } from 'execa';
import { error, info, spinner, title } from '../messages.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) => {
// eslint-disable-next-line no-console
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 execa('git', ['init'], { cwd, stdio: 'ignore' });
await execa('git', ['add', '-A'], { cwd, stdio: 'ignore' });
await execa(
'git',
[
'commit',
'-m',
'Initial commit from Astro',
'--author="houston[bot] <[email protected]>"',
],
{ cwd, stdio: 'ignore' }
);
} catch (e) {}
}