-
Notifications
You must be signed in to change notification settings - Fork 1
/
npm.js
executable file
·116 lines (107 loc) · 2.87 KB
/
npm.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
var npm = exports
, set = require("./lib/utils/set")
, get = require("./lib/utils/get")
, ini = require("./lib/utils/ini")
, log = require("./lib/utils/log")
, fs = require("fs")
, path = require("path")
npm.commands = {}
try {
var j = JSON.parse(fs.readFileSync(path.join(__dirname, "package.json"))+"")
npm.version = j.version
} catch (ex) {
log(ex, "error reading version")
npm.version = ex
}
var commandCache = {}
; [ "install"
, "activate"
, "deactivate"
, "uninstall"
, "build"
, "link"
, "publish"
, "tag"
, "adduser"
, "config"
, "help"
, "cache"
, "test"
, "stop"
, "start"
, "restart"
, "unpublish"
, "list"
, "ls"
, "rm"
, "owner"
, "update"
, "update-dependents"
, "view"
, "repl"
].forEach(function (c) {
Object.defineProperty(npm.commands, c, { get : function () {
c = c === "list" ? "ls"
: c === "rm" ? "uninstall"
: c
if (c in commandCache) return commandCache[c]
return commandCache[c] = require(__dirname+"/lib/"+c)
}, enumerable: true})
})
// Local store for package data, so it won't have to be fetched/read more than
// once in a single pass. TODO: cache this to disk somewhere when we're using
// the registry, to cut down on HTTP calls.
var registry = {}
npm.set = function (key, val) {
if (typeof key === "object" && !val && key._id) {
val = key
key = key._id
}
return set(registry, key, val)
}
npm.get = function (key) { return get(registry, key) }
var path = require("path")
npm.config =
{ get : function (key) { return ini.get(key) }
, set : function (key, val) { return ini.set(key, val, "cli") }
, del : function (key, val) { return ini.del(key, val, "cli") }
}
Object.defineProperty(npm, "root",
{ get : function () { return npm.config.get("root") }
, set : function (r) {
r = r.charAt(0) === "/" ? r
: path.join(process.execPath, "..", "..", r)
return npm.config.set("root", r)
}
, enumerable : true
})
Object.defineProperty(npm, "dir",
{ get : function () { return path.join(npm.root, ".npm") }
, enumerable : true
})
Object.defineProperty(npm, "cache",
{ get : function () { return path.join(npm.root, ".npm", ".cache") }
, enumerable : true
})
Object.defineProperty(npm, "tmp",
{ get : function () { return path.join(npm.root, ".npm", ".tmp") }
, enumerable : true
})
process.on("exit", function () {
var tmp = npm.tmp
try {
var files = fs.readdirSync(tmp)
} catch (er) {
if (er.message.indexOf("ENOENT") === 0) return
return log(er, "Couldn't clean up tmp folder")
}
files.forEach(function (f) {
try { fs.unlinkSync(tmp + "/" +f) }
catch (er) {
log("Couldn't remove "+tmp+"/"+f, "tmp cleanup error")
log(er, "tmp cleanup error")
log("No big deal, just remove it manually.", "tmp cleanup error")
log(" rm "+tmp+"/"+f, "tmp cleanup error")
}
})
})