From 04b109cefdc4a4f35e4909234af8a513ffa2ee76 Mon Sep 17 00:00:00 2001 From: Pkmmte Xeleon Date: Sat, 14 Jan 2023 20:39:24 -0800 Subject: [PATCH] fix(cli): custom command options should be delegated when using npm or pnpm We need to make sure to pass `--` before delegating arguments to `npm exec` or `pnpm exec` otherwise they'll be treated as arguments for the package manager itself. Yarn smartly delegates and doesn't need this. --- .changeset/fuzzy-experts-allow.md | 5 ++++ packages/pilot/src/cli/commands/dev/index.ts | 29 +++++++++++++++----- 2 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 .changeset/fuzzy-experts-allow.md diff --git a/.changeset/fuzzy-experts-allow.md b/.changeset/fuzzy-experts-allow.md new file mode 100644 index 0000000..de08645 --- /dev/null +++ b/.changeset/fuzzy-experts-allow.md @@ -0,0 +1,5 @@ +--- +'@waveplay/pilot': patch +--- + +fix(cli): custom command options should be delegated when using npm or pnpm diff --git a/packages/pilot/src/cli/commands/dev/index.ts b/packages/pilot/src/cli/commands/dev/index.ts index 5c56f05..196196f 100644 --- a/packages/pilot/src/cli/commands/dev/index.ts +++ b/packages/pilot/src/cli/commands/dev/index.ts @@ -46,6 +46,9 @@ export async function action(options: OptionValues) { const pkgManager = getPkgManager() logger.debug(`[PilotJS] Using package manager: ${pkgManager}`) + // Whether or not this package manager requires -- to pass arguments + const isNpmBased = pkgManager === 'npm' || pkgManager === 'pnpm' + // Check if ngrok is installed let useTunnel = options.tunnel let localtunnel @@ -97,19 +100,23 @@ export async function action(options: OptionValues) { // Start Next.js process const nextArgs = config.commands?.devWeb?.split(' ') ?? ['next', 'dev'] - if (pkgManager === 'npm' || pkgManager === 'pnpm') { - nextArgs.splice(0, 0, 'exec') - } if (!nextArgs.includes('-p') && !nextArgs.includes('--port')) { - if (pkgManager === 'npm' || pkgManager === 'pnpm') { - nextArgs.push('--') - } nextArgs.push('-p', port.toString()) } if (options.hostname) { nextArgs.push('-H', options.hostname) } + // Check if args include option flags + // Inserts -- before options to make sure they're being passed correctly + if (isNpmBased) { + nextArgs.splice(0, 0, 'exec') + const optionsIndex = nextArgs.findIndex((arg) => arg.startsWith('-')) + if (optionsIndex !== -1) { + nextArgs.splice(optionsIndex, 0, '--') + } + } + logger.debug(`[PilotJS] Executing: ${cmd(pkgManager)} ${nextArgs.join(' ')}`) spawn(cmd(pkgManager), nextArgs, { stdio: 'inherit' @@ -133,8 +140,16 @@ export async function action(options: OptionValues) { // Start native process const nativeArgs = config.commands?.devNative?.split(' ') ?? ['expo', 'start'] - if (pkgManager === 'npm' || pkgManager === 'pnpm') { + + // Check if args include option flags + // Inserts -- before options to make sure they're being passed correctly + if (isNpmBased) { nativeArgs.splice(0, 0, 'exec') + + const optionsIndex = nativeArgs.findIndex((arg) => arg.startsWith('-')) + if (optionsIndex !== -1) { + nativeArgs.splice(optionsIndex, 0, '--') + } } logger.debug(`[PilotJS] Executing: ${cmd(pkgManager)} ${nativeArgs.join(' ')}`)