symfony-style-console
Version:
Use the style and utilities of the Symfony Console in Node.js
131 lines (130 loc) • 4.22 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 OutputInterface_1 = require("./OutputInterface");
var StreamOutput_1 = require("./StreamOutput");
/**
* `ConsoleOutput` is the default class for all CLI output. It uses `stdout` and `stderr`.
*
* This class is a convenient wrapper around [[StreamOutput]] for both `stdout` and `stderr`.
*
* ```
* const output = new ConsoleOutput()
* ```
*
* This is equivalent to:
*
* ```
* const output = new StreamOutput(process.stdout)
* const stdErr = new StreamOutput(process.stderr)
* ```
*
* @author Fabien Potencier <fabien@symfony.com>
*
* Original PHP class
*
* @author Florian Reuschel <florian@loilo.de>
*
* Port to TypeScript
*
*/
var ConsoleOutput = /** @class */ (function (_super) {
__extends(ConsoleOutput, _super);
function ConsoleOutput(verbosity, output, decorated, formatter) {
if (verbosity === void 0) { verbosity = OutputInterface_1.VERBOSITY_NORMAL; }
if (output === void 0) { output = process.stdout; }
if (decorated === void 0) { decorated = true; }
if (formatter === void 0) { formatter = null; }
var _this = _super.call(this, output, verbosity, decorated, formatter) || this;
var actualDecorated = _this.isDecorated();
_this.stderr = new StreamOutput_1.default(_this.openErrorStream(), verbosity, decorated, formatter);
if (null === decorated) {
_this.setDecorated(actualDecorated && _this.stderr.isDecorated());
}
return _this;
}
/**
* {@inheritdoc}
*/
ConsoleOutput.prototype.setDecorated = function (decorated) {
_super.prototype.setDecorated.call(this, decorated);
this.stderr.setDecorated(decorated);
};
/**
* {@inheritdoc}
*/
ConsoleOutput.prototype.setFormatter = function (formatter) {
_super.prototype.setFormatter.call(this, formatter);
this.stderr.setFormatter(formatter);
};
/**
* {@inheritdoc}
*/
ConsoleOutput.prototype.setVerbosity = function (level) {
_super.prototype.setVerbosity.call(this, level);
this.stderr.setVerbosity(level);
};
/**
* {@inheritdoc}
*/
ConsoleOutput.prototype.getErrorOutput = function () {
return this.stderr;
};
/**
* {@inheritdoc}
*/
ConsoleOutput.prototype.setErrorOutput = function (error) {
this.stderr = error;
};
/**
* Returns true if current environment supports writing console output to STDOUT.
*
* @return bool
*/
ConsoleOutput.prototype.hasStdoutSupport = function () {
return !!process.stdout;
};
/**
* Returns true if current environment supports writing console output to STDERR.
*
* @return bool
*/
ConsoleOutput.prototype.hasStderrSupport = function () {
return !!process.stderr;
};
/**
* @return NodeJS.WritableStream
*/
ConsoleOutput.prototype.openOutputStream = function () {
if (!this.hasStdoutSupport()) {
throw new Error('No process.stdout available');
}
return process.stdout;
};
/**
* @return resource
*/
ConsoleOutput.prototype.openErrorStream = function () {
if (!this.hasStderrSupport()) {
if (!this.hasStdoutSupport()) {
throw new Error('Neither process.stderr nor process.stdout available');
}
return process.stdout;
}
return process.stderr;
};
return ConsoleOutput;
}(StreamOutput_1.default));
exports.default = ConsoleOutput;