Skip to content

Exercise 3: context

Kevin Schuchard edited this page Apr 30, 2019 · 10 revisions

Use SchematicContext.logger

  1. Define a new constant variable named logger that is a reference to the SchematicContext.logger instance.
  2. Use the logger.debug() method
  3. Use the logger.info() method
  4. Use the logger.warn() method
  5. Use the logger.error() method

For example:

export function schematicStarter(_options: any): Rule {
  return (tree: Tree, _context: SchematicContext) => {
	const logger = _context.logger;
    
	logger.debug('This is a debug message');

	return tree;
  };
}

Build the project

npm run build

Execute the hello-world schematic using the CLI

schematics .:hello-world --dry-run=false

You should see the info, warn, and error messages print out.

Bonus

  • Execute the schematics command by itself for a list of additional options
  • Did the debug logger not print out? Run the schematic with with the verbose flag
schematics .:hello-world --dry-run=false --verbose

Clone this wiki locally