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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
// show the installed versions of packages
//
// --parseable creates output like this:
// <full path>:<name@version>:EXTRANEOUS:INVALID:MISSING:LINK
// where the last three flags are indicators of problems,
// or at least oddness.
module.exports = exports = ls
var npm = require("../npm")
, readInstalled = require("./utils/read-installed")
, output = require("./utils/output")
, log = require("./utils/log")
, relativize = require("./utils/relativize")
, path = require("path")
ls.usage = "npm ls"
function ls (args, cb) {
if (args.length) {
log.warn("ls doesn't take args. Try the 'search' command")
}
var dir = npm.prefix
if (npm.config.get("global")) dir = path.resolve(dir, "lib")
readInstalled(dir, function (er, data) {
if (er) return cb(er)
var long = npm.config.get("long")
var out = makePretty(bfsify(data), long, dir).join("\n")
output.write(out, cb)
})
}
function bfsify (root, current, queue, seen) {
// walk over the data, and turn it from this:
// +-- a
// | `-- b
// | `-- a (truncated)
// `--b (truncated)
// into this:
// +-- a
// | `-- b (truncated)
// `-- b
// `-- a (truncated)
// which looks nicer
current = current || root
queue = queue || []
seen = seen || []
var deps = current.dependencies = current.dependencies || {}
Object.keys(deps).forEach(function (d) {
var dep = deps[d]
if (typeof dep !== "object") return
if (seen.indexOf(dep) !== -1) {
if (npm.config.get("parseable")) {
delete deps[d]
return
}
dep = deps[d] = Object.create(dep)
dep.dependencies = {}
}
queue.push(dep)
seen.push(dep)
})
if (!queue.length) return root
return bfsify(root, queue.shift(), queue, seen)
}
function makePretty (data, long, dir, prefix, list) {
list = list || []
prefix = prefix || ""
list.push(format(data, long, prefix, dir))
var deps = data.dependencies || {}
, childPref = prefix.split("├─").join("│ ")
.split("└─").join(" ")
, depList = Object.keys(deps)
, depLast = depList.length - 1
, maxDepth = npm.config.get("depth")
Object.keys(deps).forEach(function (d, i) {
var depData = deps[d]
if (typeof depData === "string") {
if (data.depth < maxDepth) {
if (npm.config.get("parseable")) {
if (npm.config.get("long")) {
depData = path.resolve(data.path, "node_modules", d)
+ ":" + d + "@" + JSON.stringify(depData)+"::::"
} else {
depData = ""
}
} else {
var p = data.link || data.path
log.warn("Unmet dependency in "+p, d+" "+deps[d])
depData = npm.config.get("parseable")
? ( npm.config.get("long")
? path.resolve(data.path, "node_modules", d)
+ ":"+d+"@"+JSON.stringify(depData)+"::INVALID:MISSING:"
: "" )
: "─ \033[31;40mUNMET DEPENDENCY\033[0m "+d+" "+depData
}
} else {
depData = "─ "+d+"@'"+depData +"' (max depth reached)"
}
}
var c = i === depLast ? "└─" : "├─"
makePretty(depData, long, dir, childPref + c, list)
})
return list.filter(function (l) { return l.trim() })
}
function ugly (data) {
if (typeof data === "string") {
return data
}
if (!npm.config.get("long")) return data.path
return data.path + ":"
+ (data._id || "") + ":"
+ (data.extraneous ? "EXTRANEOUS" : "") + ":"
+ (data.invalid ? "INVALID" : "") + ":"
+ ":" // not missing, since it's an object
+ (data.realPath !== data.path ? data.realPath : "")
}
function format (data, long, prefix, dir) {
if (npm.config.get("parseable")) return ugly(data)
if (typeof data === "string") {
return prefix + data
}
// console.log([data.path, dir], "relativize")
var depLen = Object.keys(data.dependencies).length
, space = prefix.split("├─").join("│ ")
.split("└─").join(" ")
+ (depLen ? "" : " ")
, rel = relativize(data.path, dir)
, l = prefix
+ (rel === "." ? "" : depLen ? "┬ " : "─ ")
+ (data._id ? data._id + " " : "")
+ (data.link ? "-> " + data.link : "") + ""
+ (rel === "." && !(long && data._id) ? dir : "")
if (data.invalid) {
if (data.realName !== data.name) l += "("+data.realName+") "
l += "\033[31;40minvalid\033[0m "
}
if (data.extraneous && rel !== ".") {
l += "\033[32;40mextraneous\033[0m "
}
if (!long || !data._id) return l
var extras = []
if (rel !== ".") extras.push(rel)
else extras.push(dir)
if (data.description) extras.push(data.description)
if (data.repository) extras.push(data.repository.url)
if (data.homepage) extras.push(data.homepage)
extras = extras.filter(function (e) { return e })
var lastExtra = !depLen && extras.length - 1
l += extras.map(function (e, i) {
var indent = !depLen ? " " : "│ "
return "\n" + space + indent + e
}).join("")
return l
}
|