Paul Gschwendtner c3cbfc22e0 fix(dev-infra): format command incorrectly prints outdated files (#42252)
When `ng-dev format --check` is run, the ng-dev tool prints out
all files that are out-of-date. We recently updated the format
tool to also capture `stderr` for failed files. This broke the
console message as we did not unwrap the `FormatFailure` to
their file path when printing the "ng-dev format" fix command.

PR Close #42252
2021-05-25 17:57:01 +00:00

81 lines
2.4 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {error, info, promptConfirm, red} from '../utils/console';
import {runFormatterInParallel} from './run-commands-parallel';
/**
* Format provided files in place.
*/
export async function formatFiles(files: string[]) {
// Whether any files failed to format.
let failures = await runFormatterInParallel(files, 'format');
if (failures === false) {
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) {
error(red(`The following files could not be formatted:`));
failures.forEach(({filePath, message}) => {
info(`${filePath}: ${message}`);
});
error(red(`Formatting failed, see errors above for more information.`));
process.exit(1);
}
info(`√ Formatting complete.`);
process.exit(0);
}
/**
* Check provided files for formatting correctness.
*/
export async function checkFiles(files: string[]) {
// Files which are currently not formatted correctly.
const failures = await runFormatterInParallel(files, 'check');
if (failures === false) {
info('No files matched for formatting check.');
process.exit(0);
}
if (failures.length) {
// Provide output expressing which files are failing formatting.
info.group('\nThe following files are out of format:');
for (const {filePath} of failures) {
info(`${filePath}`);
}
info.groupEnd();
info();
// If the command is run in a non-CI environment, prompt to format the files immediately.
let runFormatter = false;
if (!process.env['CI']) {
runFormatter = await promptConfirm('Format the files now?', true);
}
if (runFormatter) {
// Format the failing files as requested.
await formatFiles(failures.map(f => f.filePath));
process.exit(0);
} else {
// Inform user how to format files in the future.
info();
info(`To format the failing file run the following command:`);
info(` yarn ng-dev format files ${failures.map(f => f.filePath).join(' ')}`);
process.exit(1);
}
} else {
info('√ All files correctly formatted.');
process.exit(0);
}
}