Home / Function/ tryToInstallIntegrations() — astro Function Reference

tryToInstallIntegrations() — astro Function Reference

Architecture documentation for the tryToInstallIntegrations() function in index.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  d8c50440_84a7_3639_a0ff_bb413e95ec8d["tryToInstallIntegrations()"]
  9151bb3d_ee1e_da42_752a_45a9db1dd918["index.ts"]
  d8c50440_84a7_3639_a0ff_bb413e95ec8d -->|defined in| 9151bb3d_ee1e_da42_752a_45a9db1dd918
  e251add5_ea46_2280_c246_1b5a023acc3b["add()"]
  e251add5_ea46_2280_c246_1b5a023acc3b -->|calls| d8c50440_84a7_3639_a0ff_bb413e95ec8d
  a25a4899_7ef7_478a_55fd_a5648adaa725["convertIntegrationsToInstallSpecifiers()"]
  d8c50440_84a7_3639_a0ff_bb413e95ec8d -->|calls| a25a4899_7ef7_478a_55fd_a5648adaa725
  2ee262d1_8614_9c2d_75fd_5bb9c01ac63c["askToContinue()"]
  d8c50440_84a7_3639_a0ff_bb413e95ec8d -->|calls| 2ee262d1_8614_9c2d_75fd_5bb9c01ac63c
  style d8c50440_84a7_3639_a0ff_bb413e95ec8d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/cli/add/index.ts lines 822–898

async function tryToInstallIntegrations({
	integrations,
	cwd,
	flags,
	logger,
}: {
	integrations: IntegrationInfo[];
	cwd?: string;
	flags: Flags;
	logger: Logger;
}): Promise<UpdateResult> {
	const packageManager = await detect({
		cwd,
		// Include the `install-metadata` strategy to have the package manager that's
		// used for installation take precedence
		strategies: ['install-metadata', 'lockfile', 'packageManager-field'],
	});
	logger.debug('add', `package manager: "${packageManager?.name}"`);
	if (!packageManager) return UpdateResult.none;

	const inheritedFlags = Object.entries(flags)
		.map(([flag]) => {
			if (flag == '_') return;
			if (INHERITED_FLAGS.has(flag)) {
				if (flag.length === 1) return `-${flag}`;
				return `--${flag}`;
			}
		})
		.filter(Boolean)
		.flat() as string[];

	const installCommand = resolveCommand(packageManager?.agent ?? 'npm', 'add', inheritedFlags);
	if (!installCommand) return UpdateResult.none;

	const installSpecifiers = await convertIntegrationsToInstallSpecifiers(integrations).then(
		(specifiers) =>
			installCommand.command === 'deno'
				? specifiers.map((specifier) => `npm:${specifier}`) // Deno requires npm prefix to install packages
				: specifiers,
	);

	const coloredOutput = `${bold(installCommand.command)} ${installCommand.args.join(' ')} ${cyan(installSpecifiers.join(' '))}`;
	const message = `\n${boxen(coloredOutput, {
		margin: 0.5,
		padding: 0.5,
		borderStyle: 'round',
	})}\n`;
	logger.info(
		'SKIP_FORMAT',
		`\n  ${magenta('Astro will run the following command:')}\n  ${dim(
			'If you skip this step, you can always run it yourself later',
		)}\n${message}`,
	);

	if (await askToContinue({ flags, logger })) {
		const spinner = yoctoSpinner({ text: 'Installing dependencies...' }).start();
		try {
			await exec(installCommand.command, [...installCommand.args, ...installSpecifiers], {
				nodeOptions: {
					cwd,
					// reset NODE_ENV to ensure install command run in dev mode
					env: { NODE_ENV: undefined },
				},
			});
			spinner.success();
			return UpdateResult.updated;
		} catch (err: any) {
			spinner.error();
			logger.debug('add', 'Error installing dependencies', err);
			// NOTE: `err.stdout` can be an empty string, so log the full error instead for a more helpful log
			console.error('\n', err.stdout || err.message, '\n');
			return UpdateResult.failure;
		}
	} else {
		return UpdateResult.cancelled;
	}
}

Domain

Subdomains

Called By

Frequently Asked Questions

What does tryToInstallIntegrations() do?
tryToInstallIntegrations() is a function in the astro codebase, defined in packages/astro/src/cli/add/index.ts.
Where is tryToInstallIntegrations() defined?
tryToInstallIntegrations() is defined in packages/astro/src/cli/add/index.ts at line 822.
What does tryToInstallIntegrations() call?
tryToInstallIntegrations() calls 2 function(s): askToContinue, convertIntegrationsToInstallSpecifiers.
What calls tryToInstallIntegrations()?
tryToInstallIntegrations() is called by 1 function(s): add.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free