Permalink
Browse files

Respect --color settings in outdated.

Fixes #4493
  • Loading branch information...
1 parent 012da03 commit d4f6f3ff83bd14fb60d3ac6392cb8eb6b1c55ce1 @timoxley timoxley committed with isaacs Jan 15, 2014
Showing with 69 additions and 11 deletions.
  1. +28 −11 lib/outdated.js
  2. +41 −0 test/tap/outdated-color.js
View
@@ -40,12 +40,19 @@ function outdated (args, silent, cb) {
console.log(makeJSON(list))
} else {
var outList = list.map(makePretty)
- var outTable = [[ styles.underline("Package")
- , styles.underline("Current")
- , styles.underline("Wanted")
- , styles.underline("Latest")
- , styles.underline("Location")
+ var outTable = [[ "Package"
+ , "Current"
+ , "Wanted"
+ , "Latest"
+ , "Location"
]].concat(outList)
+
+ if (npm.color) {
+ outTable[0] = outTable[0].map(function(heading) {
+ return styles.underline(heading)
+ })
+ }
+
var tableOpts = { align: ["l", "r", "r", "r", "l"]
, stringLength: function(s) { return ansiTrim(s).length }
}
@@ -80,12 +87,22 @@ function makePretty (p) {
if (!npm.config.get("global")) {
dir = path.relative(process.cwd(), dir)
}
- return [ has === want ? color.yellow(dep) : color.red(dep)
- , (has || "MISSING")
- , color.green(want)
- , color.magenta(latest)
- , color.brightBlack(dirToPrettyLocation(dir))
- ]
+
+ var columns = [ dep
+ , has || "MISSING"
+ , want
+ , latest
+ , dirToPrettyLocation(dir)
+ ]
+
+ if (npm.color) {
+ columns[0] = color[has === want ? "yellow" : "red"](columns[0]) // dep
+ columns[2] = color.green(columns[2]) // want
+ columns[3] = color.magenta(columns[3]) // latest
+ columns[4] = color.brightBlack(columns[4]) // dir
+ }
+
+ return columns
}
function ansiTrim (str) {
View
@@ -0,0 +1,41 @@
+var common = require("../common-tap.js")
+var test = require("tap").test
+var npm = require("../../")
+var mkdirp = require("mkdirp")
+var rimraf = require("rimraf")
+var mr = require("npm-registry-mock")
+var exec = require('child_process').exec
+
+var pkg = __dirname + '/outdated'
+var NPM_BIN = __dirname + '/../../bin/npm-cli.js'
+mkdirp.sync(pkg + "/cache")
+
+function hasControlCodes(str) {
+ return str.length !== ansiTrim(str).length
+}
+
+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, "")
+}
+
+// note hard to automate tests for color = true
+// as npm kills the color config when it detects
+// it's not running in a tty
+test("does not use ansi styling", function (t) {
+ t.plan(3)
+ exec('node ' + NPM_BIN + ' outdated --color false', {
+ cwd: pkg
+ }, function(err, stdout) {
+ t.ifError(err)
+ t.ok(stdout, stdout.length)
+ t.ok(!hasControlCodes(stdout))
+ })
+})
+
+test("cleanup", function (t) {
+ rimraf.sync(pkg + "/cache")
+ t.end()
+})
+

0 comments on commit d4f6f3f

Please sign in to comment.