summaryrefslogtreecommitdiff
path: root/packages/create-astro
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--packages/create-astro/src/actions/project-name.ts13
-rw-r--r--packages/create-astro/src/actions/template.ts7
-rw-r--r--packages/create-astro/test/project-name.test.js44
3 files changed, 61 insertions, 3 deletions
diff --git a/packages/create-astro/src/actions/project-name.ts b/packages/create-astro/src/actions/project-name.ts
index 533240efd..bb4d5feb6 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<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 === './') {
diff --git a/packages/create-astro/src/actions/template.ts b/packages/create-astro/src/actions/template.ts
index 60a77104f..ae9e6eb5a 100644
--- a/packages/create-astro/src/actions/template.ts
+++ b/packages/create-astro/src/actions/template.ts
@@ -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',
diff --git a/packages/create-astro/test/project-name.test.js b/packages/create-astro/test/project-name.test.js
index 4b8cdce7f..1672fce66 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');
+ });
});