| 23 | } |
| 24 | |
| 25 | function main() { |
| 26 | return new Promise((resolve, reject) => { |
| 27 | // 获取commit消息,并去除结尾的换行符 |
| 28 | const msg = readFileSync(process.env.GIT_PARAMS, 'utf-8').replace(/(\r|\n)$/, '') |
| 29 | |
| 30 | const stdout = execSync('git branch --contains=HEAD').toString() |
| 31 | const branch = stdout.match(/^\*? +([^\s]+)/, '')[1] |
| 32 | const mergeReg = new RegExp(`Merge branch '.+' into ${branch}`, 'g') |
| 33 | if (msg.match(mergeReg)) return Promise.resolve() |
| 34 | |
| 35 | const branchName = execSync('git branch --show-current').toString() |
| 36 | const ticketNo = branchName ? `${branchName.replace(/\r?\n/g, '')}`.split('/') : [''] |
| 37 | // Ctrl+C中断事件处理 |
| 38 | const promptModule = createPromptModule() |
| 39 | const ui = new Prompt(promptModule.prompts, {}) |
| 40 | const handleCtrlC = () => { |
| 41 | ui.rl.off('SIGINT', handleCtrlC) |
| 42 | ui.close() |
| 43 | reject(new Error('Commit处理已终止', ui)) |
| 44 | } |
| 45 | ui.rl.on('SIGINT', handleCtrlC) |
| 46 | |
| 47 | const stores = [ |
| 48 | { |
| 49 | type: 'message', |
| 50 | name: 'branch', |
| 51 | message: '请确定分支名称', |
| 52 | default: ticketNo[ticketNo.length - 1], |
| 53 | }, |
| 54 | { |
| 55 | type: 'list', |
| 56 | name: 'commitType', |
| 57 | message: '请选择提交的分类', |
| 58 | choices: Object.keys(COMMIT_TYPES), |
| 59 | }, |
| 60 | { |
| 61 | name: 'message', |
| 62 | message: '消息确认(可以在此重新输入新的内容)', |
| 63 | default: msg, |
| 64 | }, |
| 65 | ] |
| 66 | |
| 67 | ui.run(stores) |
| 68 | .then(({ branch, commitType, message }) => { |
| 69 | writeFileSync(process.env.GIT_PARAMS, `${COMMIT_TYPES[commitType]}: ${message}(${branch})`) |
| 70 | resolve() |
| 71 | }) |
| 72 | .catch(reject) |
| 73 | }) |
| 74 | } |
| 75 | |
| 76 | main() |