Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(schematics): affected support for uncommitted changes #351

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/schematics/src/command-line/affected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ function printError(command: string, e: any) {
console.error(
`Or pass the list of files, as follows: npm run affected:${command} -- --files="libs/mylib/index.ts,libs/mylib2/index.ts".`
);
console.error(
`Or to get the list of files from staged or unstaged changes: npm run affected:${command} -- staged | unstaged".`
);
console.error(e.message);
}

Expand Down
21 changes: 21 additions & 0 deletions packages/schematics/src/command-line/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ export function parseFiles(
files: parseDashDashFiles(dashDashFiles),
rest: [...unnamed, ...named]
};
} else if (unnamed.length >= 1 && unnamed[0].includes('staged')) {
return {
files: getFilesFromLocalChanges(unnamed[0].startsWith('staged')),
rest: [...unnamed.slice(1), ...named]
};
} else if (unnamed.length >= 2) {
return {
files: getFilesFromShash(unnamed[0], unnamed[1]),
Expand All @@ -51,6 +56,22 @@ function parseDashDashFiles(dashDashFiles: string): string[] {
return f.split(',').map(f => f.trim());
}

function getFilesFromLocalChanges(staged: boolean): string[] {
if (staged) {
return execSync(`git diff --name-only --cached`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some code duplication. Maybe create a getFilesNamesFromGitOutput helper?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better yet, simple-git handles this more nicely. It gives nice JSON format results from git commands. https://github.com/steveukx/git-js. If we want to bring in that dependency

.toString('utf-8')
.split('\n')
.map(a => a.trim())
.filter(a => a.length > 0);
} else {
return execSync(`git diff --name-only`)
.toString('utf-8')
.split('\n')
.map(a => a.trim())
.filter(a => a.length > 0);
}
}

function getFilesFromShash(sha1: string, sha2: string): string[] {
return execSync(`git diff --name-only ${sha1} ${sha2}`)
.toString('utf-8')
Expand Down