git-checkout-interactive
Advanced tools
Comparing version 1.0.0 to 1.0.1
57
index.js
@@ -1,6 +0,9 @@ | ||
const { spawn } = require('child_process') | ||
#!/usr/bin/env node | ||
const { promisify } = require('util') | ||
const exec = promisify(require('child_process').exec) | ||
const prompts = require('prompts') | ||
async function run () { | ||
const branches = await cmd('git branch -v --sort=-committerdate') | ||
const { stdout: branches } = await exec('git branch -v --sort=-committerdate') | ||
@@ -11,44 +14,36 @@ const choices = branches | ||
.map(branch => { | ||
const [, flag, name, info] = branch.match(/([* ])\s+([^\s]+)\s+(.+)/) | ||
return { | ||
value: name, | ||
disabled: flag === '*', | ||
info | ||
} | ||
const [, flag, value, hint] = branch.match(/([* ]) +([^ ]+) +(.+)/) | ||
return { value, hint, disabled: flag === '*' } | ||
}) | ||
const commits = choices.reduce((object, { value, info }) => ({ | ||
...object, | ||
[value]: info | ||
}), {}) | ||
const { branch } = await prompts({ | ||
type: 'select', | ||
name: 'branch', | ||
message: 'Select a branch', | ||
message: 'Switch branch', | ||
choices, | ||
initial: 0, | ||
hint: commits[choices[0].value], | ||
warn: 'Current branch', | ||
hint: choices[0].hint, | ||
warn: 'current branch', | ||
onState ({ value }) { | ||
this.hint = commits[value] | ||
this.hint = choices.find(c => c.value === value).hint | ||
} | ||
}) | ||
await cmd(`git checkout ${branch}`) | ||
await checkout(branch) | ||
} | ||
function cmd (string) { | ||
const [ cmd, ...args ] = string.split(' ') | ||
return new Promise((resolve, reject) => { | ||
const child = spawn(cmd, args) | ||
let data = '' | ||
child.stdout.on('data', buffer => { | ||
data += buffer.toString() | ||
}) | ||
child.stdout.on('end', () => resolve(data)) | ||
child.on('error', reject) | ||
}) | ||
async function checkout (branch) { | ||
if (!branch) return | ||
const { stdout, stderr } = await exec(`git checkout ${branch}`) | ||
process.stdout.write(stdout) | ||
process.stderr.write(stderr) | ||
} | ||
run() | ||
function onError (e) { | ||
if (e.stderr) { | ||
process.stderr.write(e.stderr) | ||
} else { | ||
console.error(e) | ||
} | ||
} | ||
run().catch(onError) |
{ | ||
"name": "git-checkout-interactive", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Quick switch between git branches", | ||
@@ -5,0 +5,0 @@ "author": "Pete Cook", |
1722
40