Skip to content

Commit

Permalink
adding a force-latest option
Browse files Browse the repository at this point in the history
  • Loading branch information
machellerogden committed Feb 24, 2017
1 parent ad4708c commit df75af5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
23 changes: 22 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ function cli(options) {

if (_.isString(options.completions) || _.isString(options.open)) {
searchTerm = options.completions || options.open;
} else if (options['clone-all']) {
} else if (options['clone-all'] || options['force-latest']) {
if (_.isString(options['clone-all'])) {
searchTerm = options['clone-all'];
} else if (_.isString(options['force-latest'])) {
searchTerm = options['force-latest'];
} else {
searchTerm = '.*';
}
Expand Down Expand Up @@ -66,6 +68,25 @@ function cli(options) {
}
});
});
} else if (results.length && options['force-latest']) {
return Promise.all(_.map(results, lib.getSourceResult)).then((repos) => {
return inquirer.prompt([
{
type: 'confirm',
name: 'confirm',
message: 'Are you sure you want to force a hard reset to latest origin/master for ' + repos.length + ' repos?',
default: false
}
]).then((answers) => {
if (answers.confirm) {
return Promise.resolve(repos).map(lib.maybeClone).mapSeries(lib.forceLatest).then(() => {
return { repoDir: workspaceDir };
});
} else {
throw new Error('Force Latest Aborted.');
}
});
});
} else if (results.length > 1) {
resultPromise = inquirer.prompt([
{
Expand Down
32 changes: 32 additions & 0 deletions lib/forceLatest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';
const fs = require('fs');
const path = require('path');
const Promise = require('bluebird');
const workspaceDir = require('./workspaceDir');
let git;

module.exports = forceLatest;

function forceLatest(sourceResult) {
return new Promise((resolve, reject) => {
if (fs.existsSync(sourceResult.repoDir)) {
git = require('simple-git')(sourceResult.repoDir)
.outputHandler(function (command, stdout, stderr) {
stdout.pipe(process.stdout);
stderr.pipe(process.stderr);
});
return git.fetch([ 'origin' ], (fetchErr) => {
if (fetchErr) console.log('Error trying to fetch ' + sourceResult.clone + '. Message: ' + fetchErr);
git.reset([ '--hard', 'origin/master' ], (resetErr) => {
if (resetErr) console.log('Error trying to reset hard to origin/master for ' + sourceResult.clone + '. Message: ' + resetErr);
return git.clean('f', [ '-d' ], (cleanErr) => {
if (cleanErr) return reject(cleanErr);
return resolve(sourceResult);
});
});
});
} else {
return resolve(sourceResult);
}
});
}
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports.getPaths = require('./getPaths');
exports.getSourceData = require('./getSourceData');
exports.getSourceResult = require('./getSourceResult');
exports.maybeClone = require('./maybeClone');
exports.forceLatest = require('./forceLatest');
exports.openInBrowser = require('./openInBrowser');
exports.workspaceDir = require('./workspaceDir');
exports.setTarget = require('./setTarget');

0 comments on commit df75af5

Please sign in to comment.