Skip to content

Commit

Permalink
Merge pull request #6 from davina18/fix-staged-detection
Browse files Browse the repository at this point in the history
fix(prompt): only show packages that have staged changes.
  • Loading branch information
bradleyayers authored Dec 5, 2016
2 parents 8b69b22 + 64397c2 commit f543360
Showing 1 changed file with 19 additions and 27 deletions.
46 changes: 19 additions & 27 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,23 @@ function getAllPackages () {
return PackageUtilities.getPackages(packagesLocation);
}

function isStatusStaged (statusLine) {
const modifiedAddedDeletedRenamedCopied = 'MADRC'; // see git status --help (short output format)
const status = statusLine.split(' ');
const stagedStatus = status[0];
return modifiedAddedDeletedRenamedCopied.indexOf(stagedStatus) !== -1;
}

function isFileStaged (status, file) {
const stagedChanges = status.split('\n').filter(isStatusStaged);
return stagedChanges.some(function (stagedChange) {
return stagedChange.indexOf(file) !== -1;
});
}

function getChangedComponents () {
let changedComponents = [];
const status = shell.exec('git status . --short', {silent: true}).stdout;

getAllPackages().forEach(function (pkg) {
if (isFileStaged(status, path.relative('.', pkg.location))) {
changedComponents.push(pkg.name);
}
});

return changedComponents;
function getChangedPackages () {
const changedFiles = shell.exec('git diff --cached --name-only', {silent: true})
.stdout
.split('\n');

return getAllPackages()
.filter(function (pkg) {
const packagePrefix = path.relative('.', pkg.location) + path.sep;
for (let changedFile of changedFiles) {
if (changedFile.indexOf(packagePrefix) === 0) {
return true;
}
}
})
.map(function (pkg) {
return pkg.name
});
}

module.exports = {
Expand All @@ -56,9 +48,9 @@ module.exports = {
cz.prompt({
type: 'checkbox',
name: 'packages',
'default': getChangedComponents(),
'default': getChangedPackages(),
choices: allPackages,
message: `The packages that this commit has affected (${getChangedComponents().length} detected)\n`,
message: `The packages that this commit has affected (${getChangedPackages().length} detected)\n`,
validate: function (input) {
const type = conventionalAnswers.type;
const isRequired = ['feat', 'fix'].indexOf(type) > -1;
Expand Down

0 comments on commit f543360

Please sign in to comment.