Skip to content

Commit

Permalink
feat: add spawn for git
Browse files Browse the repository at this point in the history
  • Loading branch information
beetcb committed Jan 21, 2021
1 parent 2265e3c commit e71a581
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 8 deletions.
9 changes: 4 additions & 5 deletions convention.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@ const convention = {
areaDes: {
type: 'Add a <type>: a noun, feat, fix, etc.',
scope:
'Optionally add a (scope): describing a section of the codebase surrounded by parenthesis',
'Optionally Add a (scope): describing a section of the codebase surrounded by parenthesis',
des: 'Now add <description>: a short summary of the code changes',
body:
'Now optionally add [body]: providing additional contextual information',
footer: 'Now optionally add [footers]',
body: 'Optionally Add [body]: providing additional contextual information',
footer: 'Optionally add [footers]',
},
}

const { areaDes } = convention

Object.keys(areaDes).forEach(e => {
areaDes[e] = c`{cyan ${areaDes[e]}}`
areaDes[e] = c`\n{cyan ${areaDes[e]}}`
})

for (const i in convention) {
Expand Down
70 changes: 70 additions & 0 deletions git.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const spawnSync = require('child_process').spawnSync
const c = require('chalk')
const excute = (command, args) => {
const spawn = spawnSync(command, args.split(' '), { encoding: 'utf8' })
return spawn.stdout || spawn.stderr
}

const gh = args => excute('gh', args)
const git = args => excute('git', args)

exports.gitCommit = (mes, flags = '-m') => {
const commit = `commit ${flags} ${mes}`
console.log(mes)
return git(commit)
}

const ghIssues = num => {
const list = 'issue list --limit 10'
const view = `issue view ${num}`
return num ? praseString('list', gh(view)) : praseString('view', gh(list))
}

const ghPR = num => {
const list = 'pr list --limit 10'
const view = `pr view ${num}`
return num ? praseString('list', gh(view)) : praseString('view', gh(list))
}

const fetching = num => {
const fetch = ghIssues(num) || ghPR(num)
if (!fetch) {
return c`{red No Open Issues & PR found!}`
}
return fetch
}

exports.findIssuePR = repll => {
const input = repll.input
const match = input.match(/#(\d+)$/)
const num = match ? match[1] : null
if (num) {
repll.refresh(c`\n{yellow Fetching {green #${num}} Detailed Information}`)
return fetching(num)
}
if (/#$/.test(input)) {
repll.refresh(c`\n{yellow Fetching Your Issues & PR}`)
return fetching()
}

if (/#\d+\s$/.test(input)) return true
}

function praseString(type, str) {
if (!str) return str
if (type === 'view') {
const parts = str.split('\t')
if (parts.length > 1) return c`{green #${parts[0]}}: ${parts[2]}`
return str
}
if (type === 'list') {
const lines = str.split('\n')
if (lines.length > 1)
return lines.reduce(
(out, e) =>
out + c`{green ${e.split('\t')[0]}} ${e.split('\t')[1] || ''}\n`,
''
)
return str
}
}
21 changes: 18 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
const { replLive, onTab, onLine, onInput } = require('repll')
const { replLive, onTab, onLine, onInput, onStop, onSubmit } = require('../repll/index')
const c = require('chalk')
const { findIssuePR, gitCommit } = require('./git')
const { checkType, checkScope, checkDes } = require('./continuousCheck')
const { typeMap, areaDes } = require('./convention')
const { prompts, placeholder } = require('./repl')

const repll = replLive(prompts, placeholder[0])

let commitMes = ''

onLine(l => {
switch (l) {
case 2: {
Expand All @@ -17,9 +20,9 @@ onLine(l => {
break
}
case 4: {
const commit = repll.history.filter(e => e.length)
commitMes = repll.history.filter(e => e.length)
repll.refresh(
c`\n{yellow ${commit.join(
c`\n{yellow ${commitMes.join(
'\n\n'
)}\n\n}{red Press ctrl+d to commit it, ctrl+c to quit}`
)
Expand All @@ -29,6 +32,14 @@ onLine(l => {
if (placeholder[l - 1]) return placeholder[l - 1]
})

onStop(() => {
if (repll.inputLine > 1 && !repll.issued) {
const issuePR = findIssuePR(repll)
if (issuePR === true) repll.issued = true
else if (issuePR) repll.refresh(`\n${issuePR}`)
}
}, 0.5)

onInput(() => {
continuousCheck()
})
Expand All @@ -41,6 +52,10 @@ onTab(v => {
return repll.inputLine === 1 ? [selectedList, typeMap] : [[]]
})

onSubmit(() => {
gitCommit(process.argv[2], commitMes.join('\n\n'))
})

function printTips(name) {
repll.refresh(areaDes[name])
return false
Expand Down

0 comments on commit e71a581

Please sign in to comment.