Skip to content

Commit

Permalink
Merge pull request #5112 from Shopify/12-16-add_try_catch_to_currentp…
Browse files Browse the repository at this point in the history
…rocessisglobal_function

Fix global CLI detection error handling
  • Loading branch information
isaacroldan authored Dec 18, 2024
2 parents a15cffc + b711a00 commit 4e3ffcc
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions packages/cli-kit/src/public/node/is-global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,28 @@ let _isGlobal: boolean | undefined
*/
export function currentProcessIsGlobal(argv = process.argv): boolean {
// If we are running tests, we need to disable the cache
if (_isGlobal !== undefined && !isUnitTest()) return _isGlobal
try {
if (_isGlobal !== undefined && !isUnitTest()) return _isGlobal

// Path where the current project is (app/hydrogen)
const path = sniffForPath() ?? cwd()
// Path where the current project is (app/hydrogen)
const path = sniffForPath() ?? cwd()

// Closest parent directory to contain a package.json file or node_modules directory
// https://docs.npmjs.com/cli/v8/commands/npm-prefix#description
const npmPrefix = execaSync('npm', ['prefix'], {cwd: path}).stdout.trim()
// Closest parent directory to contain a package.json file or node_modules directory
// https://docs.npmjs.com/cli/v8/commands/npm-prefix#description
const npmPrefix = execaSync('npm', ['prefix'], {cwd: path}).stdout.trim()

// From node docs: "The second element [of the array] will be the path to the JavaScript file being executed"
const binDir = argv[1] ?? ''
// From node docs: "The second element [of the array] will be the path to the JavaScript file being executed"
const binDir = argv[1] ?? ''

// If binDir starts with npmPrefix, then we are running a local CLI
const isLocal = binDir.startsWith(npmPrefix.trim())
// If binDir starts with npmPrefix, then we are running a local CLI
const isLocal = binDir.startsWith(npmPrefix.trim())

_isGlobal = !isLocal
return _isGlobal
_isGlobal = !isLocal
return _isGlobal
// eslint-disable-next-line no-catch-all/no-catch-all
} catch (error) {
return false
}
}

/**
Expand Down

0 comments on commit 4e3ffcc

Please sign in to comment.