Permalink
Browse files

Make `npm outdated` output prettier.

Fixes #4176. Uses ansicolors, ansistyles, and text-table.
  • Loading branch information...
1 parent ec2c50f commit fd3017fc3e9d42acf6394a5285122edb4dc16106 @quimcalpe quimcalpe committed with domenic Nov 22, 2013
Showing with 974 additions and 7 deletions.
  1. +31 −5 lib/outdated.js
  2. +15 −0 node_modules/ansicolors/.npmignore
  3. +4 −0 node_modules/ansicolors/.travis.yml
  4. +23 −0 node_modules/ansicolors/LICENSE
  5. +62 −0 node_modules/ansicolors/README.md
  6. +65 −0 node_modules/ansicolors/ansicolors.js
  7. +34 −0 node_modules/ansicolors/package.json
  8. +71 −0 node_modules/ansicolors/test/ansicolors.js
  9. +15 −0 node_modules/ansistyles/.npmignore
  10. +4 −0 node_modules/ansistyles/.travis.yml
  11. +23 −0 node_modules/ansistyles/LICENSE
  12. +71 −0 node_modules/ansistyles/README.md
  13. +38 −0 node_modules/ansistyles/ansistyles.js
  14. +34 −0 node_modules/ansistyles/package.json
  15. +15 −0 node_modules/ansistyles/test/ansistyles.js
  16. +4 −0 node_modules/text-table/.travis.yml
  17. +18 −0 node_modules/text-table/LICENSE
  18. +8 −0 node_modules/text-table/example/align.js
  19. +8 −0 node_modules/text-table/example/center.js
  20. +9 −0 node_modules/text-table/example/dotalign.js
  21. +11 −0 node_modules/text-table/example/doubledot.js
  22. +6 −0 node_modules/text-table/example/table.js
  23. +86 −0 node_modules/text-table/index.js
  24. +52 −0 node_modules/text-table/package.json
  25. +134 −0 node_modules/text-table/readme.markdown
  26. +18 −0 node_modules/text-table/test/align.js
  27. +32 −0 node_modules/text-table/test/ansi-colors.js
  28. +18 −0 node_modules/text-table/test/center.js
  29. +20 −0 node_modules/text-table/test/dotalign.js
  30. +24 −0 node_modules/text-table/test/doubledot.js
  31. +14 −0 node_modules/text-table/test/table.js
  32. +7 −2 package.json
View
@@ -26,6 +26,9 @@ var path = require("path")
, npm = require("./npm.js")
, url = require("url")
, isGitUrl = require("./utils/is-git-url.js")
+ , color = require("ansicolors")
+ , styles = require("ansistyles")
+ , table = require("text-table")
function outdated (args, silent, cb) {
if (typeof cb !== "function") cb = silent, silent = false
@@ -35,7 +38,17 @@ function outdated (args, silent, cb) {
if (npm.config.get("json")) {
console.log(makeJSON(list))
} else {
- console.log(list.map(makePretty).join("\n"))
+ var outList = list.map(makePretty)
+ var outTable = [[ styles.underline("Package")
+ , styles.underline("Current")
+ , styles.underline("Wanted")
+ , styles.underline("Latest")
+ , styles.underline("Location")
+ ]].concat(outList)
+ var tableOpts = { align: ["l", "r", "r", "r", "l"]
+ , stringLength: function(s) { return ansiTrim(s).length }
+ }
+ console.log(table(outTable, tableOpts))
}
cb(null, list)
})
@@ -66,10 +79,23 @@ function makePretty (p) {
if (!npm.config.get("global")) {
dir = path.relative(process.cwd(), dir)
}
- return dep + " " + dir
- + " current=" + (has || "MISSING")
- + " wanted=" + want
- + " latest=" + latest
+ return [ has === want ? color.yellow(dep) : color.red(dep)
+ , (has || "MISSING")
+ , color.green(want)
+ , color.magenta(latest)
+ , color.brightBlack(dirToPrettyLocation(dir))
+ ]
+}
+
+function ansiTrim (str) {
+ var r = new RegExp("\x1b(?:\\[(?:\\d+[ABCDEFGJKSTm]|\\d+;\\d+[Hfm]|" +
+ "\\d+;\\d+;\\d+m|6n|s|u|\\?25[lh])|\\w)", "g");
+ return str.replace(r, "")
+}
+
+function dirToPrettyLocation (dir) {
+ return dir.replace(/^node_modules[/\\]/, "")
+ .replace(/[[/\\]node_modules[/\\]/g, " > ")
}
function makeJSON (list) {

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Oops, something went wrong.

3 comments on commit fd3017f

ollym replied Jan 4, 2014

Why......!? I had a wonderful little cron script that autoupdated my module:

if [ ! -z \"`npm -g outdated myapp`\" ]; then
  npm -g update myapp
  systemctl restart myapp.service;
fi

Which relied on the output being empty "" when the package was up-to-date. Now my script has to be a lot more complex...

Contributor

rlidwka replied Jan 4, 2014

@ollym, use/parse JSON output instead.

PS: I wonder if it makes sense to set a return code equal to an amount of outdated packages

PPS:
xkcd

ollym replied Jan 4, 2014

For what it's worth, I ended up with the following work-around:

if [ `npm -g outdated myapp | wc -l` -gt 1 ]; then
  npm -g update myapp
  systemctl restart myapp.service;
fi
Please sign in to comment.