diff --git a/lib/commands/common-options.js b/lib/commands/common-options.js index ba6c04b..691d4ac 100644 --- a/lib/commands/common-options.js +++ b/lib/commands/common-options.js @@ -171,3 +171,12 @@ module.exports.fileOptions = { describe: 'File containing JSON data', }, }; + +module.exports.pullOptions = { + warningUndoChangesAndCheckoutMain: { + type: 'boolean', + describe: 'Undo all uncommited changes and checkout main repo branch', + default: false, + group: 'Pull options:', + }, +}; diff --git a/lib/commands/platform/pull.js b/lib/commands/platform/pull.js index 4dc956e..563c1ed 100644 --- a/lib/commands/platform/pull.js +++ b/lib/commands/platform/pull.js @@ -4,18 +4,32 @@ const { contextMiddleware } = importLazy('../../cli/context-middleware'); const simpleGit = importLazy('simple-git'); const DevelopmentEnvironment = importLazy('../../environment/development'); +const { pullOptions } = require('../common-options'); + function isClean(status) { const check = ['conflicted', 'created', 'deleted', 'modified', 'renamed']; return check.reduce((sum, name) => sum + status[name].length, 0) === 0; } -function pullRepository(dir) { +function pullRepository(dir, undoChangesAndCheckoutMain) { const git = simpleGit(dir); // Rejects if repo is not is suitable for pulling const okToPull = () => new Promise((resolve, reject) => { git.status() - .then((status) => { + .then(async (status) => { + if (undoChangesAndCheckoutMain) { + await git.raw('add', '-A'); // add all changes, including new files, deleted files so they are tracked by git + await git.raw('reset', '--hard', 'HEAD'); // and undo them all muahahaha ψ(`∇´)ψ + try { + await git.checkout('main'); + } catch (e) {} // if we can't checkout main - then ignore the error and checkout master + + await git.checkout('master'); + + return resolve(status); + } + if (!isClean(status)) { reject(new Error('Branch contains changes.')); } @@ -85,7 +99,7 @@ function pullCommand(argv) { return new Promise((resolve) => { let pulls; moduleDirs.forEach((dir) => { - pulls = pulls ? pulls.then(() => pullRepository(dir)) : pullRepository(dir); + pulls = pulls ? pulls.then(() => pullRepository(dir, argv.warningUndoChangesAndCheckoutMain)) : pullRepository(dir, argv.warningUndoChangesAndCheckoutMain); }); pulls.then(() => { @@ -103,6 +117,7 @@ module.exports = { .middleware([ contextMiddleware(), ]) + .options(Object.assign({}), pullOptions) .example('$0 platform pull', 'Pull all clean repositories including aliases'); return yargs; },