Skip to content

Commit

Permalink
refactor(ng-dev/format): return exit code from functions formatting a…
Browse files Browse the repository at this point in the history
…nd checking format of files (#204)

Returning an exit code to represent the functions failure/success allows for other tooling to utilize
the functions in place, previously when process.exit was called directly, it would end the process
entirely.

PR Close #204
  • Loading branch information
josephperrott authored and devversion committed Sep 9, 2021
1 parent f631e36 commit 75f95e8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
16 changes: 8 additions & 8 deletions ng-dev/format/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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!);
},
);
}
21 changes: 12 additions & 9 deletions ng-dev/format/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -29,22 +31,24 @@ 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.
const failures = await runFormatterInParallel(files, 'check');

if (failures === false) {
info('No files matched for formatting check.');
process.exit(0);
return 0;
}

if (failures.length) {
Expand All @@ -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;
}
}

0 comments on commit 75f95e8

Please sign in to comment.