git branch --auto

node v6.17.1
version: master
endpointsharetweet
///////////////////////////////////////// // Logging, Deforestation, & Prompting // ///////////////////////////////////////// try { const chalk = require('chalk'); const fake = true; if (fake) { chalk.enabled = true; } class Interactor { constructor(input=process.stdin, output=process.stdout) { this._input = input; this._output = output; const readline = require('readline'); this.rl = readline.createInterface({ input, output, prompt: '', }); } log(message) { if (fake) { const a2h = new (require('ansi-to-html'))({fg: '#000', bg: '#fff', newline: true}); console.log(`<code>${a2h.toHtml(message)}</code>`); } else { this._output.write(message); } } formatBranch(...args) { return chalk.bold.underline(...args); } fail(what, err) { this.log(`${chalk.red('✗')} Failed to ${what}!\n${chalk.bold.red(err)}`); } ok(what, out) { this.log(`${chalk.green('✓')} ${what}.${out ? '\n' + chalk.bold.green(out) : ''}`) } ask(query) { query = `${chalk.blue.bold('?')} ${query}`; if (fake) { return Promise.resolve('feat'); } else { return new Promise(resolve => rl.question(query, resolve)) } } } global.interactor = new Interactor; }catch(e){console.log(e,e.stack)}
/////////// // GIT() // /////////// function streamToString(stream) { // FROM http://stackoverflow.com/a/32565479/5244995 return new Promise((resolve, reject) => { const chunks = []; stream.on('data', (chunk) => { chunks.push(chunk.toString()); }); stream.on('end', () => { resolve(chunks.join('')); }); stream.on('error', reject); }); } const git = (() => { const { spawn } = require('child_process'); return async (...args) => { const proc = spawn('git', args); const stdout = streamToString(proc.stdout); const stderr = streamToString(proc.stderr); proc.on('exit', (code, signal) => { if (code || signal) { const err = await(stderr); reject(code || signal, err); } else { resove(await(stdout)); } }); } })();
/////////////// // BRANCHING // /////////////// try { global.getBranch = async what => { try { return await git('branch', '--list'); } catch (e) { interactor.fail(what, e); } }; global.BRANCHES = { master: 'master', develop: 'dev', releasePrefix: 'release-', hotfixPrefix: 'hotfix-', }; BRANCHES.featurePrefix = BRANCHES.develop + '-'; global.type = name => { if (name === BRANCHES.master) { return 'master'; } else if (name === BRANCHES.develop) { return 'develop'; } else if (name.startsWith(BRANCHES.featurePrefix)) { return 'feature'; } else if (name.startsWith(BRANCHES.releasePrefix)) { return 'release'; } else if (name.startsWith(BRANCHES.hotfixPrefix)) { return 'hotfix'; } } }catch(e){console.log(e,e.stack)}
try{ ////////////// // Commands // ////////////// const makeFeature = async () => { const branch = await ask("What is the name of the feature?"); try { await git('checkout', BRANCHES.dev); await git('checkout', '-b', branch); interactor.ok(`Switched to the new feature branch ${interactor.formatBranch(branch)}`); } catch(e) { interactor.fail('create the branch ' + interactor.formatBranch(branch), e.stderr) } }; const saveFeature = async () => { try { let branch = await getBranch('save the branch'); if (type(branch) == 'feature') { interactor.ok(`Current branch is a feature branch: ${interactor.formatBranch(branch)}`); } else { throw `Current branch is not a feature branch: ${interactor.formatBranch(branch)}`; } git('checkout', BRANCHES.develop) // TODO: // git checkout ${BRANCHES.dev} // git merge --no-ff ${branch} // (always keep a record that this branch existed) // git branch -d ${branch} } catch(e) { interactor.fail('save the branch', e.stderr || e); } }; await saveFeature(); }catch(e){console.log(e,e.stack)}
Loading…

no comments

    sign in to comment