Skip to content

Commit 28534d8

Browse files
josephperrottAndrewKushnir
authored andcommitted
feat(dev-infra): Add support for formatting all staged files (angular#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 angular#38402
1 parent a6292fa commit 28534d8

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

dev-infra/format/cli.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
import * as yargs from 'yargs';
99

10-
import {allChangedFilesSince, allFiles} from '../utils/repo-files';
10+
import {allChangedFilesSince, allFiles, allStagedFiles} from '../utils/repo-files';
1111

1212
import {checkFiles, formatFiles} from './format';
1313

@@ -34,6 +34,12 @@ export function buildFormatParser(localYargs: yargs.Argv) {
3434
const executionCmd = check ? checkFiles : formatFiles;
3535
executionCmd(allChangedFilesSince(sha));
3636
})
37+
.command(
38+
'staged', 'Run the formatter on all staged files', {},
39+
({check}) => {
40+
const executionCmd = check ? checkFiles : formatFiles;
41+
executionCmd(allStagedFiles());
42+
})
3743
.command('files <files..>', 'Run the formatter on provided files', {}, ({check, files}) => {
3844
const executionCmd = check ? checkFiles : formatFiles;
3945
executionCmd(files);

dev-infra/utils/repo-files.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ export function allChangedFilesSince(sha = 'HEAD') {
2727
return Array.from(new Set([...diffFiles, ...untrackedFiles]));
2828
}
2929

30+
/**
31+
* A list of all staged files which have been modified.
32+
*
33+
* Only added, created and modified files are listed as others (deleted, renamed, etc) aren't
34+
* changed or available as content to act upon.
35+
*/
36+
export function allStagedFiles() {
37+
return gitOutputAsArray(`git diff --staged --name-only --diff-filter=ACM`);
38+
}
39+
40+
41+
3042
export function allFiles() {
3143
return gitOutputAsArray(`git ls-files`);
3244
}

0 commit comments

Comments
 (0)