Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion workspaces/config/lib/definitions/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,13 @@ const definitions = {
flatten,
}),
'node-gyp': new Definition('node-gyp', {
default: require.resolve('node-gyp/bin/node-gyp.js'),
default: (() => {
try {
return require.resolve('node-gyp/bin/node-gyp.js')
} catch {
return ''
}
})(),
defaultDescription: `
The path to the node-gyp bin that ships with npm
`,
Expand Down
36 changes: 36 additions & 0 deletions workspaces/config/test/definitions/definitions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const t = require('tap')
const { resolve } = require('node:path')
const mockGlobals = require('@npmcli/mock-globals')
const Module = require('node:module')

// have to fake the node version, or else it'll only pass on this one
mockGlobals(t, { 'process.version': 'v14.8.0', 'process.env.NODE_ENV': undefined })
Expand Down Expand Up @@ -1020,3 +1021,38 @@ t.test('otp changes auth-type', t => {
t.strictSame(obj, { 'auth-type': 'legacy', otp: 123456 })
t.end()
})

t.test('node-gyp', t => {
t.test('when node-gyp is available', t => {
const defs = mockDefs()
t.type(defs['node-gyp'].default, 'string')
t.ok(defs['node-gyp'].default.length > 0, 'has a default path')
t.ok(defs['node-gyp'].default.includes('node-gyp'), 'path contains node-gyp')
t.end()
})

t.test('when node-gyp is not available', t => {
const origResolve = Module._resolveFilename

Module._resolveFilename = function (request, parent, isMain, options) {
if (request === 'node-gyp/bin/node-gyp.js') {
const err = new Error(`Cannot find module '${request}'`)
err.code = 'MODULE_NOT_FOUND'
throw err
}
return origResolve.call(this, request, parent, isMain, options)
}

try {
const defs = require('../../lib/definitions/definitions.js')
t.equal(defs['node-gyp'].default, '', 'returns empty string when node-gyp not found')
} finally {
// Restore the original resolve
Module._resolveFilename = origResolve
}

t.end()
})

t.end()
})