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 treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/outdated.js +49-20Lines changed: 49 additions & 20 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -35,6 +35,7 @@ var path = require("path") | 35 | 35 | , mapToRegistry = require("./utils/map-to-registry.js") | 36 | 36 | , npa = require("npm-package-arg") | 37 | 37 | , readInstalled = require("read-installed") | | 38 | + , long = npm.config.get("long") | 38 | 39 |
| 39 | 40 | function outdated (args, silent, cb) { | 40 | 41 | if (typeof cb !== "function") cb = silent, silent = false | @@ -47,12 +48,14 @@ function outdated (args, silent, cb) { | 47 | 48 | console.log(makeParseable(list)) | 48 | 49 | } else { | 49 | 50 | 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) | 56 | 59 |
| 57 | 60 | if (npm.color) { | 58 | 61 | outTable[0] = outTable[0].map(function(heading) { | @@ -69,13 +72,14 @@ function outdated (args, silent, cb) { | 69 | 72 | }) | 70 | 73 | } | 71 | 74 |
| 72 | | -// [[ dir, dep, has, want, latest ]] | | 75 | +// [[ dir, dep, has, want, latest, type ]] | 73 | 76 | function makePretty (p) { | 74 | 77 | var dep = p[1] | 75 | 78 | , dir = path.resolve(p[0], "node_modules", dep) | 76 | 79 | , has = p[2] | 77 | 80 | , want = p[3] | 78 | 81 | , latest = p[4] | | 82 | + , type = p[6] | 79 | 83 |
| 80 | 84 | if (!npm.config.get("global")) { | 81 | 85 | dir = path.relative(process.cwd(), dir) | @@ -87,12 +91,14 @@ function makePretty (p) { | 87 | 91 | , latest | 88 | 92 | , dirToPrettyLocation(dir) | 89 | 93 | ] | | 94 | + if (long) columns[5] = type | 90 | 95 |
| 91 | 96 | if (npm.color) { | 92 | 97 | columns[0] = color[has === want ? "yellow" : "red"](columns[0]) // dep | 93 | 98 | columns[2] = color.green(columns[2]) // want | 94 | 99 | columns[3] = color.magenta(columns[3]) // latest | 95 | 100 | columns[4] = color.brightBlack(columns[4]) // dir | | 101 | + if (long) columns[5] = color.brightBlack(columns[5]) // type | 96 | 102 | } | 97 | 103 |
| 98 | 104 | return columns | @@ -111,17 +117,22 @@ function dirToPrettyLocation (dir) { | 111 | 117 |
| 112 | 118 | function makeParseable (list) { | 113 | 119 | return list.map(function (p) { | | 120 | + | 114 | 121 | var dep = p[1] | 115 | 122 | , dir = path.resolve(p[0], "node_modules", dep) | 116 | 123 | , has = p[2] | 117 | 124 | , want = p[3] | 118 | 125 | , latest = p[4] | | 126 | + , type = p[6] | 119 | 127 |
| 120 | | - return [ dir | | 128 | + var out = [ dir | 121 | 129 | , dep + "@" + want | 122 | 130 | , (has ? (dep + "@" + has) : "MISSING") | 123 | 131 | , dep + "@" + latest | 124 | | - ].join(":") | | 132 | + ] | | 133 | + if (long) out.push(type) | | 134 | + | | 135 | + return out.join(":") | 125 | 136 | }).join(os.EOL) | 126 | 137 | } | 127 | 138 |
| @@ -137,6 +148,7 @@ function makeJSON (list) { | 137 | 148 | , latest: p[4] | 138 | 149 | , location: dir | 139 | 150 | } | | 151 | + if (long) out[p[1]].type = p[6] | 140 | 152 | }) | 141 | 153 | return JSON.stringify(out, null, 2) | 142 | 154 | } | @@ -154,13 +166,23 @@ function outdated_ (args, dir, parentHas, depth, cb) { | 154 | 166 | return cb(null, []) | 155 | 167 | } | 156 | 168 | var deps = null | | 169 | + var types = {} | 157 | 170 | readJson(path.resolve(dir, "package.json"), function (er, d) { | 158 | 171 | d = d || {} | 159 | 172 | if (er && er.code !== "ENOENT" && er.code !== "ENOTDIR") return cb(er) | 160 | 173 | deps = (er) ? true : (d.dependencies || {}) | | 174 | + if (!er) { | | 175 | + Object.keys(deps).forEach(function (k) { | | 176 | + types[k] = "dependencies" | | 177 | + }) | | 178 | + } | 161 | 179 |
| 162 | 180 | if (npm.config.get("save-dev")) { | 163 | 181 | deps = d.devDependencies || {} | | 182 | + Object.keys(deps).forEach(function (k) { | | 183 | + types[k] = "devDependencies" | | 184 | + }) | | 185 | + | 164 | 186 | return next() | 165 | 187 | } | 166 | 188 |
| @@ -174,6 +196,9 @@ function outdated_ (args, dir, parentHas, depth, cb) { | 174 | 196 |
| 175 | 197 | if (npm.config.get("save-optional")) { | 176 | 198 | deps = d.optionalDependencies || {} | | 199 | + Object.keys(deps).forEach(function (k) { | | 200 | + types[k] = "optionalDependencies" | | 201 | + }) | 177 | 202 | return next() | 178 | 203 | } | 179 | 204 |
| @@ -186,6 +211,7 @@ function outdated_ (args, dir, parentHas, depth, cb) { | 186 | 211 | Object.keys(d.devDependencies || {}).forEach(function (k) { | 187 | 212 | if (!(k in parentHas)) { | 188 | 213 | deps[k] = d.devDependencies[k] | | 214 | + types[k] = "devDependencies" | 189 | 215 | } | 190 | 216 | }) | 191 | 217 | } | @@ -236,12 +262,14 @@ function outdated_ (args, dir, parentHas, depth, cb) { | 236 | 262 | // if has[dep] !== shouldHave[dep], then cb with the data | 237 | 263 | // otherwise dive into the folder | 238 | 264 | 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]) | 240 | 268 | }, cb) | 241 | 269 | } | 242 | 270 | } | 243 | 271 |
| 244 | | -function shouldUpdate (args, dir, dep, has, req, depth, cb) { | | 272 | +function shouldUpdate (args, dir, dep, has, req, depth, cb, type) { | 245 | 273 | // look up the most recent version. | 246 | 274 | // if that's what we already have, or if it's not on the args list, | 247 | 275 | // then dive into it. Otherwise, cb() with the data. | @@ -260,15 +288,14 @@ function shouldUpdate (args, dir, dep, has, req, depth, cb) { | 260 | 288 | } | 261 | 289 |
| 262 | 290 | 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]]) | 268 | 295 | } | 269 | 296 |
| 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") | 272 | 299 |
| 273 | 300 | // search for the latest package | 274 | 301 | mapToRegistry(dep, npm.config, function (er, uri, auth) { | @@ -318,10 +345,12 @@ function shouldUpdate (args, dir, dep, has, req, depth, cb) { | 318 | 345 |
| 319 | 346 | if (!curr || dFromUrl && cFromUrl && d._from !== curr.from | 320 | 347 | || d.version !== curr.version | 321 | | - || d.version !== l.version) | | 348 | + || d.version !== l.version) { | 322 | 349 | doIt(d.version, l.version) | 323 | | - else | | 350 | + } | | 351 | + else { | 324 | 352 | skip() | | 353 | + } | 325 | 354 | } | 326 | 355 | } | 327 | 356 | } |
Collapse file Copy file name to clipboard +70Lines changed: 70 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | + | | 24 | + | | 25 | + | | 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 | +} |
|