Skip to content

Commit 4e3ffcc

Browse files
authored
Merge pull request #5112 from Shopify/12-16-add_try_catch_to_currentprocessisglobal_function
Fix global CLI detection error handling
2 parents a15cffc + b711a00 commit 4e3ffcc

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

packages/cli-kit/src/public/node/is-global.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,28 @@ let _isGlobal: boolean | undefined
1616
*/
1717
export function currentProcessIsGlobal(argv = process.argv): boolean {
1818
// If we are running tests, we need to disable the cache
19-
if (_isGlobal !== undefined && !isUnitTest()) return _isGlobal
19+
try {
20+
if (_isGlobal !== undefined && !isUnitTest()) return _isGlobal
2021

21-
// Path where the current project is (app/hydrogen)
22-
const path = sniffForPath() ?? cwd()
22+
// Path where the current project is (app/hydrogen)
23+
const path = sniffForPath() ?? cwd()
2324

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

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

31-
// If binDir starts with npmPrefix, then we are running a local CLI
32-
const isLocal = binDir.startsWith(npmPrefix.trim())
32+
// If binDir starts with npmPrefix, then we are running a local CLI
33+
const isLocal = binDir.startsWith(npmPrefix.trim())
3334

34-
_isGlobal = !isLocal
35-
return _isGlobal
35+
_isGlobal = !isLocal
36+
return _isGlobal
37+
// eslint-disable-next-line no-catch-all/no-catch-all
38+
} catch (error) {
39+
return false
40+
}
3641
}
3742

3843
/**

0 commit comments

Comments
 (0)