From d8919b1a2197616b70ec57f0fb00b0bde6943e43 Mon Sep 17 00:00:00 2001 From: Reuben Tier <64310361+TheOtterlord@users.noreply.github.com> Date: Fri, 20 Jan 2023 14:53:12 +0000 Subject: [PATCH] Add headless support to create-astro (#5898) * Add headless support to create-astro * Document new flags for create-astro --- .changeset/kind-panthers-doubt.md | 5 ++++ packages/create-astro/README.md | 3 +++ packages/create-astro/src/index.ts | 41 +++++++++++++++++------------- 3 files changed, 32 insertions(+), 17 deletions(-) create mode 100644 .changeset/kind-panthers-doubt.md diff --git a/.changeset/kind-panthers-doubt.md b/.changeset/kind-panthers-doubt.md new file mode 100644 index 000000000000..a38d6b775c9b --- /dev/null +++ b/.changeset/kind-panthers-doubt.md @@ -0,0 +1,5 @@ +--- +'create-astro': patch +--- + +Support headless runs with `-y` / `--yes` diff --git a/packages/create-astro/README.md b/packages/create-astro/README.md index 661d56f480be..4c3a887ffd2c 100644 --- a/packages/create-astro/README.md +++ b/packages/create-astro/README.md @@ -43,6 +43,8 @@ May be provided in place of prompts | `--template` | Specify the template name ([list][examples]) | | `--commit` | Specify a specific Git commit or branch to use from this repo (by default, `main` branch of this repo will be used) | | `--fancy` | For Windows users, `--fancy` will enable full unicode support | +| `--typescript` | Specify the [tsconfig][typescript] to use | +| `--yes`/`-y` | Skip prompts and use default values | ### Debugging @@ -60,3 +62,4 @@ yarn create astro my-astro-project --verbose ``` [examples]: https://github.com/withastro/astro/tree/main/examples +[typescript]: https://github.com/withastro/astro/tree/main/packages/astro/tsconfigs diff --git a/packages/create-astro/src/index.ts b/packages/create-astro/src/index.ts index 1e934fc9b6b9..089fd47825a5 100644 --- a/packages/create-astro/src/index.ts +++ b/packages/create-astro/src/index.ts @@ -30,7 +30,7 @@ import { TEMPLATES } from './templates.js'; // broke our arg parser, since `--` is a special kind of flag. Filtering for `--` here // fixes the issue so that create-astro now works on all npm version. const cleanArgv = process.argv.filter((arg) => arg !== '--'); -const args = yargs(cleanArgv, { boolean: ['fancy'] }); +const args = yargs(cleanArgv, { boolean: ['fancy', 'y'], alias: { y: 'yes' } }); // Always skip Houston on Windows (for now) if (platform() === 'win32') args.skipHouston = true; prompts.override(args); @@ -77,7 +77,7 @@ const VALID_PROJECT_DIRECTORY_SAFE_LIST = [ function isValidProjectDirectory(dirPath: string) { if (!fs.existsSync(dirPath)) { - return true; + return true; } const conflicts = fs.readdirSync(dirPath).filter((content) => { @@ -93,14 +93,14 @@ const FILES_TO_REMOVE = ['.stackblitzrc', 'sandbox.config.json', 'CHANGELOG.md'] // Please also update the installation instructions in the docs at https://github.com/withastro/docs/blob/main/src/pages/en/install/auto.md if you make any changes to the flow or wording here. export async function main() { - const pkgManager = detectPackageManager()?.name || 'npm'; + const pkgManager = detectPackageManager()?.name || 'npm'; const [username, version] = await Promise.all([getName(), getVersion()]); logger.debug('Verbose logging turned on'); if (!args.skipHouston) { - await say( - [ - [ + await say( + [ + [ 'Welcome', 'to', label('astro', color.bgGreen, color.black), @@ -213,7 +213,7 @@ export async function main() { templateSpinner.text = green('Template copied!'); templateSpinner.succeed(); - const installResponse = await prompts( + const install = args.y ? true : (await prompts( { type: 'confirm', name: 'install', @@ -232,11 +232,11 @@ export async function main() { process.exit(1); }, } - ); + )).install; if (args.dryRun) { ora().info(dim(`--dry-run enabled, skipping.`)); - } else if (installResponse.install) { + } else if (install) { const installExec = execa(pkgManager, ['install'], { cwd }); const installingPackagesMsg = `Installing packages${emojiWithFallback(' 📦', '...')}`; const installSpinner = await loadWithRocketGradient(installingPackagesMsg); @@ -255,7 +255,7 @@ export async function main() { await info('No problem!', 'Remember to install dependencies after setup.'); } - const gitResponse = await prompts( + const gitResponse = args.y ? true : (await prompts( { type: 'confirm', name: 'git', @@ -270,11 +270,11 @@ export async function main() { process.exit(1); }, } - ); + )).git; if (args.dryRun) { ora().info(dim(`--dry-run enabled, skipping.`)); - } else if (gitResponse.git) { + } else if (gitResponse) { await execaCommand('git init', { cwd }); ora().succeed('Git repository created!'); } else { @@ -284,7 +284,14 @@ export async function main() { ); } - const tsResponse = await prompts( + if (args.y && !args.typescript) { + ora().warn( + dim('--typescript missing. Defaulting to "strict"') + ); + args.typescript = 'strict'; + } + + const tsResponse = args.typescript || (await prompts( { type: 'select', name: 'typescript', @@ -306,22 +313,22 @@ export async function main() { process.exit(1); }, } - ); + )).typescript; - if (tsResponse.typescript === 'unsure') { + if (tsResponse === 'unsure') { await typescriptByDefault(); tsResponse.typescript = 'base'; } if (args.dryRun) { ora().info(dim(`--dry-run enabled, skipping.`)); - } else if (tsResponse.typescript) { + } else if (tsResponse) { const templateTSConfigPath = path.join(cwd, 'tsconfig.json'); fs.readFile(templateTSConfigPath, (err, data) => { if (err && err.code === 'ENOENT') { // If the template doesn't have a tsconfig.json, let's add one instead fs.writeFileSync( templateTSConfigPath, - stringify({ extends: `astro/tsconfigs/${tsResponse.typescript}` }, null, 2) + stringify({ extends: `astro/tsconfigs/${tsResponse}` }, null, 2) ); return;