refactor(dev-infra): move getRepoBaseDir() to GitClient (#41527)

As `getRepoBaseDir()` relies on git, it should be a method on `GitClient` for retrieval
rather than its own utility outside of the common GitClient used for all git ineractions.

PR Close #41527
This commit is contained in:
Joey Perrott
2021-04-12 14:50:50 -07:00
committed by Andrew Kushnir
parent 5332a4a919
commit dd4c3dba3f
18 changed files with 882 additions and 438 deletions
+7 -5
View File
@@ -6,8 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import * as yargs from 'yargs';
import {allChangedFilesSince, allFiles, allStagedFiles} from '../utils/repo-files';
import {GitClient} from '../utils/git/index';
import {checkFiles, formatFiles} from './format';
@@ -25,7 +24,8 @@ export function buildFormatParser(localYargs: yargs.Argv) {
'all', 'Run the formatter on all files in the repository', args => args,
({check}) => {
const executionCmd = check ? checkFiles : formatFiles;
executionCmd(allFiles());
const allFiles = GitClient.getInstance().allFiles();
executionCmd(allFiles);
})
.command(
'changed [shaOrRef]', 'Run the formatter on files changed since the provided sha/ref',
@@ -33,13 +33,15 @@ export function buildFormatParser(localYargs: yargs.Argv) {
({shaOrRef, check}) => {
const sha = shaOrRef || 'master';
const executionCmd = check ? checkFiles : formatFiles;
executionCmd(allChangedFilesSince(sha));
const allChangedFilesSince = GitClient.getInstance().allChangesFilesSince(sha);
executionCmd(allChangedFilesSince);
})
.command(
'staged', 'Run the formatter on all staged files', args => args,
({check}) => {
const executionCmd = check ? checkFiles : formatFiles;
executionCmd(allStagedFiles());
const allStagedFiles = GitClient.getInstance().allStagedFiles();
executionCmd(allStagedFiles);
})
.command(
'files <files..>', 'Run the formatter on provided files',
@@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {GitClient} from '../../utils/git/index';
import {FormatConfig} from '../config';
// A callback to determine if the formatter run found a failure in formatting.
@@ -24,6 +25,7 @@ interface FormatterActionMetadata {
* The base class for formatters to run against provided files.
*/
export abstract class Formatter {
protected git = GitClient.getInstance();
/**
* The name of the formatter, this is used for identification in logging and for enabling and
* configuring the formatter in the config.
+1 -2
View File
@@ -8,7 +8,6 @@
import {join} from 'path';
import {getRepoBaseDir} from '../../utils/config';
import {error} from '../../utils/console';
import {Formatter} from './base-formatter';
@@ -19,7 +18,7 @@ import {Formatter} from './base-formatter';
export class Buildifier extends Formatter {
name = 'buildifier';
binaryFilePath = join(getRepoBaseDir(), 'node_modules/.bin/buildifier');
binaryFilePath = join(this.git.baseDir, 'node_modules/.bin/buildifier');
defaultFileMatcher = ['**/*.bzl', '**/BUILD.bazel', '**/WORKSPACE', '**/BUILD'];
+1 -2
View File
@@ -8,7 +8,6 @@
import {join} from 'path';
import {getRepoBaseDir} from '../../utils/config';
import {error} from '../../utils/console';
import {Formatter} from './base-formatter';
@@ -19,7 +18,7 @@ import {Formatter} from './base-formatter';
export class ClangFormat extends Formatter {
name = 'clang-format';
binaryFilePath = join(getRepoBaseDir(), 'node_modules/.bin/clang-format');
binaryFilePath = join(this.git.baseDir, 'node_modules/.bin/clang-format');
defaultFileMatcher = ['**/*.{t,j}s'];