This repository has been archived by the owner on Aug 11, 2022. It is now read-only.
/
deactivate.js
51 lines (47 loc) · 1.62 KB
/
deactivate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var mkdir = require("./utils/mkdir-p")
, npm = require("../npm")
, fs = require("fs")
, log = require("./utils/log")
, path = require("path")
, rm = require("./utils/rm-rf")
, chain = require("./utils/chain")
, lifecycle = require("./utils/lifecycle")
, readJson = require("./utils/read-json")
module.exports = deactivate
function deactivate (args, cb) {
// run the "deactivate" lifecycle event
// unlink the "active" folder
// unlink the libs and main.js from the npm.root
// run the "postdeactivate" lifecycle event
var pkg = args.shift()
, active = path.join(npm.dir, pkg, "active")
, jsonFile = path.join(active, "/package/package.json")
, activeMain = path.join(npm.root, pkg+".js")
, activeLib = path.join(npm.root, pkg)
, pkgData = null
fs.readlink(active, function (er, p) {
if (er) return log("nothing to do", "deactivate", cb)
var version = p.split("/").pop()
readJson(jsonFile, function (er, data) {
if (er) return cb(er)
data.version = version
npm.set(data)
chain
( [lifecycle, data, "predeactivate"]
, [lifecycle, data, "deactivate"]
, [rm, active]
, function (cb) { rm(activeMain, function () { cb() }) }
, function (cb) { rm(activeLib, function () { cb() }) }
, function (cb) {
if (!data.bin) return cb()
chain(Object.getOwnPropertyNames(data.bin)
.map(function (bin) {
return [rm, path.join(npm.config.get("binroot"), bin)]
}).concat(cb))
}
, [lifecycle, data, "postdeactivate"]
, cb
)
})
})
}