feat(schematics): affected support for uncommitted changes

Extended the syntax for affected so that it can get the
list of files from either locally staged changes or all
modified but unstaged files.  Syntax is:

nx affected apps staged
nx affected apps unstaged

This could enable developer to more easily run a subset
of tests locally before committing.

Related to #201
This commit is contained in:
Mark Phippard 2018-03-27 10:45:49 -04:00 committed by Victor Savkin
parent cd8a0feb24
commit 113b51bd33
4 changed files with 31 additions and 3 deletions

View File

@ -10,7 +10,7 @@ export class FormatFiles implements TaskConfigurationGenerator<any> {
return { return {
name: 'node-package', name: 'node-package',
options: { options: {
command: 'run format', command: 'run format -- --untracked',
quiet: true quiet: true
} }
}; };

View File

@ -38,6 +38,9 @@ function printError(command: string, e: any) {
console.error( console.error(
`Or pass the list of files, as follows: npm run affected:${command} -- --files="libs/mylib/index.ts,libs/mylib2/index.ts".` `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); console.error(e.message);
} }

View File

@ -29,12 +29,25 @@ export function parseFiles(
}); });
const dashDashFiles = named.filter(a => a.startsWith('--files='))[0]; const dashDashFiles = named.filter(a => a.startsWith('--files='))[0];
const uncommitted = named.some(a => a === '--uncommitted');
const untracked = named.some(a => a === '--untracked');
if (dashDashFiles) { if (dashDashFiles) {
named.splice(named.indexOf(dashDashFiles), 1); named.splice(named.indexOf(dashDashFiles), 1);
return { return {
files: parseDashDashFiles(dashDashFiles), files: parseDashDashFiles(dashDashFiles),
rest: [...unnamed, ...named] rest: [...unnamed, ...named]
}; };
} else if (uncommitted) {
return {
files: getUncommittedFiles(),
rest: [...unnamed, ...named]
};
} else if (untracked) {
return {
files: getUntrackedFiles(),
rest: [...unnamed, ...named]
};
} else if (unnamed.length >= 2) { } else if (unnamed.length >= 2) {
return { return {
files: getFilesFromShash(unnamed[0], unnamed[1]), files: getFilesFromShash(unnamed[0], unnamed[1]),
@ -53,8 +66,20 @@ function parseDashDashFiles(dashDashFiles: string): string[] {
return f.split(',').map(f => f.trim()); return f.split(',').map(f => f.trim());
} }
function getUncommittedFiles(): string[] {
return parseGitOutput(`git diff --name-only HEAD .`);
}
function getUntrackedFiles(): string[] {
return parseGitOutput(`git ls-files --others --exclude-standard`);
}
function getFilesFromShash(sha1: string, sha2: string): string[] { function getFilesFromShash(sha1: string, sha2: string): string[] {
return execSync(`git diff --name-only ${sha1} ${sha2}`) return parseGitOutput(`git diff --name-only ${sha1} ${sha2}`);
}
function parseGitOutput(command: string): string[] {
return execSync(command)
.toString('utf-8') .toString('utf-8')
.split('\n') .split('\n')
.map(a => a.trim()) .map(a => a.trim())

View File

@ -10,7 +10,7 @@ export class FormatFiles implements TaskConfigurationGenerator<any> {
return { return {
name: 'node-package', name: 'node-package',
options: { options: {
command: 'run format', command: 'run format -- --untracked',
quiet: true quiet: true
} }
}; };