Skip to content

Commit

Permalink
chore: add robot script
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeJim committed Mar 31, 2022
1 parent 147c6fc commit 61dce61
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ docClass: timeline
- Swiper: 修复延迟设置地址时,显示不正常的问题 [#305](https://github.com/Tencent/tdesign-miniprogram/pull/305) [@esky](https://github.com/esky)
- Button: 修复文案没有垂直居中的问题 [#311](https://github.com/Tencent/tdesign-miniprogram/pull/311) [@anlyyao](https://github.com/anlyyao)

### Feature
### Features

- Fab: 新增支持悬浮按钮 [#310](https://github.com/Tencent/tdesign-miniprogram/pull/310) [@LeeJim](https://github.com/LeeJim)
- Drawer: 新增支持抽屉 [#308](https://github.com/Tencent/tdesign-miniprogram/pull/308) [@anlyyao](https://github.com/anlyyao)
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
"test:unit": "jest -c jest.unit.config.js",
"test:e2e": "jest -c jest.e2e.config.js",
"prepare": "husky install",
"generate": "gulp generate --gulpfile script/gulpfile.js --cwd ./"
"generate": "gulp generate --gulpfile script/gulpfile.js --cwd ./",
"changelog": "node script/generate-changelog.js",
"robot": "publish-cli robot-msg"
},
"author": "tdesign",
"license": "MIT",
Expand Down Expand Up @@ -79,7 +81,9 @@
"playwright": "^1.19.1",
"prettier": "^2.0.5",
"prismjs": "^1.24.1",
"standard-changelog": "^2.0.27",
"stylelint": "^13.13.1",
"tdesign-publish-cli": "^0.0.9",
"tdesign-site-components": "^0.6.1",
"typescript": "^4.5.2",
"vite": "^2.7.6",
Expand Down
86 changes: 86 additions & 0 deletions script/generate-changelog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* eslint-disable */
const { execSync } = require('child_process');
const fs = require('fs');
const readline = require('readline');
const standardChangelog = require('standard-changelog');
const pkg = require('../package.json');

const VERSION_REG = /\d+\.\d+\.\d+/;

function updateVersion() {
return new Promise((resolve) => {
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });

rl.setPrompt(`当前 package.json 版本号为: ${pkg.version}\n请输入本次要发布的版本号:(可按回车跳过)\n`);
rl.prompt();

// eslint-disable-next-line consistent-return
rl.on('line', (input) => {
let newVersion = '';
if (!input) {
newVersion = pkg.version.replace(/(\d+\.\d+\.)(\d+)/, (verion, $1, $2) => $1 + (Number($2) + 1));
} else if (!VERSION_REG.test(input)) {
console.log('\x1B[31m%s\x1B[0m', '\n⚡ 不要搞事年轻人,请输入正确版本号格式!\n');
rl.prompt();
return;
} else {
newVersion = input;
}
const newPkg = JSON.stringify(Object.assign({}, pkg, { version: newVersion }), null, 2);
fs.writeFileSync('package.json', `${newPkg}\n`, 'utf8');
console.log('\x1B[32m%s\x1B[0m', '\n🎉 good job! package.json 文件已更新.\n');
rl.close();
});

rl.on('close', resolve);
});
}

function getLastChangeLogCommit() {
const gitCommand = 'git blame CHANGELOG.md';
const changeLogCommits = execSync(gitCommand, {
cwd: process.cwd(),
encoding: 'utf-8',
}).split('\n');

return changeLogCommits.find((cmt) => VERSION_REG.test(cmt)).slice(0, 8);
}

function getGitCommitMap(lastCommit) {
const gitCommand = `git log --pretty=format:"%H:%cn" ${lastCommit}..HEAD`;
const gitLogMap = execSync(gitCommand, { cwd: process.cwd(), encoding: 'utf-8' }).toString();
fs.writeFileSync('.gitlogmap', gitLogMap, 'utf8');
}

async function updateChangeLog() {
await updateVersion();

console.log('\x1B[32m%s\x1B[0m', '正在生成 changeLog... \n');

const lastCommit = getLastChangeLogCommit();
let initialChangelogStr = fs.readFileSync('CHANGELOG.md', 'utf8');

const pageDataStr = initialChangelogStr.match(/---[\s\S]+---/)[0] + '\n';
const data = initialChangelogStr.split(/---[\s\S]+---/);
data.unshift(pageDataStr);

new Promise((resolve) => {
standardChangelog({}, null, { from: lastCommit, to: 'HEAD' })
.on('data', (chunk) => {
let changeLogStr = chunk.toString().trim();
changeLogStr = changeLogStr.replace(/\(([\d\-]+)\)/g, '`$1`');
changeLogStr = changeLogStr.replace(/^#\s/g, '## ').trim();
data.splice(1, 0, `${changeLogStr}\n`);
})
.on('end', resolve);
}).then(() => {
getGitCommitMap(lastCommit);
const writeStream = fs.createWriteStream('CHANGELOG.md', 'utf8');
writeStream.write(data.join('\n'));
writeStream.end();

console.log('\x1B[32m%s\x1B[0m', '已生成最新 changeLog... 请打开 CHANGELOG.md 确认');
});
}

updateChangeLog();

0 comments on commit 61dce61

Please sign in to comment.