2020-04-20 16:00:10 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. 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 {prompt} from 'inquirer';
|
2020-05-20 17:19:17 -04:00
|
|
|
|
|
|
|
import {error, info} from '../utils/console';
|
|
|
|
|
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) {
|
2020-05-20 17:19:17 -04:00
|
|
|
error(`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:');
|
2020-04-20 16:00:10 -04:00
|
|
|
for (const file of failures) {
|
2020-05-20 17:19:17 -04:00
|
|
|
info(` - ${file}`);
|
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']) {
|
|
|
|
runFormatter = (await prompt({
|
|
|
|
type: 'confirm',
|
|
|
|
name: 'runFormatter',
|
|
|
|
message: 'Format the files now?',
|
|
|
|
})).runFormatter;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (runFormatter) {
|
|
|
|
// Format the failing files as requested.
|
|
|
|
await formatFiles(failures);
|
|
|
|
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:`);
|
|
|
|
info(` yarn ng-dev format files ${failures.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);
|
|
|
|
}
|
|
|
|
}
|