1 | 'use strict'
|
2 |
|
3 |
|
4 | module.exports = uninstall
|
5 |
|
6 | const path = require('path')
|
7 | const validate = require('aproba')
|
8 | const readJson = require('read-package-json')
|
9 | const iferr = require('iferr')
|
10 | const npm = require('./npm.js')
|
11 | const Installer = require('./install.js').Installer
|
12 | const getSaveType = require('./install/save.js').getSaveType
|
13 | const removeDeps = require('./install/deps.js').removeDeps
|
14 | const log = require('npmlog')
|
15 | const usage = require('./utils/usage')
|
16 |
|
17 | uninstall.usage = usage(
|
18 | 'uninstall',
|
19 | 'npm uninstall [<@scope>/]<pkg>[@<version>]... [--save-prod|--save-dev|--save-optional] [--no-save]'
|
20 | )
|
21 |
|
22 | uninstall.completion = require('./utils/completion/installed-shallow.js')
|
23 |
|
24 | function uninstall (args, cb) {
|
25 | validate('AF', arguments)
|
26 |
|
27 | const dryrun = !!npm.config.get('dry-run')
|
28 |
|
29 | if (args.length === 1 && args[0] === '.') args = []
|
30 |
|
31 | const where = npm.config.get('global') || !args.length
|
32 | ? path.resolve(npm.globalDir, '..')
|
33 | : npm.prefix
|
34 |
|
35 | args = args.filter(function (a) {
|
36 | return path.resolve(a) !== where
|
37 | })
|
38 |
|
39 | if (args.length) {
|
40 | new Uninstaller(where, dryrun, args).run(cb)
|
41 | } else {
|
42 |
|
43 | readJson(path.resolve(npm.localPrefix, 'package.json'), function (er, pkg) {
|
44 | if (er && er.code !== 'ENOENT' && er.code !== 'ENOTDIR') return cb(er)
|
45 | if (er) return cb(uninstall.usage)
|
46 | new Uninstaller(where, dryrun, [pkg.name]).run(cb)
|
47 | })
|
48 | }
|
49 | }
|
50 |
|
51 | class Uninstaller extends Installer {
|
52 | constructor (where, dryrun, args) {
|
53 | super(where, dryrun, args)
|
54 | this.remove = []
|
55 | }
|
56 |
|
57 | loadArgMetadata (next) {
|
58 | this.remove = this.args.map(function (arg) { return {name: arg} })
|
59 | this.args = []
|
60 | next()
|
61 | }
|
62 |
|
63 | loadAllDepsIntoIdealTree (cb) {
|
64 | validate('F', arguments)
|
65 | log.silly('uninstall', 'loadAllDepsIntoIdealTree')
|
66 | const saveDeps = getSaveType()
|
67 |
|
68 | super.loadAllDepsIntoIdealTree(iferr(cb, () => {
|
69 | removeDeps(this.remove, this.idealTree, saveDeps, (err) => {
|
70 | cb(err)
|
71 | })
|
72 | }))
|
73 | }
|
74 |
|
75 |
|
76 | runPreinstallTopLevelLifecycles (cb) { cb() }
|
77 | runPostinstallTopLevelLifecycles (cb) { cb() }
|
78 | }
|
79 |
|
80 | module.exports.Uninstaller = Uninstaller
|