2020-10-02 17:52:19 -04:00
|
|
|
const npmFetch = require('npm-registry-fetch')
|
2023-11-03 07:03:57 -07:00
|
|
|
const { getAuth } = npmFetch
|
2024-04-30 23:53:22 -07:00
|
|
|
const { log } = require('proc-log')
|
|
|
|
const BaseCommand = require('../base-cmd.js')
|
2015-02-20 09:03:34 -08:00
|
|
|
|
2021-03-11 17:54:23 -05:00
|
|
|
class Logout extends BaseCommand {
|
2021-11-18 20:58:02 +00:00
|
|
|
static description = 'Log out of the registry'
|
|
|
|
static name = 'logout'
|
|
|
|
static params = [
|
|
|
|
'registry',
|
|
|
|
'scope',
|
|
|
|
]
|
2020-10-02 17:52:19 -04:00
|
|
|
|
2024-04-30 23:53:22 -07:00
|
|
|
async exec () {
|
2021-03-23 14:58:11 -04:00
|
|
|
const registry = this.npm.config.get('registry')
|
|
|
|
const scope = this.npm.config.get('scope')
|
2021-03-04 17:40:28 -05:00
|
|
|
const regRef = scope ? `${scope}:registry` : 'registry'
|
2021-03-23 14:58:11 -04:00
|
|
|
const reg = this.npm.config.get(regRef) || registry
|
2021-03-04 17:40:28 -05:00
|
|
|
|
|
|
|
const auth = getAuth(reg, this.npm.flatOptions)
|
|
|
|
|
2023-11-03 07:03:57 -07:00
|
|
|
const level = this.npm.config.find(`${auth.regKey}:${auth.authKey}`)
|
|
|
|
|
|
|
|
// find the config level and only delete from there
|
2021-03-04 17:40:28 -05:00
|
|
|
if (auth.token) {
|
|
|
|
log.verbose('logout', `clearing token for ${reg}`)
|
|
|
|
await npmFetch(`/-/user/token/${encodeURIComponent(auth.token)}`, {
|
|
|
|
...this.npm.flatOptions,
|
2023-11-03 07:03:57 -07:00
|
|
|
registry: reg,
|
2021-03-04 17:40:28 -05:00
|
|
|
method: 'DELETE',
|
|
|
|
ignoreBody: true,
|
|
|
|
})
|
2021-11-18 20:58:02 +00:00
|
|
|
} else if (auth.isBasicAuth) {
|
2021-03-04 17:40:28 -05:00
|
|
|
log.verbose('logout', `clearing user credentials for ${reg}`)
|
2021-11-18 20:58:02 +00:00
|
|
|
} else {
|
2021-03-04 17:40:28 -05:00
|
|
|
const msg = `not logged in to ${reg}, so can't log out!`
|
|
|
|
throw Object.assign(new Error(msg), { code: 'ENEEDAUTH' })
|
|
|
|
}
|
|
|
|
|
2021-11-18 20:58:02 +00:00
|
|
|
if (scope) {
|
2023-11-03 07:03:57 -07:00
|
|
|
this.npm.config.delete(regRef, level)
|
2021-11-18 20:58:02 +00:00
|
|
|
}
|
2021-03-04 17:40:28 -05:00
|
|
|
|
2023-11-03 07:03:57 -07:00
|
|
|
this.npm.config.clearCredentialsByURI(reg, level)
|
2021-03-04 17:40:28 -05:00
|
|
|
|
2023-11-03 07:03:57 -07:00
|
|
|
await this.npm.config.save(level)
|
2021-03-04 17:40:28 -05:00
|
|
|
}
|
2015-02-20 09:03:34 -08:00
|
|
|
}
|
2024-04-30 23:53:22 -07:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
module.exports = Logout
|