2024-05-30 04:21:05 -07:00
|
|
|
const { resolve } = require('node:path')
|
2023-06-08 05:24:49 -07:00
|
|
|
const pkgJson = require('@npmcli/package-json')
|
2021-11-04 20:42:47 +00:00
|
|
|
const reifyFinish = require('../utils/reify-finish.js')
|
2024-04-30 23:53:22 -07:00
|
|
|
const completion = require('../utils/installed-shallow.js')
|
2021-11-04 20:42:47 +00:00
|
|
|
const ArboristWorkspaceCmd = require('../arborist-cmd.js')
|
2024-04-30 23:53:22 -07:00
|
|
|
|
2021-05-20 15:54:50 -04:00
|
|
|
class Uninstall extends ArboristWorkspaceCmd {
|
2021-11-18 20:58:02 +00:00
|
|
|
static description = 'Remove a package'
|
|
|
|
static name = 'uninstall'
|
2023-06-08 05:24:49 -07:00
|
|
|
static params = ['save', 'global', ...super.params]
|
2021-11-18 20:58:02 +00:00
|
|
|
static usage = ['[<@scope>/]<pkg>...']
|
2022-03-03 21:38:08 +00:00
|
|
|
static ignoreImplicitWorkspace = false
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2021-11-18 20:58:02 +00:00
|
|
|
// TODO
|
|
|
|
/* istanbul ignore next */
|
2023-06-08 05:24:49 -07:00
|
|
|
static async completion (opts, npm) {
|
|
|
|
return completion(npm, opts)
|
2021-03-04 17:40:28 -05:00
|
|
|
}
|
2015-10-09 23:13:57 -07:00
|
|
|
|
2021-11-04 20:42:47 +00:00
|
|
|
async exec (args) {
|
2021-03-04 17:40:28 -05:00
|
|
|
if (!args.length) {
|
2022-05-25 21:26:36 +00:00
|
|
|
if (!this.npm.global) {
|
2021-03-04 17:40:28 -05:00
|
|
|
throw new Error('Must provide a package name to remove')
|
2021-11-18 20:58:02 +00:00
|
|
|
} else {
|
2021-03-04 17:40:28 -05:00
|
|
|
try {
|
2023-06-08 05:24:49 -07:00
|
|
|
const { content: pkg } = await pkgJson.normalize(this.npm.localPrefix)
|
2023-01-16 22:38:23 -05:00
|
|
|
args.push(pkg.name)
|
2021-03-04 17:40:28 -05:00
|
|
|
} catch (er) {
|
2021-11-18 20:58:02 +00:00
|
|
|
if (er.code !== 'ENOENT' && er.code !== 'ENOTDIR') {
|
2021-03-04 17:40:28 -05:00
|
|
|
throw er
|
2021-11-18 20:58:02 +00:00
|
|
|
} else {
|
2021-11-04 20:42:47 +00:00
|
|
|
throw this.usageError()
|
2021-11-18 20:58:02 +00:00
|
|
|
}
|
2021-03-04 17:40:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 22:38:23 -05:00
|
|
|
// the /path/to/node_modules/..
|
|
|
|
const path = this.npm.global
|
|
|
|
? resolve(this.npm.globalDir, '..')
|
|
|
|
: this.npm.localPrefix
|
|
|
|
|
2023-05-07 03:37:34 -07:00
|
|
|
const Arborist = require('@npmcli/arborist')
|
2021-03-24 17:25:32 -04:00
|
|
|
const opts = {
|
2021-03-04 17:40:28 -05:00
|
|
|
...this.npm.flatOptions,
|
2021-03-24 17:25:32 -04:00
|
|
|
path,
|
2021-03-04 17:40:28 -05:00
|
|
|
rm: args,
|
2021-06-17 18:59:38 +00:00
|
|
|
workspaces: this.workspaceNames,
|
2021-03-24 17:25:32 -04:00
|
|
|
}
|
|
|
|
const arb = new Arborist(opts)
|
|
|
|
await arb.reify(opts)
|
2021-03-04 17:40:28 -05:00
|
|
|
await reifyFinish(this.npm, arb)
|
|
|
|
}
|
|
|
|
}
|
2024-04-30 23:53:22 -07:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
module.exports = Uninstall
|