11cb0ef41Sopenharmony_ciconst npmFetch = require('npm-registry-fetch') 21cb0ef41Sopenharmony_ciconst { getAuth } = npmFetch 31cb0ef41Sopenharmony_ciconst log = require('../utils/log-shim') 41cb0ef41Sopenharmony_ciconst BaseCommand = require('../base-command.js') 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciclass Logout extends BaseCommand { 71cb0ef41Sopenharmony_ci static description = 'Log out of the registry' 81cb0ef41Sopenharmony_ci static name = 'logout' 91cb0ef41Sopenharmony_ci static params = [ 101cb0ef41Sopenharmony_ci 'registry', 111cb0ef41Sopenharmony_ci 'scope', 121cb0ef41Sopenharmony_ci ] 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ci async exec (args) { 151cb0ef41Sopenharmony_ci const registry = this.npm.config.get('registry') 161cb0ef41Sopenharmony_ci const scope = this.npm.config.get('scope') 171cb0ef41Sopenharmony_ci const regRef = scope ? `${scope}:registry` : 'registry' 181cb0ef41Sopenharmony_ci const reg = this.npm.config.get(regRef) || registry 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci const auth = getAuth(reg, this.npm.flatOptions) 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci const level = this.npm.config.find(`${auth.regKey}:${auth.authKey}`) 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci // find the config level and only delete from there 251cb0ef41Sopenharmony_ci if (auth.token) { 261cb0ef41Sopenharmony_ci log.verbose('logout', `clearing token for ${reg}`) 271cb0ef41Sopenharmony_ci await npmFetch(`/-/user/token/${encodeURIComponent(auth.token)}`, { 281cb0ef41Sopenharmony_ci ...this.npm.flatOptions, 291cb0ef41Sopenharmony_ci registry: reg, 301cb0ef41Sopenharmony_ci method: 'DELETE', 311cb0ef41Sopenharmony_ci ignoreBody: true, 321cb0ef41Sopenharmony_ci }) 331cb0ef41Sopenharmony_ci } else if (auth.isBasicAuth) { 341cb0ef41Sopenharmony_ci log.verbose('logout', `clearing user credentials for ${reg}`) 351cb0ef41Sopenharmony_ci } else { 361cb0ef41Sopenharmony_ci const msg = `not logged in to ${reg}, so can't log out!` 371cb0ef41Sopenharmony_ci throw Object.assign(new Error(msg), { code: 'ENEEDAUTH' }) 381cb0ef41Sopenharmony_ci } 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ci if (scope) { 411cb0ef41Sopenharmony_ci this.npm.config.delete(regRef, level) 421cb0ef41Sopenharmony_ci } 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci this.npm.config.clearCredentialsByURI(reg, level) 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci await this.npm.config.save(level) 471cb0ef41Sopenharmony_ci } 481cb0ef41Sopenharmony_ci} 491cb0ef41Sopenharmony_cimodule.exports = Logout 50