From ed093ba912621c21705776683d527f28dec92867 Mon Sep 17 00:00:00 2001 From: Jacob Lamb Date: Wed, 13 Sep 2023 13:55:38 -0700 Subject: [PATCH 1/9] feat(cli): Support --yes flag for automatic project name and template selection - Automatically choose the 'basics' template when --yes flag is provided. - Display relevant info messages based on the chosen template and project name. --- packages/create-astro/src/actions/project-name.ts | 10 ++++++++-- packages/create-astro/src/actions/template.ts | 7 +++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/create-astro/src/actions/project-name.ts b/packages/create-astro/src/actions/project-name.ts index 533240efd42d..407e5b3c7750 100644 --- a/packages/create-astro/src/actions/project-name.ts +++ b/packages/create-astro/src/actions/project-name.ts @@ -6,12 +6,18 @@ import { info, log, title } from '../messages.js'; import { isEmpty, toValidName } from './shared.js'; -export async function projectName(ctx: Pick) { +export async function projectName(ctx: Pick) { 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!`)}`); + await info('Hmm...', `${color.reset(`./${ctx.cwd}`)}${color.dim(` is not empty!`)}`); + } + + if (ctx.yes) { + ctx.projectName = generateProjectName(); + await info('dir', `Project created at ./${ctx.projectName}`); + return; } const { name } = await ctx.prompt({ diff --git a/packages/create-astro/src/actions/template.ts b/packages/create-astro/src/actions/template.ts index 60a77104f028..f1dda50810f7 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) { - if (!ctx.template) { +export async function template(ctx: Pick) { + if (ctx.yes) { + ctx.template = 'basics'; + await info('tmpl', `Using default ${color.reset(ctx.template)}${color.dim(' as project template')}`); + } else if (!ctx.template) { const { template: tmpl } = await ctx.prompt({ name: 'template', type: 'select', From 09cdd29c1f4a4ff650e88fd848deed97a1d618ce Mon Sep 17 00:00:00 2001 From: Jacob Lamb Date: Wed, 13 Sep 2023 13:58:27 -0700 Subject: [PATCH 2/9] Create plenty-taxis-hunt.md --- .changeset/plenty-taxis-hunt.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/plenty-taxis-hunt.md diff --git a/.changeset/plenty-taxis-hunt.md b/.changeset/plenty-taxis-hunt.md new file mode 100644 index 000000000000..f911734fb300 --- /dev/null +++ b/.changeset/plenty-taxis-hunt.md @@ -0,0 +1,5 @@ +--- +'create-astro': minor +--- + +- add `--yes` flag for project name and template From 906c9d8d77860dbdf91c1c09efe4a8e1fc99611b Mon Sep 17 00:00:00 2001 From: Jacob Lamb Date: Wed, 13 Sep 2023 15:46:24 -0700 Subject: [PATCH 3/9] Checking --- packages/create-astro/src/actions/project-name.ts | 9 ++++++++- packages/create-astro/src/actions/template.ts | 7 ++----- packages/create-astro/test/project-name.test.js | 5 +++-- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/create-astro/src/actions/project-name.ts b/packages/create-astro/src/actions/project-name.ts index 407e5b3c7750..38725829b95b 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) { +export async function projectName(ctx: Pick) { await checkCwd(ctx.cwd); if (!ctx.cwd || !isEmpty(ctx.cwd)) { @@ -14,6 +14,7 @@ export async function projectName(ctx: Pick) { - if (ctx.yes) { - ctx.template = 'basics'; - await info('tmpl', `Using default ${color.reset(ctx.template)}${color.dim(' as project template')}`); - } else if (!ctx.template) { +export async function template(ctx: Pick) { +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 4b8cdce7feb3..2922fd17f92b 100644 --- a/packages/create-astro/test/project-name.test.js +++ b/packages/create-astro/test/project-name.test.js @@ -15,7 +15,7 @@ describe('project name', () => { }); it('dot', async () => { - const context = { projectName: '', cwd: '.', prompt: () => ({ name: 'foobar' }) }; + const context = { projectName: '', cwd: '.', prompt: () => ({ name: 'foobar' }), yes: false }; await projectName(context); expect(fixture.hasMessage('"." is not empty!')).to.be.true; @@ -23,7 +23,7 @@ describe('project name', () => { }); it('dot slash', async () => { - const context = { projectName: '', cwd: './', prompt: () => ({ name: 'foobar' }) }; + const context = { projectName: '', cwd: './', prompt: () => ({ name: 'foobar' }), yes: false }; await projectName(context); expect(fixture.hasMessage('"./" is not empty!')).to.be.true; @@ -47,6 +47,7 @@ describe('project name', () => { projectName: '', cwd: './test/fixtures/not-empty', prompt: () => ({ name: 'foobar' }), + yes: false, }; await projectName(context); From 4a1c2f87e95ed5ceec0247cf2e8c33c5af0bc6db Mon Sep 17 00:00:00 2001 From: Jacob Lamb Date: Wed, 13 Sep 2023 16:19:08 -0700 Subject: [PATCH 4/9] Revert some changes --- packages/create-astro/src/actions/project-name.ts | 5 +---- packages/create-astro/test/project-name.test.js | 5 ++--- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/packages/create-astro/src/actions/project-name.ts b/packages/create-astro/src/actions/project-name.ts index 38725829b95b..c1cf7f2842a2 100644 --- a/packages/create-astro/src/actions/project-name.ts +++ b/packages/create-astro/src/actions/project-name.ts @@ -11,10 +11,9 @@ export async function projectName(ctx: Pick { }); it('dot', async () => { - const context = { projectName: '', cwd: '.', prompt: () => ({ name: 'foobar' }), yes: false }; + const context = { projectName: '', cwd: '.', prompt: () => ({ name: 'foobar' }) }; await projectName(context); expect(fixture.hasMessage('"." is not empty!')).to.be.true; @@ -23,7 +23,7 @@ describe('project name', () => { }); it('dot slash', async () => { - const context = { projectName: '', cwd: './', prompt: () => ({ name: 'foobar' }), yes: false }; + const context = { projectName: '', cwd: './', prompt: () => ({ name: 'foobar' }) }; await projectName(context); expect(fixture.hasMessage('"./" is not empty!')).to.be.true; @@ -47,7 +47,6 @@ describe('project name', () => { projectName: '', cwd: './test/fixtures/not-empty', prompt: () => ({ name: 'foobar' }), - yes: false, }; await projectName(context); From 08f48cd5565f3556c71222ece01a4736445810e5 Mon Sep 17 00:00:00 2001 From: Jacob Lamb Date: Wed, 13 Sep 2023 16:44:16 -0700 Subject: [PATCH 5/9] Send off --- packages/create-astro/src/actions/project-name.ts | 1 + packages/create-astro/src/actions/template.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/create-astro/src/actions/project-name.ts b/packages/create-astro/src/actions/project-name.ts index c1cf7f2842a2..bb4d5feb6618 100644 --- a/packages/create-astro/src/actions/project-name.ts +++ b/packages/create-astro/src/actions/project-name.ts @@ -16,6 +16,7 @@ export async function projectName(ctx: Pick) { -if (!ctx.template) { + if (!ctx.template) { const { template: tmpl } = await ctx.prompt({ name: 'template', type: 'select', From 2e2815622e77a08015de046615609c323fd1d8e6 Mon Sep 17 00:00:00 2001 From: Jacob Lamb Date: Wed, 13 Sep 2023 16:56:09 -0700 Subject: [PATCH 6/9] Update template.ts --- packages/create-astro/src/actions/template.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/create-astro/src/actions/template.ts b/packages/create-astro/src/actions/template.ts index 60a77104f028..ae9e6eb5adb6 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) { - if (!ctx.template) { +export async function template(ctx: Pick) { + 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', From 38af93b61f421afaf09c71645bd06f85fb8a9d79 Mon Sep 17 00:00:00 2001 From: Jacob Lamb Date: Wed, 13 Sep 2023 16:58:26 -0700 Subject: [PATCH 7/9] Update changeset --- .changeset/plenty-taxis-hunt.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/plenty-taxis-hunt.md b/.changeset/plenty-taxis-hunt.md index f911734fb300..28984c8bfdf1 100644 --- a/.changeset/plenty-taxis-hunt.md +++ b/.changeset/plenty-taxis-hunt.md @@ -2,4 +2,4 @@ 'create-astro': minor --- -- add `--yes` flag for project name and template +Add `--yes` and `dry-run` flags to project name and `yes` flag to template. From b99946d48b969a1a4a3aed5b22b37571feeb665e Mon Sep 17 00:00:00 2001 From: Jacob Lamb Date: Wed, 13 Sep 2023 17:46:45 -0700 Subject: [PATCH 8/9] Add tests --- .changeset/plenty-taxis-hunt.md | 2 +- .../create-astro/test/project-name.test.js | 56 +++++++++++++++++++ packages/create-astro/test/template.test.js | 51 +++++++++++++++++ 3 files changed, 108 insertions(+), 1 deletion(-) diff --git a/.changeset/plenty-taxis-hunt.md b/.changeset/plenty-taxis-hunt.md index 28984c8bfdf1..7747a2a76fa9 100644 --- a/.changeset/plenty-taxis-hunt.md +++ b/.changeset/plenty-taxis-hunt.md @@ -2,4 +2,4 @@ 'create-astro': minor --- -Add `--yes` and `dry-run` flags to project name and `yes` flag to template. +Adds `--yes` and `dry-run` flags to project-name and the `yes` flag to template. diff --git a/packages/create-astro/test/project-name.test.js b/packages/create-astro/test/project-name.test.js index 4b8cdce7feb3..f12202be7397 100644 --- a/packages/create-astro/test/project-name.test.js +++ b/packages/create-astro/test/project-name.test.js @@ -54,6 +54,18 @@ describe('project name', () => { expect(context.projectName).to.eq('foobar'); }); + it('yes flag with non-empty cwd', async () => { + const context = { + projectName: '', + cwd: './test/fixtures/not-empty', + yes: true, + prompt: () => {}, + }; + await projectName(context); + + expect(context.projectName).to.not.eq(''); + }); + it('basic', async () => { const context = { projectName: '', cwd: '', prompt: () => ({ name: 'foobar' }) }; await projectName(context); @@ -92,4 +104,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'); + }); }); diff --git a/packages/create-astro/test/template.test.js b/packages/create-astro/test/template.test.js index 66c7f54468b5..d048d27b320d 100644 --- a/packages/create-astro/test/template.test.js +++ b/packages/create-astro/test/template.test.js @@ -33,4 +33,55 @@ describe('template', () => { expect(fixture.hasMessage('Using blog as project template')).to.be.true; }); + + it('none (--yes)', async () => { + const context = { + template: '', + cwd: '', + yes: true, + prompt: () => ({ template: 'blog' }), + }; + await template(context); + + expect(context.template).to.eq('blog'); + }); + + it('minimal (--yes)', async () => { + const context = { + template: 'minimal', + cwd: '', + yes: true, + prompt: () => {}, + }; + await template(context); + + expect(context.template).to.eq('minimal'); + expect(fixture.hasMessage('Using minimal as project template')).to.be.true; + }); + + it('basics (--yes)', async () => { + const context = { + template: 'basics', + cwd: '', + yes: true, + prompt: () => {}, + }; + await template(context); + + expect(context.template).to.eq('basics'); + expect(fixture.hasMessage('Using basics as project template')).to.be.true; + }); + + it('blog (--yes)', async () => { + const context = { + template: 'blog', + cwd: '', + yes: true, + prompt: () => {}, + }; + await template(context); + + expect(context.template).to.eq('blog'); + expect(fixture.hasMessage('Using blog as project template')).to.be.true; + }); }); From 5d9398b6d8c39df117c6712c10f754a87414a661 Mon Sep 17 00:00:00 2001 From: Jacob Lamb Date: Wed, 13 Sep 2023 17:53:01 -0700 Subject: [PATCH 9/9] Update tests --- .../create-astro/test/project-name.test.js | 12 ----- packages/create-astro/test/template.test.js | 51 ------------------- 2 files changed, 63 deletions(-) diff --git a/packages/create-astro/test/project-name.test.js b/packages/create-astro/test/project-name.test.js index f12202be7397..1672fce66501 100644 --- a/packages/create-astro/test/project-name.test.js +++ b/packages/create-astro/test/project-name.test.js @@ -54,18 +54,6 @@ describe('project name', () => { expect(context.projectName).to.eq('foobar'); }); - it('yes flag with non-empty cwd', async () => { - const context = { - projectName: '', - cwd: './test/fixtures/not-empty', - yes: true, - prompt: () => {}, - }; - await projectName(context); - - expect(context.projectName).to.not.eq(''); - }); - it('basic', async () => { const context = { projectName: '', cwd: '', prompt: () => ({ name: 'foobar' }) }; await projectName(context); diff --git a/packages/create-astro/test/template.test.js b/packages/create-astro/test/template.test.js index d048d27b320d..66c7f54468b5 100644 --- a/packages/create-astro/test/template.test.js +++ b/packages/create-astro/test/template.test.js @@ -33,55 +33,4 @@ describe('template', () => { expect(fixture.hasMessage('Using blog as project template')).to.be.true; }); - - it('none (--yes)', async () => { - const context = { - template: '', - cwd: '', - yes: true, - prompt: () => ({ template: 'blog' }), - }; - await template(context); - - expect(context.template).to.eq('blog'); - }); - - it('minimal (--yes)', async () => { - const context = { - template: 'minimal', - cwd: '', - yes: true, - prompt: () => {}, - }; - await template(context); - - expect(context.template).to.eq('minimal'); - expect(fixture.hasMessage('Using minimal as project template')).to.be.true; - }); - - it('basics (--yes)', async () => { - const context = { - template: 'basics', - cwd: '', - yes: true, - prompt: () => {}, - }; - await template(context); - - expect(context.template).to.eq('basics'); - expect(fixture.hasMessage('Using basics as project template')).to.be.true; - }); - - it('blog (--yes)', async () => { - const context = { - template: 'blog', - cwd: '', - yes: true, - prompt: () => {}, - }; - await template(context); - - expect(context.template).to.eq('blog'); - expect(fixture.hasMessage('Using blog as project template')).to.be.true; - }); });