diff --git a/lib/plugin/npm/npm.js b/lib/plugin/npm/npm.js index c7c5d887..8b40fe21 100644 --- a/lib/plugin/npm/npm.js +++ b/lib/plugin/npm/npm.js @@ -132,34 +132,43 @@ class npm extends Plugin { ); } - isCollaborator() { + async isCollaborator() { const registry = this.getRegistry(); const registryArg = registry ? ` --registry ${registry}` : ''; const name = this.getName(); const { username } = this.getContext(); if (username === undefined) return true; if (username === null) return false; - return this.exec(`npm access ls-collaborators ${name}${registryArg}`, { options }).then( - output => { - try { - const collaborators = JSON.parse(output); - const permissions = collaborators[username]; - return permissions && permissions.includes('write'); - } catch (err) { - this.debug(err); - return false; - } - }, - err => { + + try { + let npmVersion = await this.exec('npm --version', { options }); + + let accessCommand; + if (semver.gt(npmVersion, '9.0.0')) { + accessCommand = 'npm access list collaborators --json'; + } else { + accessCommand = 'npm access ls-collaborators'; + } + + const output = await this.exec(`${accessCommand} ${name}${registryArg}`, { options }); + + try { + const collaborators = JSON.parse(output); + const permissions = collaborators[username]; + return permissions && permissions.includes('write'); + } catch (err) { this.debug(err); - if (/code E400/.test(err)) { - this.log.warn('Ignoring response from unsupported `npm access` command.'); - } else { - this.log.warn(`Unable to verify if user ${username} is a collaborator for ${name}.`); - } - return true; + return false; } - ); + } catch (err) { + this.debug(err); + if (/code E400/.test(err)) { + this.log.warn('Ignoring response from unsupported `npm access` command.'); + } else { + this.log.warn(`Unable to verify if user ${username} is a collaborator for ${name}.`); + } + return true; + } } async getLatestRegistryVersion() {