Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more robustly handle npm version changes #145

Merged
merged 2 commits into from
Jul 18, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@

# Library specific ones
!/.vscode/extensions.json
/test/socket-npm-fixtures/**/node_modules/
31 changes: 23 additions & 8 deletions lib/shadow/npm-injection.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -254,29 +254,44 @@ const ttyServerPromise = chalkPromise.then(async (chalk) => {
const npmEntrypoint = fs.realpathSync(`${process.argv[1]}`)
/**
* @param {string} filepath
* @returns {string}
* @returns {string | null}
*/
function findRoot (filepath) {
if (path.basename(filepath) === 'npm') {
return filepath
}
const parent = path.dirname(filepath)
if (parent === filepath) {
process.exit(127)
return null
}
return findRoot(parent)
}
const npmDir = findRoot(path.dirname(npmEntrypoint))
const arboristLibClassPath = path.join(npmDir, 'node_modules', '@npmcli', 'arborist', 'lib', 'arborist', 'index.js')
if (npmDir === null) {
console.error('Unable to find npm cli install directory, this is potentiall a bug with socket-npm caused by changes to npm cli.')
console.error(`Searched parent directories of ${npmEntrypoint}`)
process.exit(127)
}
let arboristLibClassPath
try {
arboristLibClassPath = path.join(npmDir, 'node_modules', '@npmcli', 'arborist', 'lib', 'arborist', 'index.js')
} catch (e) {
console.error('Unable to integrate with npm cli internals, this is potentially a bug with socket-npm caused by changes to npm cli.')
process.exit(127);
}

const npmVersion = process.env.NPM_VERSION.split('.')
let npmlog

if(npmVersion[0] === '10' && npmVersion[1] >= '6'){
const { log } = require(path.join(npmDir, 'node_modules', 'proc-log', 'lib', 'index.js'))
npmlog = log
} else {
try {
npmlog = require(path.join(npmDir, 'node_modules', 'npmlog', 'lib', 'log.js'))
} catch {
try {
const { log } = require(path.join(npmDir, 'node_modules', 'proc-log', 'lib', 'index.js'))
npmlog = log
} catch {
console.error('Unable to integrate with npm cli logging infrastructure, this is potentially a bug with socket-npm caused by changes to npm cli.')
process.exit(127);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
},

"scripts": {
"check:dependency-check": "dependency-check '*.js' 'lib/shadow/*.cjs' '*.mjs' 'test/**/*.js' --no-dev --ignore-module node:* --ignore-module @cyclonedx/* --ignore-module synp",
"check:dependency-check": "dependency-check '*.js' 'lib/shadow/*.cjs' '*.mjs' 'test/*.js' --no-dev --ignore-module node:* --ignore-module @cyclonedx/* --ignore-module synp",
"check:installed-check": "installed-check -i eslint-plugin-jsdoc",
"check:lint": "eslint --report-unused-disable-directives .",
"check:tsc": "tsc",
Expand Down
Loading
Loading