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