2020-04-20 16:00:10 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2020-04-20 16:00:10 -04:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2021-05-20 14:47:38 -04:00
|
|
|
import {error, info, promptConfirm, red} from '../utils/console';
|
2020-05-20 17:19:17 -04:00
|
|
|
|
2020-04-24 19:29:53 -04:00
|
|
|
import {runFormatterInParallel} from './run-commands-parallel';
|
2020-04-20 16:00:10 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Format provided files in place.
|
|
|
|
*/
|
2020-04-24 19:29:53 -04:00
|
|
|
export async function formatFiles(files: string[]) {
|
2020-04-20 16:00:10 -04:00
|
|
|
// Whether any files failed to format.
|
2020-04-24 19:29:53 -04:00
|
|
|
let failures = await runFormatterInParallel(files, 'format');
|
2020-04-20 16:00:10 -04:00
|
|
|
|
2020-04-24 19:29:53 -04:00
|
|
|
if (failures === false) {
|
2020-05-20 17:19:17 -04:00
|
|
|
info('No files matched for formatting.');
|
2020-04-24 19:29:53 -04:00
|
|
|
process.exit(0);
|
|
|
|
}
|
2020-04-20 16:00:10 -04:00
|
|
|
|
|
|
|
// The process should exit as a failure if any of the files failed to format.
|
2020-04-24 19:29:53 -04:00
|
|
|
if (failures.length !== 0) {
|
2021-05-20 14:47:38 -04:00
|
|
|
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.`));
|
2020-04-20 16:00:10 -04:00
|
|
|
process.exit(1);
|
|
|
|
}
|
2020-05-20 17:19:17 -04:00
|
|
|
info(`√ Formatting complete.`);
|
2020-04-20 16:00:10 -04:00
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check provided files for formatting correctness.
|
|
|
|
*/
|
2020-04-24 19:29:53 -04:00
|
|
|
export async function checkFiles(files: string[]) {
|
2020-04-20 16:00:10 -04:00
|
|
|
// Files which are currently not formatted correctly.
|
2020-04-24 19:29:53 -04:00
|
|
|
const failures = await runFormatterInParallel(files, 'check');
|
2020-04-20 16:00:10 -04:00
|
|
|
|
2020-04-24 19:29:53 -04:00
|
|
|
if (failures === false) {
|
2020-05-20 17:19:17 -04:00
|
|
|
info('No files matched for formatting check.');
|
2020-04-24 19:29:53 -04:00
|
|
|
process.exit(0);
|
|
|
|
}
|
2020-04-20 16:00:10 -04:00
|
|
|
|
|
|
|
if (failures.length) {
|
|
|
|
// Provide output expressing which files are failing formatting.
|
2020-05-20 17:19:17 -04:00
|
|
|
info.group('\nThe following files are out of format:');
|
2021-05-20 14:47:38 -04:00
|
|
|
for (const {filePath} of failures) {
|
|
|
|
info(` • ${filePath}`);
|
2020-04-20 16:00:10 -04:00
|
|
|
}
|
2020-05-20 17:19:17 -04:00
|
|
|
info.groupEnd();
|
|
|
|
info();
|
2020-04-20 16:00:10 -04:00
|
|
|
|
|
|
|
// If the command is run in a non-CI environment, prompt to format the files immediately.
|
|
|
|
let runFormatter = false;
|
|
|
|
if (!process.env['CI']) {
|
2020-08-11 18:29:11 -04:00
|
|
|
runFormatter = await promptConfirm('Format the files now?', true);
|
2020-04-20 16:00:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (runFormatter) {
|
|
|
|
// Format the failing files as requested.
|
2021-05-20 14:47:38 -04:00
|
|
|
await formatFiles(failures.map(f => f.filePath));
|
2020-04-20 16:00:10 -04:00
|
|
|
process.exit(0);
|
|
|
|
} else {
|
|
|
|
// Inform user how to format files in the future.
|
2020-05-20 17:19:17 -04:00
|
|
|
info();
|
|
|
|
info(`To format the failing file run the following command:`);
|
2021-05-23 13:15:14 -04:00
|
|
|
info(` yarn ng-dev format files ${failures.map(f => f.filePath).join(' ')}`);
|
2020-04-20 16:00:10 -04:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
} else {
|
2020-05-20 17:19:17 -04:00
|
|
|
info('√ All files correctly formatted.');
|
2020-04-20 16:00:10 -04:00
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
}
|