feat(create-astro): Update flag behavior for template and project-name (#8551)

This commit is contained in:
Jacob Lamb 2023-09-19 02:09:27 -07:00 committed by GitHub
parent d0e513f214
commit 1d5b3f079d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 66 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
'create-astro': minor
---
Adds `--yes` and `dry-run` flags to project-name and the `yes` flag to template.

View file

@ -6,7 +6,7 @@ import { info, log, title } from '../messages.js';
import { isEmpty, toValidName } from './shared.js';
export async function projectName(ctx: Pick<Context, 'cwd' | 'prompt' | 'projectName' | 'exit'>) {
export async function projectName(ctx: Pick<Context, 'cwd' | 'yes' | 'dryRun' | 'prompt' | 'projectName' | 'exit'>) {
await checkCwd(ctx.cwd);
if (!ctx.cwd || !isEmpty(ctx.cwd)) {
@ -14,6 +14,13 @@ export async function projectName(ctx: Pick<Context, 'cwd' | 'prompt' | 'project
await info('Hmm...', `${color.reset(`"${ctx.cwd}"`)}${color.dim(` is not empty!`)}`);
}
if (ctx.yes) {
ctx.projectName = generateProjectName();
ctx.cwd = `./${ctx.projectName}`;
await info('dir', `Project created at ./${ctx.projectName}`);
return;
}
const { name } = await ctx.prompt({
name: 'name',
type: 'text',
@ -33,6 +40,10 @@ export async function projectName(ctx: Pick<Context, 'cwd' | 'prompt' | 'project
ctx.cwd = name!.trim();
ctx.projectName = toValidName(name!);
if (ctx.dryRun) {
await info('--dry-run', 'Skipping project naming');
return;
}
} else {
let name = ctx.cwd;
if (name === '.' || name === './') {

View file

@ -6,8 +6,11 @@ import fs from 'node:fs';
import path from 'node:path';
import { error, info, spinner, title } from '../messages.js';
export async function template(ctx: Pick<Context, 'template' | 'prompt' | 'dryRun' | 'exit'>) {
if (!ctx.template) {
export async function template(ctx: Pick<Context, 'template' | 'prompt' | 'yes' | 'dryRun' | 'exit'>) {
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',

View file

@ -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');
});
});