UNPKG

symfony-style-console

Version:

Use the style and utilities of the Symfony Console in Node.js

89 lines (88 loc) 2.65 kB
import Output from './Output'; import { VERBOSITY_NORMAL } from './OutputInterface'; import { EOL, DIRECTORY_SEPARATOR } from '../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 */ export default class StreamOutput extends Output { /** * 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 */ constructor(stream, verbosity = VERBOSITY_NORMAL, decorated = null, formatter = null) { super(verbosity, decorated, formatter); this.stream = stream; if (null === decorated) { decorated = this.hasColorSupport(); } } /** * Gets the stream attached to this StreamOutput instance. * * @return A stream resource */ getStream() { return this.stream; } /** * {@inheritdoc} */ doWrite(message, newline) { if (false === this.stream.write(message) || (newline && false === this.stream.write(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 */ hasColorSupport() { if (DIRECTORY_SEPARATOR === '\\') { const 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; } } }