Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support flags for astro add #8032

Merged
merged 5 commits into from
Aug 14, 2023
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/soft-colts-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

`astro add` now passes down `--save-prod`, `--save-dev`, `--save-exact`, and `--no-save` flags for installation
26 changes: 24 additions & 2 deletions packages/astro/src/cli/add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,15 @@ async function getInstallIntegrationsCommand({
}
}

// Allow forwarding of standard `npm install` flags
// See https://docs.npmjs.com/cli/v8/commands/npm-install#description
const INHERITED_FLAGS = new Set<string>([
"P", "save-prod",
"D", "save-dev",
"E", "save-exact",
"no-save",
])
Comment on lines +637 to +644
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scoped these commands down to a hard-coded list!


async function tryToInstallIntegrations({
integrations,
cwd,
Expand All @@ -647,12 +656,24 @@ async function tryToInstallIntegrations({
}): Promise<UpdateResult> {
const installCommand = await getInstallIntegrationsCommand({ integrations, cwd });

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[];

if (installCommand === null) {
return UpdateResult.none;
} else {
const coloredOutput = `${bold(installCommand.pm)} ${installCommand.command}${[
'',
...installCommand.flags,
...inheritedFlags
].join(' ')} ${cyan(installCommand.dependencies.join(' '))}`;
const message = `\n${boxen(coloredOutput, {
margin: 0.5,
Expand All @@ -672,14 +693,15 @@ async function tryToInstallIntegrations({
try {
await execa(
installCommand.pm,
[installCommand.command, ...installCommand.flags, ...installCommand.dependencies],
[installCommand.command, ...installCommand.flags, ...inheritedFlags, ...installCommand.dependencies],
{ cwd }
);
spinner.succeed();
return UpdateResult.updated;
} catch (err) {
debug('add', 'Error installing dependencies', err);
spinner.fail();
debug('add', 'Error installing dependencies', err);
console.error('\n', (err as any).stdout, '\n');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an error log here since the installation step can fail silently.

return UpdateResult.failure;
}
} else {
Expand Down
Loading