diff --git a/.changeset/clever-taxis-smell.md b/.changeset/clever-taxis-smell.md new file mode 100644 index 000000000..09752ee3c --- /dev/null +++ b/.changeset/clever-taxis-smell.md @@ -0,0 +1,5 @@ +--- +'astro': minor +--- + +astro add - move configuration updates to final step diff --git a/packages/astro/src/core/add/index.ts b/packages/astro/src/core/add/index.ts index 678004b08..efcf75789 100644 --- a/packages/astro/src/core/add/index.ts +++ b/packages/astro/src/core/add/index.ts @@ -95,17 +95,77 @@ export default async function add(names: string[], { cwd, flags, logging, teleme }); return; } - let configURL: URL | undefined; + + // Some packages might have a common alias! We normalize those here. + const integrationNames = names.map((name) => (ALIASES.has(name) ? ALIASES.get(name)! : name)); + const integrations = await validateIntegrations(integrationNames); + let installResult = await tryToInstallIntegrations({ integrations, cwd, flags, logging }); const root = pathToFileURL(cwd ? path.resolve(cwd) : process.cwd()); + // Append forward slash to compute relative paths + root.href = appendForwardSlash(root.href); + + switch (installResult) { + case UpdateResult.updated: { + if (integrations.find((integration) => integration.id === 'tailwind')) { + const possibleConfigFiles = [ + './tailwind.config.cjs', + './tailwind.config.mjs', + './tailwind.config.js', + ].map((p) => fileURLToPath(new URL(p, root))); + let alreadyConfigured = false; + for (const possibleConfigPath of possibleConfigFiles) { + if (existsSync(possibleConfigPath)) { + alreadyConfigured = true; + break; + } + } + if (!alreadyConfigured) { + info( + logging, + null, + `\n ${magenta( + `Astro will generate a minimal ${bold('./tailwind.config.cjs')} file.` + )}\n` + ); + if (await askToContinue({ flags })) { + await fs.writeFile( + fileURLToPath(new URL('./tailwind.config.cjs', root)), + TAILWIND_CONFIG_STUB, + { encoding: 'utf-8' } + ); + debug('add', `Generated default ./tailwind.config.cjs file`); + } + } else { + debug('add', `Using existing Tailwind configuration`); + } + } + break; + } + case UpdateResult.cancelled: { + info( + logging, + null, + msg.cancelled( + `Dependencies ${bold('NOT')} installed.`, + `Be sure to install them manually before continuing!` + ) + ); + break; + } + case UpdateResult.failure: { + throw createPrettyError(new Error(`Unable to install dependencies`)); + } + } + const rawConfigPath = await resolveConfigPath({ cwd, flags }); - configURL = rawConfigPath ? pathToFileURL(rawConfigPath) : undefined; + let configURL = rawConfigPath ? pathToFileURL(rawConfigPath) : undefined; applyPolyfill(); if (configURL) { debug('add', `Found config at ${configURL}`); } else { info(logging, 'add', `Unable to locate a config file, generating one for you.`); - configURL = new URL('./astro.config.mjs', appendForwardSlash(root.href)); + configURL = new URL('./astro.config.mjs', root); await fs.writeFile(fileURLToPath(configURL), ASTRO_CONFIG_STUB, { encoding: 'utf-8' }); } @@ -115,11 +175,6 @@ export default async function add(names: string[], { cwd, flags, logging, teleme `Unable to use "astro add" with package.json configuration. Try migrating to \`astro.config.mjs\` and try again.` ); } - - // Some packages might have a common alias! We normalize those here. - const integrationNames = names.map((name) => (ALIASES.has(name) ? ALIASES.get(name)! : name)); - const integrations = await validateIntegrations(integrationNames); - let ast: t.File | null = null; try { ast = await parseAstroConfig(configURL); @@ -165,7 +220,6 @@ export default async function add(names: string[], { cwd, flags, logging, teleme } let configResult: UpdateResult | undefined; - let installResult: UpdateResult | undefined; if (ast) { try { @@ -204,71 +258,19 @@ export default async function add(names: string[], { cwd, flags, logging, teleme } info(logging, null, msg.success(`Configuration up-to-date.`)); - break; + return; } - } - - installResult = await tryToInstallIntegrations({ integrations, cwd, flags, logging }); - - switch (installResult) { - case UpdateResult.updated: { - const len = integrations.length; - if (integrations.find((integration) => integration.id === 'tailwind')) { - const possibleConfigFiles = [ - './tailwind.config.cjs', - './tailwind.config.mjs', - './tailwind.config.js', - ].map((p) => fileURLToPath(new URL(p, configURL))); - let alreadyConfigured = false; - for (const possibleConfigPath of possibleConfigFiles) { - if (existsSync(possibleConfigPath)) { - alreadyConfigured = true; - break; - } - } - if (!alreadyConfigured) { - info( - logging, - null, - `\n ${magenta( - `Astro will generate a minimal ${bold('./tailwind.config.cjs')} file.` - )}\n` - ); - if (await askToContinue({ flags })) { - await fs.writeFile( - fileURLToPath(new URL('./tailwind.config.cjs', configURL)), - TAILWIND_CONFIG_STUB, - { encoding: 'utf-8' } - ); - debug('add', `Generated default ./tailwind.config.cjs file`); - } - } else { - debug('add', `Using existing Tailwind configuration`); - } - } + default: { const list = integrations.map((integration) => ` - ${integration.packageName}`).join('\n'); info( logging, null, msg.success( - `Added the following integration${len === 1 ? '' : 's'} to your project:\n${list}` + `Added the following integration${ + integrations.length === 1 ? '' : 's' + } to your project:\n${list}` ) ); - return; - } - case UpdateResult.cancelled: { - info( - logging, - null, - msg.cancelled( - `Dependencies ${bold('NOT')} installed.`, - `Be sure to install them manually before continuing!` - ) - ); - return; - } - case UpdateResult.failure: { - throw createPrettyError(new Error(`Unable to install dependencies`)); } } }