diff --git a/.changeset/plenty-taxis-hunt.md b/.changeset/plenty-taxis-hunt.md new file mode 100644 index 000000000000..7747a2a76fa9 --- /dev/null +++ b/.changeset/plenty-taxis-hunt.md @@ -0,0 +1,5 @@ +--- +'create-astro': minor +--- + +Adds `--yes` and `dry-run` flags to project-name and the `yes` flag to template. diff --git a/packages/create-astro/src/actions/project-name.ts b/packages/create-astro/src/actions/project-name.ts index 533240efd42d..bb4d5feb6618 100644 --- a/packages/create-astro/src/actions/project-name.ts +++ b/packages/create-astro/src/actions/project-name.ts @@ -6,7 +6,7 @@ import { info, log, title } from '../messages.js'; import { isEmpty, toValidName } from './shared.js'; -export async function projectName(ctx: Pick) { +export async function projectName(ctx: Pick) { await checkCwd(ctx.cwd); if (!ctx.cwd || !isEmpty(ctx.cwd)) { @@ -14,6 +14,13 @@ export async function projectName(ctx: Pick) { - if (!ctx.template) { +export async function template(ctx: Pick) { + if (ctx.yes) { + ctx.template = 'basics'; + await info('tmpl', `Using ${color.reset(ctx.template)}${color.dim(' as project template')}`); + } else if (!ctx.template) { const { template: tmpl } = await ctx.prompt({ name: 'template', type: 'select', diff --git a/packages/create-astro/test/project-name.test.js b/packages/create-astro/test/project-name.test.js index 4b8cdce7feb3..1672fce66501 100644 --- a/packages/create-astro/test/project-name.test.js +++ b/packages/create-astro/test/project-name.test.js @@ -92,4 +92,48 @@ describe('project name', () => { expect(context.cwd).to.eq('@astro/site'); expect(context.projectName).to.eq('@astro/site'); }); + + it('--yes', async () => { + const context = { + projectName: '', + cwd: './foo/bar/baz', + yes: true, + prompt: () => {}, + }; + await projectName(context); + expect(context.projectName).to.eq('baz'); + }); + + it('dry run with name', async () => { + const context = { + projectName: '', + cwd: './foo/bar/baz', + dryRun: true, + prompt: () => {}, + }; + await projectName(context); + expect(context.projectName).to.eq('baz'); + }); + + it('dry run with dot', async () => { + const context = { + projectName: '', + cwd: '.', + dryRun: true, + prompt: () => ({ name: 'foobar' }), + }; + await projectName(context); + expect(context.projectName).to.eq('foobar'); + }); + + it('dry run with empty', async () => { + const context = { + projectName: '', + cwd: './test/fixtures/empty', + dryRun: true, + prompt: () => ({ name: 'foobar' }), + }; + await projectName(context); + expect(context.projectName).to.eq('empty'); + }); });