summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jacob Lamb <jacobtlamb@hey.com> 2023-09-19 02:09:27 -0700
committerGravatar GitHub <noreply@github.com> 2023-09-19 17:09:27 +0800
commit1d5b3f079d0b4aa5a5c46f97b8b724ab88497fbe (patch)
tree88447f1b3f7a9bf40d620101c831919c6242e112
parentd0e513f214fe3cb30bab6d98936cda796477f2f8 (diff)
downloadastro-1d5b3f079d0b4aa5a5c46f97b8b724ab88497fbe.tar.gz
astro-1d5b3f079d0b4aa5a5c46f97b8b724ab88497fbe.tar.zst
astro-1d5b3f079d0b4aa5a5c46f97b8b724ab88497fbe.zip
feat(create-astro): Update flag behavior for template and project-name (#8551)
Diffstat (limited to '')
-rw-r--r--.changeset/plenty-taxis-hunt.md5
-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
4 files changed, 66 insertions, 3 deletions
diff --git a/.changeset/plenty-taxis-hunt.md b/.changeset/plenty-taxis-hunt.md
new file mode 100644
index 000000000..7747a2a76
--- /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 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');
+ });
});