Skip to content

Commit

Permalink
Support flags for astro add (#8032)
Browse files Browse the repository at this point in the history
* astro add cli pass down arguments to install cmd

* add changeset

* feat: pass common flags down to install command

* Update .changeset/soft-colts-heal.md

---------

Co-authored-by: Elod Tobak <tobakelod@gmail.com>
  • Loading branch information
natemoo-re and Elod-T authored Aug 14, 2023
1 parent d1f7143 commit 3e46634
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
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",
])

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');
return UpdateResult.failure;
}
} else {
Expand Down

0 comments on commit 3e46634

Please sign in to comment.