diff --git a/cli.js b/cli.js index e4f351f..1638db1 100644 --- a/cli.js +++ b/cli.js @@ -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 = '.*'; } @@ -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([ { diff --git a/lib/forceLatest.js b/lib/forceLatest.js new file mode 100644 index 0000000..cd4bf1a --- /dev/null +++ b/lib/forceLatest.js @@ -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); + } + }); +} diff --git a/lib/index.js b/lib/index.js index 7c0f6d6..6650894 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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');