Support flags for astro add (#8032)

* 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>
This commit is contained in:
Nate Moore 2023-08-14 10:50:49 -05:00 committed by GitHub
parent d1f7143f9c
commit 3e46634fd5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
`astro add` now passes down `--save-prod`, `--save-dev`, `--save-exact`, and `--no-save` flags for installation

View file

@ -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({ async function tryToInstallIntegrations({
integrations, integrations,
cwd, cwd,
@ -647,12 +656,24 @@ async function tryToInstallIntegrations({
}): Promise<UpdateResult> { }): Promise<UpdateResult> {
const installCommand = await getInstallIntegrationsCommand({ integrations, cwd }); 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) { if (installCommand === null) {
return UpdateResult.none; return UpdateResult.none;
} else { } else {
const coloredOutput = `${bold(installCommand.pm)} ${installCommand.command}${[ const coloredOutput = `${bold(installCommand.pm)} ${installCommand.command}${[
'', '',
...installCommand.flags, ...installCommand.flags,
...inheritedFlags
].join(' ')} ${cyan(installCommand.dependencies.join(' '))}`; ].join(' ')} ${cyan(installCommand.dependencies.join(' '))}`;
const message = `\n${boxen(coloredOutput, { const message = `\n${boxen(coloredOutput, {
margin: 0.5, margin: 0.5,
@ -672,14 +693,15 @@ async function tryToInstallIntegrations({
try { try {
await execa( await execa(
installCommand.pm, installCommand.pm,
[installCommand.command, ...installCommand.flags, ...installCommand.dependencies], [installCommand.command, ...installCommand.flags, ...inheritedFlags, ...installCommand.dependencies],
{ cwd } { cwd }
); );
spinner.succeed(); spinner.succeed();
return UpdateResult.updated; return UpdateResult.updated;
} catch (err) { } catch (err) {
debug('add', 'Error installing dependencies', err);
spinner.fail(); spinner.fail();
debug('add', 'Error installing dependencies', err);
console.error('\n', (err as any).stdout, '\n');
return UpdateResult.failure; return UpdateResult.failure;
} }
} else { } else {