From 3e46634fd540e5b967d2e5c9abd6235452cee2f2 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Mon, 14 Aug 2023 10:50:49 -0500 Subject: [PATCH] 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 --- .changeset/soft-colts-heal.md | 5 +++++ packages/astro/src/cli/add/index.ts | 26 ++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 .changeset/soft-colts-heal.md diff --git a/.changeset/soft-colts-heal.md b/.changeset/soft-colts-heal.md new file mode 100644 index 000000000000..4ceaf2a9bf72 --- /dev/null +++ b/.changeset/soft-colts-heal.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +`astro add` now passes down `--save-prod`, `--save-dev`, `--save-exact`, and `--no-save` flags for installation diff --git a/packages/astro/src/cli/add/index.ts b/packages/astro/src/cli/add/index.ts index 6ef96561d7cb..1af3e7633cae 100644 --- a/packages/astro/src/cli/add/index.ts +++ b/packages/astro/src/cli/add/index.ts @@ -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([ + "P", "save-prod", + "D", "save-dev", + "E", "save-exact", + "no-save", +]) + async function tryToInstallIntegrations({ integrations, cwd, @@ -647,12 +656,24 @@ async function tryToInstallIntegrations({ }): Promise { 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, @@ -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 {