Skip to content

Commit

Permalink
fix(cli): custom command options should be delegated when using npm o…
Browse files Browse the repository at this point in the history
…r 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.
  • Loading branch information
Pkmmte committed Jan 15, 2023
1 parent 7755630 commit 04b109c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-experts-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@waveplay/pilot': patch
---

fix(cli): custom command options should be delegated when using npm or pnpm
29 changes: 22 additions & 7 deletions packages/pilot/src/cli/commands/dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand All @@ -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(' ')}`)
Expand Down

0 comments on commit 04b109c

Please sign in to comment.