angular-cn/dev-infra/utils/repo-files.ts
George Kalpakas d4c0962c7b refactor(dev-infra): use the exec() helper from utils/shelljs whenever possible (#37444)
There is an `exec()` helper provided by `utils/shelljs.ts`, which is a
wrapper around ShellJS' `exec()` with some default options (currently
`silent: true`). The intention is to avoid having to pass these options
to every invocation of the `exec()` function.

This commit updates all code inside `dev-infra/` to use this helper
whenever possible).

NOTE: For simplicity, the `utils/shelljs` helper does not support some
      of the less common call signatures of the original `exec()`
      helper, so in some cases we still need to use the original.

PR Close #37444
2020-06-05 09:21:18 -07:00

38 lines
1.2 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {getRepoBaseDir} from './config';
import {exec} from './shelljs';
/**
* A list of all files currently in the repo which have been modified since the provided sha.
*
* git diff
* Deleted files (--diff-filter=d) are not included as they are not longer present in the repo
* and can not be checked anymore.
*
* git ls-files
* Untracked files (--others), which are not matched by .gitignore (--exclude-standard)
* as they are expected to become tracked files.
*/
export function allChangedFilesSince(sha = 'HEAD') {
const diffFiles = gitOutputAsArray(`git diff --name-only --diff-filter=d ${sha}`);
const untrackedFiles = gitOutputAsArray(`git ls-files --others --exclude-standard`);
// Use a set to deduplicate the list as its possible for a file to show up in both lists.
return Array.from(new Set([...diffFiles, ...untrackedFiles]));
}
export function allFiles() {
return gitOutputAsArray(`git ls-files`);
}
function gitOutputAsArray(cmd: string) {
return exec(cmd, {cwd: getRepoBaseDir()}).split('\n').map(x => x.trim()).filter(x => !!x);
}