diff --git a/ng-dev/format/cli.ts b/ng-dev/format/cli.ts index 770566d76..bf8c0bd93 100644 --- a/ng-dev/format/cli.ts +++ b/ng-dev/format/cli.ts @@ -25,41 +25,41 @@ export function buildFormatParser(localYargs: yargs.Argv) { 'all', 'Run the formatter on all files in the repository', (args) => args, - ({check}) => { + async ({check}) => { const executionCmd = check ? checkFiles : formatFiles; const allFiles = GitClient.get().allFiles(); - executionCmd(allFiles); + process.exitCode = await executionCmd(allFiles); }, ) .command( 'changed [shaOrRef]', 'Run the formatter on files changed since the provided sha/ref', (args) => args.positional('shaOrRef', {type: 'string'}), - ({shaOrRef, check}) => { + async ({shaOrRef, check}) => { const git = GitClient.get(); const sha = shaOrRef || git.mainBranchName; const executionCmd = check ? checkFiles : formatFiles; const allChangedFilesSince = git.allChangesFilesSince(sha); - executionCmd(allChangedFilesSince); + process.exitCode = await executionCmd(allChangedFilesSince); }, ) .command( 'staged', 'Run the formatter on all staged files', (args) => args, - ({check}) => { + async ({check}) => { const executionCmd = check ? checkFiles : formatFiles; const allStagedFiles = GitClient.get().allStagedFiles(); - executionCmd(allStagedFiles); + process.exitCode = await executionCmd(allStagedFiles); }, ) .command( 'files ', 'Run the formatter on provided files', (args) => args.positional('files', {array: true, type: 'string'}), - ({check, files}) => { + async ({check, files}) => { const executionCmd = check ? checkFiles : formatFiles; - executionCmd(files!); + process.exitCode = await executionCmd(files!); }, ); } diff --git a/ng-dev/format/format.ts b/ng-dev/format/format.ts index bbcf7fba7..f3e4b02b2 100644 --- a/ng-dev/format/format.ts +++ b/ng-dev/format/format.ts @@ -12,14 +12,16 @@ import {runFormatterInParallel} from './run-commands-parallel'; /** * Format provided files in place. + * + * @returns a status code indicating whether the formatting run was successful. */ -export async function formatFiles(files: string[]) { +export async function formatFiles(files: string[]): Promise<1 | 0> { // Whether any files failed to format. let failures = await runFormatterInParallel(files, 'format'); if (failures === false) { info('No files matched for formatting.'); - process.exit(0); + return 0; } // The process should exit as a failure if any of the files failed to format. @@ -29,14 +31,16 @@ export async function formatFiles(files: string[]) { info(` • ${filePath}: ${message}`); }); error(red(`Formatting failed, see errors above for more information.`)); - process.exit(1); + return 1; } info(`√ Formatting complete.`); - process.exit(0); + return 0; } /** * Check provided files for formatting correctness. + * + * @returns a status code indicating whether the format check run was successful. */ export async function checkFiles(files: string[]) { // Files which are currently not formatted correctly. @@ -44,7 +48,7 @@ export async function checkFiles(files: string[]) { if (failures === false) { info('No files matched for formatting check.'); - process.exit(0); + return 0; } if (failures.length) { @@ -64,17 +68,16 @@ export async function checkFiles(files: string[]) { if (runFormatter) { // Format the failing files as requested. - await formatFiles(failures.map((f) => f.filePath)); - process.exit(0); + return (await formatFiles(failures.map((f) => f.filePath))) || 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); + return 1; } } else { info('√ All files correctly formatted.'); - process.exit(0); + return 0; } }