Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log a colored message with Yeoman?

I have tried console.log('message'), but it returns the message in black.

Is there a way to log a message in color in a generator?

like image 483
benoit Avatar asked Aug 21 '13 10:08

benoit


2 Answers

You can use the same color module Chalk as used by the generator system to colorize your text.

First install it:npm install --save chalk

Then:

var chalk = require('chalk');
this.log(chalk.bold.yellow('message'));
like image 141
Sindre Sorhus Avatar answered Nov 16 '22 08:11

Sindre Sorhus


For new users viewing this question console.log(); should "never" be used in Yeoman, according to the Documentation. Instead use generator.log(); mainly seen in practice as this.log().

To allow for this flexibility [of running in various user interfaces, not just in terminals], Yeoman provides a set of user interface element abstractions. It is your responsibility as an author to only use those abstractions when interacting with your end user. [Emphasis added.] Using other ways will probably prevent your generator from running correctly in different Yeoman tools.

For example, it is important to never use console.log() or process.stdout.write() to output content. Using them would hide the output from users not using a terminal. Instead, always rely on the UI generic this.log() method, where this is the context of your current generator.

like image 43
Chris Haugen Avatar answered Nov 16 '22 08:11

Chris Haugen