Dedicated central output writing path

All output goes through a central point.  This is the start of a dramatic
refactoring which makes it much easier to track what goes on in npm when
it is used programmagically.
This commit is contained in:
isaacs
2010-10-28 18:32:06 -07:00
parent 8650ff35d1
commit ed004e0817

63
lib/utils/output.js Normal file
View File

@@ -0,0 +1,63 @@
// centralized stdout writer.
exports.doColor = doColor
exports.write = write
var npm = require("../../npm")
, stdio = process.binding("stdio")
, streams = {}
, colored = {}
, net = require("net")
, sys = require("./sys")
function doColor (stream) {
if (!npm.config.get("color")) return false
if (!stdio.isatty) return true
if (stream && stream.fd) stream = stream.fd
if (stream in colored) return colored[stream]
return colored[stream] = stdio.isatty(stream)
}
function write (stream, args, cb) {
stream = getStream(stream)
if (!stream) return
if (!Array.isArray(args)) args = [args]
var msg = ""
, colored = doColor(stream)
args.forEach(function (arg) {
msg += " "
if (typeof arg !== "string") {
msg += sys.inspect(arg, false, 5, colored) + "\n"
} else {
if (!colored) arg = arg.replace(/\033\[[0-9;]+m/g, '')
msg += arg
}
})
// use the \r\n in case we're in raw mode.
msg = (msg.trim() + "\n").split(/\r?\n/).join("\r\n")
var flushed = stream.write(msg)
if (!cb) return flushed
if (flushed) return cb(), true
stream.on("drain", function D () {
stream.removeListener("drain", D)
cb()
})
return false
}
function getStream (fd) {
var stream
if (!fd && fd !== 0) return
if (typeof fd === "string") fd = +fd
if (fd && typeof fd === "object") {
stream = fd
fd = fd.fd
} else if (streams[fd]) {
stream = streams[fd]
} else try {
stream = new net.Stream(fd)
} catch (ex) {}
if (!stream || !stream.writable) return
return streams[fd] = stream
}