平时改完bug重新部署测试环境,总要重新打tag,虽然也就几行git命令的事,但能用一句命令解决还是舒服的
在scripts新建文件 generateTag.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
const { execSync } = require('child_process'); const readline = require('readline');
function getCurrentTag() { const now = new Date(); const pad = n => n < 10 ? '0' + n : n; return [ now.getFullYear(), pad(now.getMonth() + 1), pad(now.getDate()), pad(now.getHours()), pad(now.getMinutes()), pad(now.getSeconds()) ].join(''); }
try { const branch = execSync('git rev-parse --abbrev-ref HEAD').toString().trim(); const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
rl.question(`当前分支为 "${branch}",确定要在该分支上打 tag 并推送吗?(y/N): `, (answer) => { if (answer.toLowerCase() !== 'y') { console.log('操作已取消。'); rl.close(); process.exit(0); } try { const tag = getCurrentTag(); execSync(`git tag ${tag}`, { stdio: 'inherit' }); execSync(`git push origin ${tag}`, { stdio: 'inherit' }); console.log(`Tag ${tag} 已创建并推送到远端`); } catch (err) { console.error('创建或推送 tag 失败:', err.message); process.exit(1); } rl.close(); process.exit(0); }); return; } catch (err) { console.error('创建或推送 tag 失败:', err.message); process.exit(1); }
|
在package.json中的’scripts’添加
1
| "tag": "node ./scripts/generateTag"
|
使用