feat(dev-infra): migrate format tool to use new logging system (#37232)

Migrate the formatting tool in ng-dev to use new logging system rather
than directly calling console.* to create a better experience
for users.

PR Close #37232
This commit is contained in:
Joey Perrott 2020-05-20 14:19:17 -07:00 committed by Matias Niemelä
parent 52c7aae4df
commit 40e357411e
4 changed files with 27 additions and 20 deletions

View File

@ -7,6 +7,9 @@
*/
import {prompt} from 'inquirer';
import {error, info} from '../utils/console';
import {runFormatterInParallel} from './run-commands-parallel';
/**
@ -17,16 +20,16 @@ export async function formatFiles(files: string[]) {
let failures = await runFormatterInParallel(files, 'format');
if (failures === false) {
console.info('No files matched for formatting.');
info('No files matched for formatting.');
process.exit(0);
}
// The process should exit as a failure if any of the files failed to format.
if (failures.length !== 0) {
console.error(`Formatting failed, see errors above for more information.`);
error(`Formatting failed, see errors above for more information.`);
process.exit(1);
}
console.info(`√ Formatting complete.`);
info(`√ Formatting complete.`);
process.exit(0);
}
@ -38,18 +41,18 @@ export async function checkFiles(files: string[]) {
const failures = await runFormatterInParallel(files, 'check');
if (failures === false) {
console.info('No files matched for formatting check.');
info('No files matched for formatting check.');
process.exit(0);
}
if (failures.length) {
// Provide output expressing which files are failing formatting.
console.group('\nThe following files are out of format:');
info.group('\nThe following files are out of format:');
for (const file of failures) {
console.info(` - ${file}`);
info(` - ${file}`);
}
console.groupEnd();
console.info();
info.groupEnd();
info();
// If the command is run in a non-CI environment, prompt to format the files immediately.
let runFormatter = false;
@ -67,13 +70,13 @@ export async function checkFiles(files: string[]) {
process.exit(0);
} else {
// Inform user how to format files in the future.
console.info();
console.info(`To format the failing file run the following command:`);
console.info(` yarn ng-dev format files ${failures.join(' ')}`);
info();
info(`To format the failing file run the following command:`);
info(` yarn ng-dev format files ${failures.join(' ')}`);
process.exit(1);
}
} else {
console.info('√ All files correctly formatted.');
info('√ All files correctly formatted.');
process.exit(0);
}
}

View File

@ -9,6 +9,7 @@
import {join} from 'path';
import {getRepoBaseDir} from '../../utils/config';
import {error} from '../../utils/console';
import {Formatter} from './base-formatter';
@ -35,9 +36,9 @@ export class Buildifier extends Formatter {
callback:
(file: string, code: number, _: string, stderr: string) => {
if (code !== 0) {
console.error(`Error running buildifier on: ${file}`);
console.error(stderr);
console.error();
error(`Error running buildifier on: ${file}`);
error(stderr);
error();
return true;
}
return false;

View File

@ -9,6 +9,7 @@
import {join} from 'path';
import {getRepoBaseDir} from '../../utils/config';
import {error} from '../../utils/console';
import {Formatter} from './base-formatter';
@ -35,9 +36,9 @@ export class ClangFormat extends Formatter {
callback:
(file: string, code: number, _: string, stderr: string) => {
if (code !== 0) {
console.error(`Error running clang-format on: ${file}`);
console.error(stderr);
console.error();
error(`Error running clang-format on: ${file}`);
error(stderr);
error();
return true;
}
return false;

View File

@ -11,6 +11,8 @@ import * as multimatch from 'multimatch';
import {cpus} from 'os';
import {exec} from 'shelljs';
import {info} from '../utils/console';
import {Formatter, FormatterAction, getActiveFormatters} from './formatters';
const AVAILABLE_THREADS = Math.max(cpus().length - 1, 1);
@ -47,10 +49,10 @@ export function runFormatterInParallel(allFiles: string[], action: FormatterActi
switch (action) {
case 'format':
console.info(`Formatting ${pendingCommands.length} file(s)`);
info(`Formatting ${pendingCommands.length} file(s)`);
break;
case 'check':
console.info(`Checking format of ${pendingCommands.length} file(s)`);
info(`Checking format of ${pendingCommands.length} file(s)`);
break;
default:
throw Error(`Invalid format action "${action}": allowed actions are "format" and "check"`);