|
| 1 | +import { existsSync } from 'node:fs' |
1 | 2 | import Module from 'node:module' |
2 | 3 | import path from 'node:path' |
3 | 4 | import process from 'node:process' |
4 | 5 |
|
| 6 | +import { globSync } from 'tinyglobby' |
| 7 | + |
5 | 8 | import constants from '../constants' |
6 | 9 | import { findBinPathDetailsSync, findNpmPathSync } from '../utils/path-resolve' |
7 | 10 |
|
8 | | -const { NPM, NPX, SOCKET_CLI_ISSUES_URL } = constants |
| 11 | +const { NODE_MODULES, NPM, NPX, SOCKET_CLI_ISSUES_URL } = constants |
9 | 12 |
|
10 | 13 | function exitWithBinPathError(binName: string): never { |
11 | 14 | console.error( |
@@ -85,17 +88,39 @@ export function getNpmPath() { |
85 | 88 | let _npmRequire: NodeJS.Require | undefined |
86 | 89 | export function getNpmRequire(): NodeJS.Require { |
87 | 90 | if (_npmRequire === undefined) { |
88 | | - _npmRequire = Module.createRequire(path.join(getNpmPath(), '<dummy-basename>')) |
| 91 | + const npmPath = getNpmPath() |
| 92 | + _npmRequire = Module.createRequire( |
| 93 | + path.join(npmPath, '<dummy-basename>') |
| 94 | + ) |
89 | 95 | } |
90 | 96 | return _npmRequire |
91 | 97 | } |
92 | 98 |
|
93 | 99 | let _arboristPkgPath: string | undefined |
94 | 100 | export function getArboristPackagePath() { |
95 | 101 | if (_arboristPkgPath === undefined) { |
96 | | - const pkgName = '@npmcli/arborist' |
97 | | - const mainPath = getNpmRequire().resolve(pkgName) |
98 | | - _arboristPkgPath = mainPath.slice(0, mainPath.indexOf(pkgName) + pkgName.length) |
| 102 | + try { |
| 103 | + const pkgName = '@npmcli/arborist' |
| 104 | + const mainPath = getNpmRequire().resolve(pkgName) |
| 105 | + _arboristPkgPath = mainPath.slice( |
| 106 | + 0, |
| 107 | + mainPath.indexOf(pkgName) + pkgName.length |
| 108 | + ) |
| 109 | + } catch { |
| 110 | + const npmPath = getNpmPath() |
| 111 | + const npmNmPath = path.join(npmPath, NODE_MODULES, NPM) |
| 112 | + |
| 113 | + console.error(npmPath) |
| 114 | + console.error(path.join(existsSync(npmNmPath) ? npmNmPath : npmPath, '<dummy-basename>')) |
| 115 | + console.error( |
| 116 | + JSON.stringify( |
| 117 | + globSync(['node_modules/**'], { cwd: getNpmPath() }), |
| 118 | + null, |
| 119 | + 2 |
| 120 | + ) |
| 121 | + ) |
| 122 | + throw new Error() |
| 123 | + } |
99 | 124 | } |
100 | 125 | return _arboristPkgPath |
101 | 126 | } |
|
0 commit comments