[dev/precommitHook] log all failures, don't stop at first (#19271)

This commit is contained in:
Spencer 2018-06-22 12:41:31 -07:00 committed by GitHub
parent da2da74a08
commit d9b31710d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 5 deletions

View file

@ -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);
}

View file

@ -18,4 +18,4 @@
*/
export { run } from './run';
export { createFailError } from './fail';
export { createFailError, combineErrors } from './fail';

View file

@ -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);
}
});