Skip to content

Commit d8cd8b0

Browse files
committed
Fix Windows test fails
1 parent ceeed95 commit d8cd8b0

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/shadow/npm-paths.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { existsSync } from 'node:fs'
2+
import Module from 'node:module'
13
import path from 'node:path'
24
import process from 'node:process'
35

@@ -81,18 +83,27 @@ export function getNpmPath() {
8183
return _npmPath
8284
}
8385

84-
let _npmNmPath: string | undefined
85-
export function getNpmNodeModulesPath() {
86-
if (_npmNmPath === undefined) {
87-
_npmNmPath = path.join(getNpmPath(), NODE_MODULES)
86+
let _npmRequire: NodeJS.Require | undefined
87+
export function getNpmRequire(): NodeJS.Require {
88+
if (_npmRequire === undefined) {
89+
const npmPath = getNpmPath()
90+
const npmNmPath = path.join(npmPath, NODE_MODULES, NPM)
91+
_npmRequire = Module.createRequire(
92+
path.join(existsSync(npmNmPath) ? npmNmPath : npmPath, '<dummy-basename>')
93+
)
8894
}
89-
return _npmNmPath
95+
return _npmRequire
9096
}
9197

9298
let _arboristPkgPath: string | undefined
9399
export function getArboristPackagePath() {
94100
if (_arboristPkgPath === undefined) {
95-
_arboristPkgPath = path.join(getNpmNodeModulesPath(), '@npmcli/arborist')
101+
const pkgName = '@npmcli/arborist'
102+
const mainPath = getNpmRequire().resolve(pkgName)
103+
_arboristPkgPath = mainPath.slice(
104+
0,
105+
mainPath.indexOf(pkgName) + pkgName.length
106+
)
96107
}
97108
return _arboristPkgPath
98109
}

src/shadow/proc-log.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import path from 'node:path'
2-
3-
import { getNpmNodeModulesPath } from './npm-paths'
1+
import { getNpmRequire } from './npm-paths'
42
import constants from '../constants'
53

64
const { UNDEFINED_TOKEN } = constants
@@ -17,6 +15,7 @@ type RequireTransformer<T extends keyof RequireKnownModules> = (
1715
) => RequireKnownModules[T]
1816

1917
function tryRequire<T extends keyof RequireKnownModules>(
18+
req: NodeJS.Require,
2019
...ids: (T | [T, RequireTransformer<T>])[]
2120
): RequireKnownModules[T] | undefined {
2221
for (const data of ids) {
@@ -32,7 +31,7 @@ function tryRequire<T extends keyof RequireKnownModules>(
3231
try {
3332
// Check that the transformed value isn't `undefined` because older
3433
// versions of packages like 'proc-log' may not export a `log` method.
35-
const exported = transformer(require(id))
34+
const exported = transformer(req(id))
3635
if (exported !== undefined) {
3736
return exported
3837
}
@@ -49,15 +48,15 @@ export type Logger =
4948
let _log: Logger | {} | undefined = UNDEFINED_TOKEN
5049
export function getLogger(): Logger {
5150
if (_log === UNDEFINED_TOKEN) {
52-
const npmNmPath = getNpmNodeModulesPath()
5351
_log = tryRequire(
52+
getNpmRequire(),
5453
[
55-
<'proc-log'>path.join(npmNmPath, 'proc-log/lib/index.js'),
54+
<'proc-log'>'proc-log/lib/index.js',
5655
// The proc-log DefinitelyTyped definition is incorrect. The type definition
5756
// is really that of its export log.
5857
mod => <RequireKnownModules['proc-log']>(mod as any).log
5958
],
60-
<'npmlog'>path.join(npmNmPath, 'npmlog/lib/log.js')
59+
<'npmlog'>'npmlog/lib/log.js'
6160
)
6261
}
6362
return <Logger | undefined>_log

0 commit comments

Comments
 (0)