Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 11 commits into from
Sep 19, 2023
5 changes: 5 additions & 0 deletions .changeset/plenty-taxis-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-astro': minor
---

Adds `--yes` and `dry-run` flags to project-name and the `yes` flag to template.
13 changes: 12 additions & 1 deletion packages/create-astro/src/actions/project-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@ 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)) {
if (!isEmpty(ctx.cwd)) {
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',
Expand All @@ -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 === './') {
Expand Down
7 changes: 5 additions & 2 deletions packages/create-astro/src/actions/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
44 changes: 44 additions & 0 deletions packages/create-astro/test/project-name.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
Loading