feat(dev-infra): Add support for formatting all staged files (#38402)

Adds an ng-dev formatter option to format all of the staged files. This will can
be used to format only the staged files during the pre-commit hook.

PR Close #38402
This commit is contained in:
Joey Perrott 2020-08-11 16:08:35 -07:00 committed by Andrew Kushnir
parent a6292faa97
commit 28534d83ee
2 changed files with 19 additions and 1 deletions

View File

@ -7,7 +7,7 @@
*/
import * as yargs from 'yargs';
import {allChangedFilesSince, allFiles} from '../utils/repo-files';
import {allChangedFilesSince, allFiles, allStagedFiles} from '../utils/repo-files';
import {checkFiles, formatFiles} from './format';
@ -34,6 +34,12 @@ export function buildFormatParser(localYargs: yargs.Argv) {
const executionCmd = check ? checkFiles : formatFiles;
executionCmd(allChangedFilesSince(sha));
})
.command(
'staged', 'Run the formatter on all staged files', {},
({check}) => {
const executionCmd = check ? checkFiles : formatFiles;
executionCmd(allStagedFiles());
})
.command('files <files..>', 'Run the formatter on provided files', {}, ({check, files}) => {
const executionCmd = check ? checkFiles : formatFiles;
executionCmd(files);

View File

@ -27,6 +27,18 @@ export function allChangedFilesSince(sha = 'HEAD') {
return Array.from(new Set([...diffFiles, ...untrackedFiles]));
}
/**
* A list of all staged files which have been modified.
*
* Only added, created and modified files are listed as others (deleted, renamed, etc) aren't
* changed or available as content to act upon.
*/
export function allStagedFiles() {
return gitOutputAsArray(`git diff --staged --name-only --diff-filter=ACM`);
}
export function allFiles() {
return gitOutputAsArray(`git ls-files`);
}