Skip to content

fix: make cli aware when run in npx/pnpx context #7315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/commands/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { OptionValues } from 'commander'
import inquirer from 'inquirer'
import isEmpty from 'lodash/isEmpty.js'

import { chalk, exit, log } from '../../utils/command-helpers.js'
import { chalk, exit, log, netlifyCommand } from '../../utils/command-helpers.js'
import getRepoData from '../../utils/get-repo-data.js'
import { ensureNetlifyIgnore } from '../../utils/gitignore.js'
import { configureRepo } from '../../utils/init/config.js'
Expand Down Expand Up @@ -31,8 +31,8 @@ const logExistingAndExit = ({ siteInfo }: { siteInfo: SiteInfo }): never => {
log(`Admin URL: ${chalk.cyan(siteInfo.admin_url)}`)
log()
log(`To disconnect this directory and create a new project (or link to another project ID)`)
log(`1. Run ${chalk.cyanBright.bold('netlify unlink')}`)
log(`2. Then run ${chalk.cyanBright.bold('netlify init')} again`)
log(`1. Run ${chalk.cyanBright.bold(`${netlifyCommand()} unlink`)}`)
log(`2. Then run ${chalk.cyanBright.bold(`${netlifyCommand()} init`)} again`)
return exit()
}

Expand Down Expand Up @@ -78,7 +78,7 @@ const createNewSiteAndExit = async ({
}

log()
log(`To deploy to this project, run ${chalk.cyanBright.bold('netlify deploy')}`)
log(`To deploy to this project, run ${chalk.cyanBright.bold(`${netlifyCommand()} deploy`)}`)

return exit()
}
Expand Down Expand Up @@ -111,7 +111,7 @@ const logGitSetupInstructionsAndExit = (): never => {

7. Initialize your Netlify Site

${chalk.cyanBright.bold('netlify init')}
${chalk.cyanBright.bold(`${netlifyCommand()} init`)}
`)
return exit()
}
Expand Down
20 changes: 12 additions & 8 deletions src/commands/link/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { NetlifyAPI } from '@netlify/api'

import { listSites } from '../../lib/api.js'
import { startSpinner } from '../../lib/spinner.js'
import { chalk, logAndThrowError, exit, log, APIError } from '../../utils/command-helpers.js'
import { chalk, logAndThrowError, exit, log, APIError, netlifyCommand } from '../../utils/command-helpers.js'
import getRepoData from '../../utils/get-repo-data.js'
import { ensureNetlifyIgnore } from '../../utils/gitignore.js'
import { track } from '../../utils/telemetry/index.js'
Expand All @@ -23,7 +23,9 @@ const findSiteByRepoUrl = async (api: NetlifyAPI, repoUrl: string): Promise<Site
if (sites.length === 0) {
spinner.error()
return logAndThrowError(
`You don't have any projects yet. Run ${chalk.cyanBright('netlify sites:create')} to create a project.`,
`You don't have any projects yet. Run ${chalk.cyanBright(
`${netlifyCommand()} sites:create`,
)} to create a project.`,
)
}

Expand Down Expand Up @@ -92,7 +94,7 @@ const linkPrompt = async (command: BaseCommand, options: LinkOptionValues): Prom
}

log()
log(`${chalk.cyanBright('netlify link')} will connect this folder to a project on Netlify`)
log(`${chalk.cyanBright(`${netlifyCommand()} link`)} will connect this folder to a project on Netlify`)
log()
const { linkType } = await inquirer.prompt<{ linkType: string | undefined }>([
{
Expand Down Expand Up @@ -142,8 +144,8 @@ const linkPrompt = async (command: BaseCommand, options: LinkOptionValues): Prom
if (!matchingSites || matchingSites.length === 0) {
return logAndThrowError(`No project names found containing '${searchTerm}'.

Run ${chalk.cyanBright('netlify link')} again to try a new search,
or run ${chalk.cyanBright('netlify sites:create')} to create a project.`)
Run ${chalk.cyanBright(`${netlifyCommand()} link`)} again to try a new search,
or run ${chalk.cyanBright(`npx ${netlifyCommand()} sites:create`)} to create a project.`)
}

if (matchingSites.length > 1) {
Expand Down Expand Up @@ -183,7 +185,9 @@ or run ${chalk.cyanBright('netlify sites:create')} to create a project.`)

if (!sites || sites.length === 0) {
return logAndThrowError(
`You don't have any projects yet. Run ${chalk.cyanBright('netlify sites:create')} to create a project.`,
`You don't have any projects yet. Run ${chalk.cyanBright(
`${netlifyCommand()} sites:create`,
)} to create a project.`,
)
}

Expand Down Expand Up @@ -271,7 +275,7 @@ export const link = async (options: LinkOptionValues, command: BaseCommand) => {
// Site id is incorrect
if (siteId && isEmpty(siteInfo)) {
log(`"${siteId}" was not found in your Netlify account.`)
log(`Please double check your project ID and which account you are logged into via \`netlify status\`.`)
log(`Please double check your project ID and which account you are logged into via \`${netlifyCommand()} status\`.`)
return exit()
}

Expand All @@ -281,7 +285,7 @@ export const link = async (options: LinkOptionValues, command: BaseCommand) => {
log(`Project already linked to "${initialSiteData.name}"`)
log(`Admin url: ${initialSiteData.admin_url}`)
log()
log(`To unlink this project, run: ${chalk.cyanBright('netlify unlink')}`)
log(`To unlink this project, run: ${chalk.cyanBright(`${netlifyCommand()} unlink`)}`)
} else if (options.id) {
try {
// @ts-expect-error FIXME(serhalp): Mismatch between hardcoded `SiteInfo` and new generated Netlify API types.
Expand Down
24 changes: 24 additions & 0 deletions src/utils/command-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,27 @@ export const checkFileForLine = (filename: string, line: string) => {

export const TABTAB_CONFIG_LINE = '[[ -f ~/.config/tabtab/__tabtab.zsh ]] && . ~/.config/tabtab/__tabtab.zsh || true'
export const AUTOLOAD_COMPINIT = 'autoload -U compinit; compinit'

function pkgFromUserAgent(userAgent: string | undefined): string | undefined {
if (!userAgent) return undefined
const pkgSpec = userAgent.split(' ')[0]
const [pkgManagerName] = pkgSpec.split('/')
return pkgManagerName
}

export const netlifyCommand = () => {
const { npm_command, npm_config_user_agent, npm_lifecycle_event } = process.env

// Captures both `npx netlify ...` and `npm exec netlify ...`
if (npm_lifecycle_event === 'npx') {
return `npx netlify`
}

// Captures both `pnpx netlify ...` and `pnpm exec netlify ...`
if (pkgFromUserAgent(npm_config_user_agent) === 'pnpm' && npm_command === 'exec') {
return `pnpx netlify`
}

// Default
return 'netlify'
}
Loading