diff options
Diffstat (limited to 'packages/create-astro')
-rwxr-xr-x | packages/create-astro/create-astro.mjs | 2 | ||||
-rw-r--r-- | packages/create-astro/src/index.ts | 94 | ||||
-rw-r--r-- | packages/create-astro/test/astro-add-step.test.js | 69 | ||||
-rw-r--r-- | packages/create-astro/test/install-step.test.js | 5 | ||||
-rw-r--r-- | packages/create-astro/test/utils.js | 8 |
5 files changed, 45 insertions, 133 deletions
diff --git a/packages/create-astro/create-astro.mjs b/packages/create-astro/create-astro.mjs index be032f3c2..7f09ba700 100755 --- a/packages/create-astro/create-astro.mjs +++ b/packages/create-astro/create-astro.mjs @@ -3,7 +3,7 @@ const currentVersion = process.versions.node; const requiredMajorVersion = parseInt(currentVersion.split('.')[0], 10); -const minimumMajorVersion = 12; +const minimumMajorVersion = 14; if (requiredMajorVersion < minimumMajorVersion) { console.error(`Node.js v${currentVersion} is out of date and unsupported!`); diff --git a/packages/create-astro/src/index.ts b/packages/create-astro/src/index.ts index 08396a4e8..4960062cb 100644 --- a/packages/create-astro/src/index.ts +++ b/packages/create-astro/src/index.ts @@ -2,7 +2,7 @@ import degit from 'degit'; import { execa, execaCommand } from 'execa'; import fs from 'fs'; -import { bgCyan, black, bold, cyan, gray, green, red, yellow } from 'kleur/colors'; +import { bgCyan, black, bold, cyan, dim, gray, green, red, yellow } from 'kleur/colors'; import ora from 'ora'; import path from 'path'; import prompts from 'prompts'; @@ -11,6 +11,13 @@ import { loadWithRocketGradient, rocketAscii } from './gradient.js'; import { defaultLogLevel, logger } from './logger.js'; import { TEMPLATES } from './templates.js'; +function wait(ms: number) { + return new Promise((resolve) => setTimeout(resolve, ms)); +} +function logAndWait(message: string, ms: number = 100) { + console.log(message); + return wait(ms); +} // NOTE: In the v7.x version of npm, the default behavior of `npm init` was changed // to no longer require `--` to pass args and instead pass `--` directly to us. This // broke our arg parser, since `--` is a special kind of flag. Filtering for `--` here @@ -43,6 +50,7 @@ export async function main() { logger.debug('Verbose logging turned on'); console.log(`\n${bold('Welcome to Astro!')} ${gray(`(create-astro v${version})`)}`); + console.log(`Lets walk through setting up your new Astro project.\n`); let cwd = args['_'][2] as string; @@ -64,7 +72,7 @@ export async function main() { const dirResponse = await prompts({ type: 'text', name: 'directory', - message: 'Where would you like to create your app?', + message: 'Where would you like to create your new project?', initial: './my-astro-site', validate(value) { if (!isEmpty(value)) { @@ -84,7 +92,7 @@ export async function main() { { type: 'select', name: 'template', - message: 'Which app template would you like to use?', + message: 'Which template would you like to use?', choices: TEMPLATES, }, ]); @@ -112,7 +120,7 @@ export async function main() { }); // Copy - if (!args.dryrun) { + if (!args.dryRun) { try { emitter.on('info', (info) => { logger.debug(info.message); @@ -175,11 +183,9 @@ export async function main() { initial: true, }); - if (!installResponse) { - process.exit(0); - } - - if (installResponse.install && !args.dryrun) { + if (args.dryRun) { + ora().info(dim(`--dry-run enabled, skipping.`)); + } else if (installResponse.install) { const installExec = execa(pkgManager, ['install'], { cwd }); const installingPackagesMsg = `Installing packages${emojiWithFallback(' 📦', '...')}`; const installSpinner = await loadWithRocketGradient(installingPackagesMsg); @@ -194,69 +200,51 @@ export async function main() { }); installSpinner.text = green('Packages installed!'); installSpinner.succeed(); - } - - const astroAddCommand = installResponse.install - ? 'astro add --yes' - : `${pkgManagerExecCommand(pkgManager)} astro@latest add --yes`; - - const astroAddResponse = await prompts({ - type: 'confirm', - name: 'astroAdd', - message: `Run "${astroAddCommand}?" This lets you optionally add component frameworks (ex. React), CSS frameworks (ex. Tailwind), and more.`, - initial: true, - }); - - if (!astroAddResponse) { - process.exit(0); - } - - if (!astroAddResponse.astroAdd) { - ora().info( - `No problem. You can always run "${pkgManagerExecCommand(pkgManager)} astro add" later!` - ); - } - - if (astroAddResponse.astroAdd && !args.dryrun) { - await execaCommand( - astroAddCommand, - astroAddCommand === 'astro add --yes' - ? { cwd, stdio: 'inherit', localDir: cwd, preferLocal: true } - : { cwd, stdio: 'inherit' } - ); + } else { + ora().info(dim(`No problem! You can install dependencies yourself after setup.`)); } const gitResponse = await prompts({ type: 'confirm', name: 'git', - message: 'Initialize a git repository?', + message: `Initialize a new git repository? ${dim('This can be useful to track changes.')}`, initial: true, }); - if (!gitResponse) { - process.exit(0); - } - - if (gitResponse.git && !args.dryrun) { + if (args.dryRun) { + ora().info(dim(`--dry-run enabled, skipping.`)); + } else if (gitResponse.git) { await execaCommand('git init', { cwd }); + } else { + ora().info( + dim(`Sounds good! You can come back and run ${cyan(`git init`)} later.`) + ); } - ora({ text: green('Done. Ready for liftoff!') }).succeed(); + ora().succeed('Setup complete.'); + ora({ text: green('Ready for liftoff!') }).succeed(); + await wait(300); + console.log(`\n${bgCyan(black(' Next steps '))}\n`); - const projectDir = path.relative(process.cwd(), cwd); + let projectDir = path.relative(process.cwd(), cwd); const devCmd = pkgManager === 'npm' ? 'npm run dev' : `${pkgManager} dev`; - console.log( + await logAndWait( `You can now ${bold(cyan('cd'))} into the ${bold(cyan(projectDir))} project directory.` ); - console.log( + await logAndWait( `Run ${bold(cyan(devCmd))} to start the Astro dev server. ${bold(cyan('CTRL-C'))} to close.` ); - if (!installResponse.install) { - console.log(yellow(`Remember to install dependencies first!`)); - } - console.log(`\nStuck? Come join us at ${bold(cyan('https://astro.build/chat'))}`); + await logAndWait( + `Add frameworks like ${bold(cyan('react'))} and ${bold( + cyan('tailwind') + )} to your project using ${bold(cyan('astro add'))}` + ); + await logAndWait(''); + await logAndWait(`Stuck? Come join us at ${bold(cyan('https://astro.build/chat'))}`, 1000); + await logAndWait(dim('Good luck out there, astronaut.')); + await logAndWait('', 300); } function emojiWithFallback(char: string, fallback: string) { diff --git a/packages/create-astro/test/astro-add-step.test.js b/packages/create-astro/test/astro-add-step.test.js deleted file mode 100644 index 73d963ed0..000000000 --- a/packages/create-astro/test/astro-add-step.test.js +++ /dev/null @@ -1,69 +0,0 @@ -import { setup, promiseWithTimeout, timeout, PROMPT_MESSAGES } from './utils.js'; -import { sep } from 'path'; -import fs from 'fs'; -import os from 'os'; - -// reset package manager in process.env -// prevents test issues when running with pnpm -const FAKE_PACKAGE_MANAGER = 'npm'; -let initialEnvValue = null; - -describe('[create-astro] astro add', function () { - this.timeout(timeout); - let tempDir = ''; - beforeEach(async () => { - tempDir = await fs.promises.mkdtemp(`${os.tmpdir()}${sep}`); - }); - this.beforeAll(() => { - initialEnvValue = process.env.npm_config_user_agent; - process.env.npm_config_user_agent = FAKE_PACKAGE_MANAGER; - }); - this.afterAll(() => { - process.env.npm_config_user_agent = initialEnvValue; - }); - - it('should use "astro add" when user has installed dependencies', function () { - const { stdout, stdin } = setup([tempDir]); - return promiseWithTimeout((resolve) => { - const seen = new Set(); - const installPrompt = PROMPT_MESSAGES.install('npm'); - stdout.on('data', (chunk) => { - if (!seen.has(PROMPT_MESSAGES.template) && chunk.includes(PROMPT_MESSAGES.template)) { - seen.add(PROMPT_MESSAGES.template); - // respond with "enter key" - stdin.write('\x0D'); - } - if (!seen.has(installPrompt) && chunk.includes(installPrompt)) { - seen.add(installPrompt); - stdin.write('\x0D'); - } - if (chunk.includes(PROMPT_MESSAGES.astroAdd('astro add --yes'))) { - resolve(); - } - }); - }); - }); - - it('should use "npx astro@latest add" when use has NOT installed dependencies', function () { - const { stdout, stdin } = setup([tempDir]); - return promiseWithTimeout((resolve) => { - const seen = new Set(); - const installPrompt = PROMPT_MESSAGES.install('npm'); - stdout.on('data', (chunk) => { - if (!seen.has(PROMPT_MESSAGES.template) && chunk.includes(PROMPT_MESSAGES.template)) { - seen.add(PROMPT_MESSAGES.template); - // respond with "enter key" - stdin.write('\x0D'); - } - if (!seen.has(installPrompt) && chunk.includes(installPrompt)) { - seen.add(installPrompt); - // respond with "no, then enter key" - stdin.write('n\x0D'); - } - if (chunk.includes(PROMPT_MESSAGES.astroAdd('npx astro@latest add --yes'))) { - resolve(); - } - }); - }); - }); -}); diff --git a/packages/create-astro/test/install-step.test.js b/packages/create-astro/test/install-step.test.js index d8219b520..3f1dea4a9 100644 --- a/packages/create-astro/test/install-step.test.js +++ b/packages/create-astro/test/install-step.test.js @@ -44,7 +44,6 @@ describe('[create-astro] install', function () { return promiseWithTimeout((resolve) => { const seen = new Set(); const installPrompt = PROMPT_MESSAGES.install(FAKE_PACKAGE_MANAGER); - const astroAddPrompt = PROMPT_MESSAGES.astroAdd(); stdout.on('data', (chunk) => { if (!seen.has(PROMPT_MESSAGES.template) && chunk.includes(PROMPT_MESSAGES.template)) { seen.add(PROMPT_MESSAGES.template); @@ -56,10 +55,6 @@ describe('[create-astro] install', function () { // respond with "no, then enter key" stdin.write('n\x0D'); } - if (!seen.has(astroAddPrompt) && chunk.includes(astroAddPrompt)) { - seen.add(astroAddPrompt); - stdin.write('n\x0D'); - } if (!seen.has(PROMPT_MESSAGES.git) && chunk.includes(PROMPT_MESSAGES.git)) { seen.add(PROMPT_MESSAGES.git); stdin.write('\x0D'); diff --git a/packages/create-astro/test/utils.js b/packages/create-astro/test/utils.js index 964ae6a20..9d227f6b5 100644 --- a/packages/create-astro/test/utils.js +++ b/packages/create-astro/test/utils.js @@ -24,12 +24,10 @@ export function promiseWithTimeout(testFn) { } export const PROMPT_MESSAGES = { - directory: 'Where would you like to create your app?', - template: 'Which app template would you like to use?', + directory: 'Where would you like to create your new project?', + template: 'Which template would you like to use?', install: (pkgManager) => `Would you like us to run "${pkgManager} install?"`, - astroAdd: (astroAddCommand = 'npx astro@latest add --yes') => - `Run "${astroAddCommand}?" This lets you optionally add component frameworks (ex. React), CSS frameworks (ex. Tailwind), and more.`, - git: 'Initialize a git repository?', + git: 'Initialize a new git repository?', }; export function setup(args = []) { |