Skip to content

Commit

Permalink
feat(schematics): affected support for uncommitted changes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
markphip authored and vsavkin committed Mar 27, 2018
1 parent cd8a0fe commit 113b51b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/bazel/src/utils/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class FormatFiles implements TaskConfigurationGenerator<any> {
return {
name: 'node-package',
options: {
command: 'run format',
command: 'run format -- --untracked',
quiet: true
}
};
Expand Down
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 @@ -38,6 +38,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
27 changes: 26 additions & 1 deletion packages/schematics/src/command-line/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,25 @@ export function parseFiles(
});

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) {
named.splice(named.indexOf(dashDashFiles), 1);
return {
files: parseDashDashFiles(dashDashFiles),
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) {
return {
files: getFilesFromShash(unnamed[0], unnamed[1]),
Expand All @@ -53,8 +66,20 @@ function parseDashDashFiles(dashDashFiles: string): string[] {
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[] {
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')
.split('\n')
.map(a => a.trim())
Expand Down
2 changes: 1 addition & 1 deletion packages/schematics/src/utils/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class FormatFiles implements TaskConfigurationGenerator<any> {
return {
name: 'node-package',
options: {
command: 'run format',
command: 'run format -- --untracked',
quiet: true
}
};
Expand Down

0 comments on commit 113b51b

Please sign in to comment.