git-mikado/src/commands/new.js

62 lines
1.5 KiB
JavaScript

const Git = require('simple-git');
const inquirer = require('inquirer');
const report = require('yurnalist');
const _ = require('lodash');
async function checkWorkspaceClean() {
return Git().status().then((result) => {
if (!result.isClean()) throw new Error('workspace not clean, aborting');
})
}
async function currentBranch() {
let branch = await Git().raw('branch', '--show-current');
return branch.replace("\n",'');
}
async function getBaseBranch(branch) {
const base = await Git().getConfig(`branch.${branch}.mikado-base`);
if(base.value) return base.value;
// not in the config? Okay then, let's see all the
// options we have
const result = await Git().branch({'--merged': 'HEAD'});
const answer = await inquirer.prompt([
{
name: 'base',
message: 'base branch: ',
type: 'list',
choices: result.all,
}
]);
return answer.base;
}
module.exports = async (yargs) => {
await checkWorkspaceClean();
const {branch,upstream} = yargs;
const base = yargs.base ?? await getBaseBranch(upstream);
report.list('setup', ['branch','upstream','base'],{
branch,
upstream,
base,
});
report.info(`branching ${branch} off ${base}`);
await Git().checkoutBranch(branch,base);
await Git().addConfig( `branch.${branch}.mikado-base`, base );
await Git().addConfig( `branch.${branch}.mikado-upstream`, upstream );
report.success('done!');
}