[astro add] Move config update to final step (#4682)

* feat: move astro.config update to end

* chore: changeset
This commit is contained in:
Ben Holmes 2022-09-09 14:06:39 -04:00 committed by GitHub
parent 80ae62ad99
commit d1e6959147
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 73 additions and 66 deletions

View file

@ -0,0 +1,5 @@
---
'astro': minor
---
astro add - move configuration updates to final step

View file

@ -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`));
}
}
}