GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Supports npm outdated include package type with long option · npm/npm@9d61e96

This repository was archived by the owner on Aug 11, 2022. It is now read-only.

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib
    • outdated.js
  • test/tap
    • outdated-long.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/outdated.js‎

Copy file name to clipboardExpand all lines: lib/outdated.js
+49-20Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var path = require("path")
3535
, mapToRegistry = require("./utils/map-to-registry.js")
3636
, npa = require("npm-package-arg")
3737
, readInstalled = require("read-installed")
38+
, long = npm.config.get("long")
3839

3940
function outdated (args, silent, cb) {
4041
if (typeof cb !== "function") cb = silent, silent = false
@@ -47,12 +48,14 @@ function outdated (args, silent, cb) {
4748
console.log(makeParseable(list))
4849
} else {
4950
var outList = list.map(makePretty)
50-
var outTable = [[ "Package"
51-
, "Current"
52-
, "Wanted"
53-
, "Latest"
54-
, "Location"
55-
]].concat(outList)
51+
var outHead = [ "Package"
52+
, "Current"
53+
, "Wanted"
54+
, "Latest"
55+
, "Location"
56+
]
57+
if (long) outHead.push("Package Type")
58+
var outTable = [outHead].concat(outList)
5659

5760
if (npm.color) {
5861
outTable[0] = outTable[0].map(function(heading) {
@@ -69,13 +72,14 @@ function outdated (args, silent, cb) {
6972
})
7073
}
7174

72-
// [[ dir, dep, has, want, latest ]]
75+
// [[ dir, dep, has, want, latest, type ]]
7376
function makePretty (p) {
7477
var dep = p[1]
7578
, dir = path.resolve(p[0], "node_modules", dep)
7679
, has = p[2]
7780
, want = p[3]
7881
, latest = p[4]
82+
, type = p[6]
7983

8084
if (!npm.config.get("global")) {
8185
dir = path.relative(process.cwd(), dir)
@@ -87,12 +91,14 @@ function makePretty (p) {
8791
, latest
8892
, dirToPrettyLocation(dir)
8993
]
94+
if (long) columns[5] = type
9095

9196
if (npm.color) {
9297
columns[0] = color[has === want ? "yellow" : "red"](columns[0]) // dep
9398
columns[2] = color.green(columns[2]) // want
9499
columns[3] = color.magenta(columns[3]) // latest
95100
columns[4] = color.brightBlack(columns[4]) // dir
101+
if (long) columns[5] = color.brightBlack(columns[5]) // type
96102
}
97103

98104
return columns
@@ -111,17 +117,22 @@ function dirToPrettyLocation (dir) {
111117

112118
function makeParseable (list) {
113119
return list.map(function (p) {
120+
114121
var dep = p[1]
115122
, dir = path.resolve(p[0], "node_modules", dep)
116123
, has = p[2]
117124
, want = p[3]
118125
, latest = p[4]
126+
, type = p[6]
119127

120-
return [ dir
128+
var out = [ dir
121129
, dep + "@" + want
122130
, (has ? (dep + "@" + has) : "MISSING")
123131
, dep + "@" + latest
124-
].join(":")
132+
]
133+
if (long) out.push(type)
134+
135+
return out.join(":")
125136
}).join(os.EOL)
126137
}
127138

@@ -137,6 +148,7 @@ function makeJSON (list) {
137148
, latest: p[4]
138149
, location: dir
139150
}
151+
if (long) out[p[1]].type = p[6]
140152
})
141153
return JSON.stringify(out, null, 2)
142154
}
@@ -154,13 +166,23 @@ function outdated_ (args, dir, parentHas, depth, cb) {
154166
return cb(null, [])
155167
}
156168
var deps = null
169+
var types = {}
157170
readJson(path.resolve(dir, "package.json"), function (er, d) {
158171
d = d || {}
159172
if (er && er.code !== "ENOENT" && er.code !== "ENOTDIR") return cb(er)
160173
deps = (er) ? true : (d.dependencies || {})
174+
if (!er) {
175+
Object.keys(deps).forEach(function (k) {
176+
types[k] = "dependencies"
177+
})
178+
}
161179

162180
if (npm.config.get("save-dev")) {
163181
deps = d.devDependencies || {}
182+
Object.keys(deps).forEach(function (k) {
183+
types[k] = "devDependencies"
184+
})
185+
164186
return next()
165187
}
166188

@@ -174,6 +196,9 @@ function outdated_ (args, dir, parentHas, depth, cb) {
174196

175197
if (npm.config.get("save-optional")) {
176198
deps = d.optionalDependencies || {}
199+
Object.keys(deps).forEach(function (k) {
200+
types[k] = "optionalDependencies"
201+
})
177202
return next()
178203
}
179204

@@ -186,6 +211,7 @@ function outdated_ (args, dir, parentHas, depth, cb) {
186211
Object.keys(d.devDependencies || {}).forEach(function (k) {
187212
if (!(k in parentHas)) {
188213
deps[k] = d.devDependencies[k]
214+
types[k] = "devDependencies"
189215
}
190216
})
191217
}
@@ -236,12 +262,14 @@ function outdated_ (args, dir, parentHas, depth, cb) {
236262
// if has[dep] !== shouldHave[dep], then cb with the data
237263
// otherwise dive into the folder
238264
asyncMap(Object.keys(deps), function (dep, cb) {
239-
shouldUpdate(args, dir, dep, has, deps[dep], depth, cb)
265+
if (!long) return shouldUpdate(args, dir, dep, has, deps[dep], depth, cb)
266+
267+
shouldUpdate(args, dir, dep, has, deps[dep], depth, cb, types[dep])
240268
}, cb)
241269
}
242270
}
243271

244-
function shouldUpdate (args, dir, dep, has, req, depth, cb) {
272+
function shouldUpdate (args, dir, dep, has, req, depth, cb, type) {
245273
// look up the most recent version.
246274
// if that's what we already have, or if it's not on the args list,
247275
// then dive into it. Otherwise, cb() with the data.
@@ -260,15 +288,14 @@ function shouldUpdate (args, dir, dep, has, req, depth, cb) {
260288
}
261289

262290
function doIt (wanted, latest) {
263-
cb(null, [[ dir, dep, curr && curr.version, wanted, latest, req ]])
264-
}
265-
266-
if (args.length && args.indexOf(dep) === -1) {
267-
return skip()
291+
if (!long) {
292+
return cb(null, [[ dir, dep, curr && curr.version, wanted, latest, req]])
293+
}
294+
cb(null, [[ dir, dep, curr && curr.version, wanted, latest, req, type]])
268295
}
269296

270-
if (npa(req).type === "git")
271-
return doIt("git", "git")
297+
if (args.length && args.indexOf(dep) === -1) return skip()
298+
if (npa(req).type === "git") return doIt("git", "git")
272299

273300
// search for the latest package
274301
mapToRegistry(dep, npm.config, function (er, uri, auth) {
@@ -318,10 +345,12 @@ function shouldUpdate (args, dir, dep, has, req, depth, cb) {
318345

319346
if (!curr || dFromUrl && cFromUrl && d._from !== curr.from
320347
|| d.version !== curr.version
321-
|| d.version !== l.version)
348+
|| d.version !== l.version) {
322349
doIt(d.version, l.version)
323-
else
350+
}
351+
else {
324352
skip()
353+
}
325354
}
326355
}
327356
}
Collapse file

‎test/tap/outdated-long.js‎

Copy file name to clipboard
+70Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
var path = require("path")
2+
3+
var mr = require("npm-registry-mock")
4+
var rimraf = require("rimraf")
5+
var test = require("tap").test
6+
7+
var common = require("../common-tap.js")
8+
var npm = require("../../")
9+
10+
// config
11+
var pkg = path.resolve(__dirname, "outdated")
12+
var cache = path.resolve(pkg, "cache")
13+
var nodeModules = path.resolve(pkg, "node_modules")
14+
15+
test("it should not throw", function (t) {
16+
cleanup()
17+
process.chdir(pkg)
18+
19+
var originalLog = console.log
20+
var output = []
21+
var expOut = [ path.resolve(__dirname, "outdated/node_modules/underscore"),
22+
path.resolve(__dirname, "outdated/node_modules/underscore")
23+
+ ":[email protected]"
24+
+ ":[email protected]"
25+
+ ":[email protected]"
26+
+ ":dependencies" ]
27+
var expData = [ [ path.resolve(__dirname, "outdated"),
28+
"underscore",
29+
"1.3.1",
30+
"1.3.1",
31+
"1.5.1",
32+
"1.3.1",
33+
"dependencies" ] ]
34+
35+
console.log = function () {
36+
output.push.apply(output, arguments)
37+
}
38+
mr(common.port, function (s) {
39+
npm.load({
40+
cache : "cache",
41+
loglevel : "silent",
42+
parseable : true,
43+
registry : common.registry
44+
},
45+
function () {
46+
npm.install(".", function (err) {
47+
t.ifError(err, "install success")
48+
npm.config.set("long", true)
49+
npm.outdated(function (er, d) {
50+
t.ifError(er, "outdated success")
51+
console.log = originalLog
52+
t.same(output, expOut)
53+
t.same(d, expData)
54+
s.close()
55+
t.end()
56+
})
57+
})
58+
})
59+
})
60+
})
61+
62+
test("cleanup", function (t) {
63+
cleanup()
64+
t.end()
65+
})
66+
67+
function cleanup () {
68+
rimraf.sync(nodeModules)
69+
rimraf.sync(cache)
70+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.