From 87e1664d679e99bab1b4424b43acd9c1c88760c4 Mon Sep 17 00:00:00 2001 From: Spencer Date: Fri, 22 Jun 2018 12:41:31 -0700 Subject: [PATCH] [dev/precommitHook] log all failures, don't stop at first (#19271) --- src/dev/run/fail.js | 22 ++++++++++++++++++++++ src/dev/run/index.js | 2 +- src/dev/run_precommit_hook.js | 21 +++++++++++++++++---- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/dev/run/fail.js b/src/dev/run/fail.js index db72da0dfea6fc..af0197fd47fd4a 100644 --- a/src/dev/run/fail.js +++ b/src/dev/run/fail.js @@ -17,6 +17,8 @@ * under the License. */ +import { inspect } from 'util'; + const FAIL_TAG = Symbol('fail error'); export function createFailError(reason, exitCode = 1) { @@ -29,3 +31,23 @@ export function createFailError(reason, exitCode = 1) { export function isFailError(error) { return Boolean(error && error[FAIL_TAG]); } + +export function combineErrors(errors) { + if (errors.length === 1) { + return errors[0]; + } + + const exitCode = errors + .filter(isFailError) + .reduce((acc, error) => Math.max(acc, error.exitCode), 1); + + const message = errors.reduce((acc, error) => { + if (isFailError(error)) { + return acc + '\n' + error.message; + } + + return acc + `\nUNHANDLED ERROR\n${inspect(error)}`; + }, ''); + + return createFailError(`${errors.length} errors:\n${message}`, exitCode); +} diff --git a/src/dev/run/index.js b/src/dev/run/index.js index 0f432d7f7d36be..1eef88d60b0a52 100644 --- a/src/dev/run/index.js +++ b/src/dev/run/index.js @@ -18,4 +18,4 @@ */ export { run } from './run'; -export { createFailError } from './fail'; +export { createFailError, combineErrors } from './fail'; diff --git a/src/dev/run_precommit_hook.js b/src/dev/run_precommit_hook.js index 8ee81f812a8283..bf8a5665675138 100644 --- a/src/dev/run_precommit_hook.js +++ b/src/dev/run_precommit_hook.js @@ -17,20 +17,33 @@ * under the License. */ -import { run } from './run'; - +import { run, combineErrors } from './run'; import * as Eslint from './eslint'; import * as Tslint from './tslint'; import { getFilesForCommit, checkFileCasing } from './precommit_hook'; run(async ({ log }) => { const files = await getFilesForCommit(); - await checkFileCasing(log, files); + const errors = []; + + try { + await checkFileCasing(log, files); + } catch (error) { + errors.push(error); + } for (const Linter of [Eslint, Tslint]) { const filesToLint = Linter.pickFilesToLint(log, files); if (filesToLint.length > 0) { - await Linter.lintFiles(log, filesToLint); + try { + await Linter.lintFiles(log, filesToLint); + } catch (error) { + errors.push(error); + } } } + + if (errors.length) { + throw combineErrors(errors); + } });