feat(create-astro): Update flag behavior for template and project-name (#8551)
This commit is contained in:
parent
d0e513f214
commit
1d5b3f079d
4 changed files with 66 additions and 3 deletions
5
.changeset/plenty-taxis-hunt.md
Normal file
5
.changeset/plenty-taxis-hunt.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'create-astro': minor
|
||||||
|
---
|
||||||
|
|
||||||
|
Adds `--yes` and `dry-run` flags to project-name and the `yes` flag to template.
|
|
@ -6,7 +6,7 @@ import { info, log, title } from '../messages.js';
|
||||||
|
|
||||||
import { isEmpty, toValidName } from './shared.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);
|
await checkCwd(ctx.cwd);
|
||||||
|
|
||||||
if (!ctx.cwd || !isEmpty(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!`)}`);
|
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({
|
const { name } = await ctx.prompt({
|
||||||
name: 'name',
|
name: 'name',
|
||||||
type: 'text',
|
type: 'text',
|
||||||
|
@ -33,6 +40,10 @@ export async function projectName(ctx: Pick<Context, 'cwd' | 'prompt' | 'project
|
||||||
|
|
||||||
ctx.cwd = name!.trim();
|
ctx.cwd = name!.trim();
|
||||||
ctx.projectName = toValidName(name!);
|
ctx.projectName = toValidName(name!);
|
||||||
|
if (ctx.dryRun) {
|
||||||
|
await info('--dry-run', 'Skipping project naming');
|
||||||
|
return;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
let name = ctx.cwd;
|
let name = ctx.cwd;
|
||||||
if (name === '.' || name === './') {
|
if (name === '.' || name === './') {
|
||||||
|
|
|
@ -6,8 +6,11 @@ import fs from 'node:fs';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import { error, info, spinner, title } from '../messages.js';
|
import { error, info, spinner, title } from '../messages.js';
|
||||||
|
|
||||||
export async function template(ctx: Pick<Context, 'template' | 'prompt' | 'dryRun' | 'exit'>) {
|
export async function template(ctx: Pick<Context, 'template' | 'prompt' | 'yes' | 'dryRun' | 'exit'>) {
|
||||||
if (!ctx.template) {
|
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({
|
const { template: tmpl } = await ctx.prompt({
|
||||||
name: 'template',
|
name: 'template',
|
||||||
type: 'select',
|
type: 'select',
|
||||||
|
|
|
@ -92,4 +92,48 @@ describe('project name', () => {
|
||||||
expect(context.cwd).to.eq('@astro/site');
|
expect(context.cwd).to.eq('@astro/site');
|
||||||
expect(context.projectName).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');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue