Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eighty-cloths-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': minor
---

Adds a hint for code agents to use the `--yes` flag to skip prompts when running `astro add`
30 changes: 18 additions & 12 deletions packages/astro/src/cli/add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export async function add(names: string[], { flags }: AddOptions) {
const cwd = inlineConfig.root;
const logger = createLoggerFromFlags(flags);
const integrationNames = names.map((name) => (ALIASES.has(name) ? ALIASES.get(name)! : name));
const integrations = await validateIntegrations(integrationNames, flags);
const integrations = await validateIntegrations(integrationNames, flags, logger);
let installResult = await tryToInstallIntegrations({ integrations, cwd, flags, logger });
const rootPath = resolveRoot(cwd);
const root = pathToFileURL(rootPath);
Expand Down Expand Up @@ -216,7 +216,7 @@ export async function add(names: string[], { flags }: AddOptions) {
`\n ${magenta(`Astro will scaffold ${green('./wrangler.jsonc')}.`)}\n`,
);

if (await askToContinue({ flags })) {
if (await askToContinue({ flags, logger })) {
const data = await getPackageJson();

await fs.writeFile(
Expand All @@ -237,7 +237,7 @@ export async function add(names: string[], { flags }: AddOptions) {
`\n ${magenta(`Astro will scaffold ${green('./public/.assetsignore')}.`)}\n`,
);

if (await askToContinue({ flags })) {
if (await askToContinue({ flags, logger })) {
if (!existsSync(dir)) {
await fs.mkdir(dir);
}
Expand All @@ -256,7 +256,7 @@ export async function add(names: string[], { flags }: AddOptions) {
`\n ${magenta(`Astro will scaffold ${green('./src/styles/global.css')}.`)}\n`,
);

if (await askToContinue({ flags })) {
if (await askToContinue({ flags, logger })) {
if (!existsSync(dir)) {
await fs.mkdir(dir);
}
Expand Down Expand Up @@ -293,7 +293,7 @@ export async function add(names: string[], { flags }: AddOptions) {
)}\n`,
);

if (await askToContinue({ flags })) {
if (await askToContinue({ flags, logger })) {
await fs.mkdir(new URL('./db', root));
await Promise.all([
fs.writeFile(new URL('./db/config.ts', root), STUBS.DB_CONFIG, { encoding: 'utf-8' }),
Expand Down Expand Up @@ -666,7 +666,7 @@ async function updateAstroConfig({
);
}

if (await askToContinue({ flags })) {
if (await askToContinue({ flags, logger })) {
await fs.writeFile(fileURLToPath(configURL), output, { encoding: 'utf-8' });
logger.debug('add', `Updated astro config`);
return UpdateResult.updated;
Expand Down Expand Up @@ -770,7 +770,7 @@ async function tryToInstallIntegrations({
)}\n${message}`,
);

if (await askToContinue({ flags })) {
if (await askToContinue({ flags, logger })) {
const spinner = yoctoSpinner({ text: 'Installing dependencies...' }).start();
try {
await exec(installCommand.command, [...installCommand.args, ...installSpecifiers], {
Expand All @@ -797,6 +797,7 @@ async function tryToInstallIntegrations({
async function validateIntegrations(
integrations: string[],
flags: yargsParser.Arguments,
logger: Logger,
): Promise<IntegrationInfo[]> {
const spinner = yoctoSpinner({ text: 'Resolving packages...' }).start();
try {
Expand All @@ -819,7 +820,7 @@ async function validateIntegrations(
spinner.warning(yellow(firstPartyPkgCheck.message));
}
spinner.warning(yellow(`${bold(integration)} is not an official Astro package.`));
if (!(await askToContinue({ flags }))) {
if (!(await askToContinue({ flags, logger }))) {
throw new Error(
`No problem! Find our official integrations at ${cyan(
'https://astro.build/integrations',
Expand Down Expand Up @@ -987,7 +988,7 @@ async function updateTSConfig(
);
}

if (await askToContinue({ flags })) {
if (await askToContinue({ flags, logger })) {
await fs.writeFile(inputConfig.tsconfigFile, output, {
encoding: 'utf-8',
});
Expand All @@ -1014,9 +1015,14 @@ function parseIntegrationName(spec: string) {
return { scope, name, tag };
}

async function askToContinue({ flags }: { flags: Flags }): Promise<boolean> {
if (flags.yes || flags.y) return true;
let hasHintedAboutYesFlag = false;

async function askToContinue({ flags, logger }: { flags: Flags, logger: Logger }): Promise<boolean> {
if (flags.yes || flags.y) return true;
if (!hasHintedAboutYesFlag) {
hasHintedAboutYesFlag = true;
logger.info('SKIP_FORMAT', dim(' To run this command without prompts, pass the --yes flag\n'));
}
const response = await prompts({
type: 'confirm',
name: 'askToContinue',
Expand Down Expand Up @@ -1078,7 +1084,7 @@ async function setupIntegrationConfig(opts: {
'SKIP_FORMAT',
`\n ${magenta(`Astro will generate a minimal ${bold(opts.defaultConfigFile)} file.`)}\n`,
);
if (await askToContinue({ flags: opts.flags })) {
if (await askToContinue({ flags: opts.flags, logger })) {
await fs.writeFile(
fileURLToPath(new URL(opts.defaultConfigFile, opts.root)),
opts.defaultConfigContent,
Expand Down
Loading