Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

Commit

Permalink
Make npm outdated output prettier.
Browse files Browse the repository at this point in the history
Fixes #4176. Uses ansicolors, ansistyles, and text-table.
  • Loading branch information
quimcalpe authored and domenic committed Nov 26, 2013
1 parent ec2c50f commit fd3017f
Show file tree
Hide file tree
Showing 32 changed files with 974 additions and 7 deletions.
36 changes: 31 additions & 5 deletions lib/outdated.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
})
Expand Down Expand Up @@ -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) {
Expand Down
15 changes: 15 additions & 0 deletions node_modules/ansicolors/.npmignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions node_modules/ansicolors/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions node_modules/ansicolors/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions node_modules/ansicolors/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions node_modules/ansicolors/ansicolors.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions node_modules/ansicolors/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 71 additions & 0 deletions node_modules/ansicolors/test/ansicolors.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions node_modules/ansistyles/.npmignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions node_modules/ansistyles/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions node_modules/ansistyles/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

3 comments on commit fd3017f

@ollym
Copy link

@ollym ollym commented on fd3017f Jan 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

@rlidwka
Copy link
Contributor

@rlidwka rlidwka commented on fd3017f Jan 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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
Copy link

@ollym ollym commented on fd3017f Jan 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.