Skip to content

Commit

Permalink
debug
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Feb 19, 2025
1 parent 7e3c8c4 commit 28bf476
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
25 changes: 21 additions & 4 deletions src/shadow/npm-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import Module from 'node:module'
import path from 'node:path'
import process from 'node:process'

import { globSync } from 'tinyglobby'

import constants from '../constants'
import { findBinPathDetailsSync, findNpmPathSync } from '../utils/path-resolve'

Expand Down Expand Up @@ -85,17 +87,32 @@ export function getNpmPath() {
let _npmRequire: NodeJS.Require | undefined
export function getNpmRequire(): NodeJS.Require {
if (_npmRequire === undefined) {
_npmRequire = Module.createRequire(path.join(getNpmPath(), '<dummy-basename>'))
_npmRequire = Module.createRequire(
path.join(getNpmPath(), '<dummy-basename>')
)
}
return _npmRequire
}

let _arboristPkgPath: string | undefined
export function getArboristPackagePath() {
if (_arboristPkgPath === undefined) {
const pkgName = '@npmcli/arborist'
const mainPath = getNpmRequire().resolve(pkgName)
_arboristPkgPath = mainPath.slice(0, mainPath.indexOf(pkgName) + pkgName.length)
try {
const pkgName = '@npmcli/arborist'
const mainPath = getNpmRequire().resolve(pkgName)
_arboristPkgPath = mainPath.slice(
0,
mainPath.indexOf(pkgName) + pkgName.length
)
} catch {
console.error(getNpmPath())
console.error(
JSON.stringify(
globSync(['node_modules/**'], { cwd: getNpmPath() })
, null, 2)
)
throw new Error()
}
}
return _arboristPkgPath
}
Expand Down
22 changes: 6 additions & 16 deletions src/utils/path-resolve.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync, promises as fs, realpathSync, statSync } from 'node:fs'
import { existsSync, promises as fs, realpathSync } from 'node:fs'
import path from 'node:path'
import process from 'node:process'

Expand All @@ -19,7 +19,7 @@ type GlobWithGitIgnoreOptions = GlobOptions & {
socketConfig?: SocketYml | undefined
}

const { NODE_MODULES, NPM, shadowBinPath } = constants
const { NPM, shadowBinPath } = constants

async function filterGlobResultToSupportedFiles(
entries: string[],
Expand Down Expand Up @@ -201,7 +201,6 @@ export function findBinPathDetailsSync(binName: string): {
export function findNpmPathSync(npmBinPath: string): string | undefined {
let thePath = npmBinPath
while (true) {
const nmPath = path.join(thePath, NODE_MODULES)
if (
// npm bin paths may look like:
// /usr/local/share/npm/bin/npm
Expand All @@ -210,20 +209,11 @@ export function findNpmPathSync(npmBinPath: string): string | undefined {
// OR
// C:\Program Files\nodejs\npm.cmd
//
// In all cases the npm path contains a node_modules folder:
// /usr/local/share/npm/bin/npm/node_modules
// C:\Program Files\nodejs\node_modules
//
// Use existsSync here because statsSync, even with { throwIfNoEntry: false },
// will throw an ENOTDIR error for paths like ./a-file-that-exists/a-directory-that-does-not.
// See https://github.com/nodejs/node/issues/56993.
existsSync(nmPath) &&
statSync(nmPath, { throwIfNoEntry: false })?.isDirectory() &&
// Optimistically look for the default location.
(path.basename(thePath) === NPM ||
// Chocolatey installs npm bins in the same directory as node bins.
// Lazily access constants.WIN32.
(constants.WIN32 && existsSync(path.join(thePath, `${NPM}.cmd`))))
path.basename(thePath) === NPM ||
// Chocolatey installs npm bins in the same directory as node bins.
// Lazily access constants.WIN32.
(constants.WIN32 && existsSync(path.join(thePath, `${NPM}.cmd`)))
) {
return thePath
}
Expand Down

0 comments on commit 28bf476

Please sign in to comment.