astro/packages/create-astro/src/actions/git.ts
Nate Moore 066b4b4efc
Improve create-astro error handling (#6266)
* fix(create-astro): improve error handling for tasks that use spinner display

* refactor: timeout after 60s

* chore: remove unused file
2023-02-16 14:28:15 -06:00

61 lines
1.6 KiB
TypeScript

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 { info, spinner, error, 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] <astrobot-houston@users.noreply.github.com>"',
],
{ cwd, stdio: 'ignore' }
);
} catch (e) {}
}