From 6afa952475dc0cef512bd933cd5f475d091c3a6f Mon Sep 17 00:00:00 2001 From: Mike Brocchi Date: Thu, 15 Mar 2018 12:51:56 -0400 Subject: [PATCH] fix(@angular/cli): Pass logger to allow schematics to log messages fixes #9976 --- packages/@angular/cli/commands/generate.ts | 34 ++++++++++++++++++-- packages/@angular/cli/tasks/schematic-run.ts | 6 ++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/packages/@angular/cli/commands/generate.ts b/packages/@angular/cli/commands/generate.ts index 1c53e54866d7..83d02f47763a 100644 --- a/packages/@angular/cli/commands/generate.ts +++ b/packages/@angular/cli/commands/generate.ts @@ -2,6 +2,8 @@ import chalk from 'chalk'; const stringUtils = require('ember-cli-string-utils'); import { oneLine } from 'common-tags'; import { CliConfig } from '../models/config'; +import { logging, terminal } from '@angular-devkit/core'; +import { filter } from 'rxjs/operators'; import { getCollection, @@ -199,12 +201,40 @@ export default Command.extend({ commandOptions.type = rawArgs[2]; } + const logger = new logging.IndentLogger('cling'); + const loggerSubscription = logger.pipe( + filter(entry => (entry.level != 'debug'))) + .subscribe(entry => { + let color = (x: any) => terminal.dim(terminal.white(x)); + let output = process.stdout; + switch (entry.level) { + case 'info': + color = terminal.white; + break; + case 'warn': + color = terminal.yellow; + break; + case 'error': + color = terminal.red; + output = process.stderr; + break; + case 'fatal': + color = (x) => terminal.bold(terminal.red(x)); + output = process.stderr; + break; + } + + output.write(color(entry.message) + '\n'); + }); + return schematicRunTask.run({ taskOptions: commandOptions, workingDir: cwd, collectionName, - schematicName - }); + schematicName, + logger: this.logger + }) + .then(() => loggerSubscription.unsubscribe()); }, printDetailedHelp: function (_options: any, rawArgs: any): string | Promise { diff --git a/packages/@angular/cli/tasks/schematic-run.ts b/packages/@angular/cli/tasks/schematic-run.ts index 5283ee30d294..ecb1d812bdd9 100644 --- a/packages/@angular/cli/tasks/schematic-run.ts +++ b/packages/@angular/cli/tasks/schematic-run.ts @@ -15,6 +15,7 @@ import chalk from 'chalk'; import { CliConfig } from '../models/config'; import { concat, concatMap, ignoreElements, map } from 'rxjs/operators'; import { getCollection, getSchematic, getEngineHost, getEngine } from '../utilities/schematics'; +import { logging } from '@angular-devkit/core'; const { green, red, yellow } = chalk; const Task = require('../ember-cli/lib/models/task'); @@ -25,6 +26,7 @@ export interface SchematicRunOptions { emptyHost: boolean; collectionName: string; schematicName: string; + logger: logging.Logger; } export interface SchematicOptions { @@ -45,7 +47,7 @@ interface OutputLogging { export default Task.extend({ run: function (options: SchematicRunOptions): Promise { - const { taskOptions, workingDir, emptyHost, collectionName, schematicName } = options; + const { taskOptions, workingDir, emptyHost, collectionName, schematicName, logger } = options; const ui = this.ui; @@ -125,7 +127,7 @@ export default Task.extend({ }); return new Promise((resolve, reject) => { - schematic.call(opts, host).pipe( + schematic.call(opts, host, { logger }).pipe( map((tree: Tree) => Tree.optimize(tree)), concatMap((tree: Tree) => { return dryRunSink.commit(tree).pipe(