symfony-style-console
Version:
Use the style and utilities of the Symfony Console in Node.js
111 lines (110 loc) • 3.76 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var Output_1 = require("./Output");
var OutputInterface_1 = require("./OutputInterface");
var env_1 = require("../env");
/**
* StreamOutput writes the output to a given stream.
*
* Usage:
*
* ```
* const output = new StreamOutput(process.stdout)
* ```
*
* As `StreamOutput` can use any stream, you can also use a file:
*
* ```
* const output = new StreamOutput(fs.createWriteStream('/path/to/output.log', { flags: 'a' }))
* ```
*
* @author Fabien Potencier <fabien@symfony.com>
*
* Original PHP class
*
* @author Florian Reuschel
*
* Port to TypeScript
*/
var StreamOutput = /** @class */ (function (_super) {
__extends(StreamOutput, _super);
/**
* Creates a new StreamOutput instance
*
* @param stream A stream resource
* @param verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
* @param decorated Whether to decorate messages (null for auto-guessing)
* @param formatter Output formatter instance (null to use default OutputFormatter)
*
* @throws InvalidArgumentException When first argument is not a real stream
*/
function StreamOutput(stream, verbosity, decorated, formatter) {
if (verbosity === void 0) { verbosity = OutputInterface_1.VERBOSITY_NORMAL; }
if (decorated === void 0) { decorated = null; }
if (formatter === void 0) { formatter = null; }
var _this = _super.call(this, verbosity, decorated, formatter) || this;
_this.stream = stream;
if (null === decorated) {
decorated = _this.hasColorSupport();
}
return _this;
}
/**
* Gets the stream attached to this StreamOutput instance.
*
* @return A stream resource
*/
StreamOutput.prototype.getStream = function () {
return this.stream;
};
/**
* {@inheritdoc}
*/
StreamOutput.prototype.doWrite = function (message, newline) {
if (false === this.stream.write(message) ||
(newline && false === this.stream.write(env_1.EOL))) {
// should never happen
throw new Error('Unable to write output.');
}
};
/**
* Returns true if the stream supports colorization.
*
* Colorization is disabled if not supported by the stream:
*
* - Windows != 10.0.10586 without Ansicon, ConEmu or Mintty
* - non tty consoles
*
* @return bool true if the stream supports colorization, false otherwise
*/
StreamOutput.prototype.hasColorSupport = function () {
if (env_1.DIRECTORY_SEPARATOR === '\\') {
var os = require('os');
return ('10.0.10586' === os.release() ||
process.env.ANSICON ||
'ON' === process.env.ConEmuANSI ||
'xterm' === process.env.TERM);
}
if (typeof this.stream.fd !== 'undefined') {
return require('tty').isatty(this.stream.fd);
}
else {
return false;
}
};
return StreamOutput;
}(Output_1.default));
exports.default = StreamOutput;