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 2739412
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
36 changes: 31 additions & 5 deletions src/shadow/npm-paths.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { existsSync } from 'node:fs'
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'

const { NPM, NPX, SOCKET_CLI_ISSUES_URL } = constants
const { NODE_MODULES, NPM, NPX, SOCKET_CLI_ISSUES_URL } = constants

function exitWithBinPathError(binName: string): never {
console.error(
Expand Down Expand Up @@ -85,17 +88,40 @@ export function getNpmPath() {
let _npmRequire: NodeJS.Require | undefined
export function getNpmRequire(): NodeJS.Require {
if (_npmRequire === undefined) {
_npmRequire = Module.createRequire(path.join(getNpmPath(), '<dummy-basename>'))
const npmPath = getNpmPath()
const npmNmPath = path.join(npmPath, NODE_MODULES, NPM)
_npmRequire = Module.createRequire(
path.join(existsSync(npmNmPath) ? npmNmPath : npmPath, '<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 {
const npmPath = getNpmPath()
const npmNmPath = path.join(npmPath, NODE_MODULES, NPM)

console.error(npmPath)
console.error(path.join(existsSync(npmNmPath) ? npmNmPath : npmPath, '<dummy-basename>'))
console.error(
JSON.stringify(
globSync(['node_modules/**'], { cwd: getNpmPath() }),
null,
2
)
)
throw new Error()
}
}
return _arboristPkgPath
}
Expand Down
10 changes: 5 additions & 5 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 /*, statSync*/ } 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 { /*NODE_MODULES,*/ NPM, shadowBinPath } = constants

async function filterGlobResultToSupportedFiles(
entries: string[],
Expand Down Expand Up @@ -201,7 +201,7 @@ export function findBinPathDetailsSync(binName: string): {
export function findNpmPathSync(npmBinPath: string): string | undefined {
let thePath = npmBinPath
while (true) {
const nmPath = path.join(thePath, NODE_MODULES)
//const nmPath = path.join(thePath, NODE_MODULES)
if (
// npm bin paths may look like:
// /usr/local/share/npm/bin/npm
Expand All @@ -217,8 +217,8 @@ export function findNpmPathSync(npmBinPath: string): string | undefined {
// 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() &&
// 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.
Expand Down

0 comments on commit 2739412

Please sign in to comment.