axios-miniprogram/scripts/release.ts

123 lines
2.8 KiB
TypeScript
Raw Normal View History

2023-04-19 21:36:06 +08:00
import { writeFileSync } from 'node:fs';
2023-03-23 20:09:00 +08:00
import semver from 'semver';
import enquirer from 'enquirer';
import consola from 'consola';
2023-04-19 21:36:06 +08:00
import { exec, pkgPath, getPkgJSON, resolve } from './utils';
2023-03-23 20:09:00 +08:00
const pkg = getPkgJSON();
2023-04-19 21:36:06 +08:00
const versionTSPath = resolve('src/version.ts');
2023-03-23 20:09:00 +08:00
const { version: currentVersion } = pkg;
2023-03-24 19:41:17 +08:00
main().catch((err) => exit(err.message));
2023-03-23 20:09:00 +08:00
async function main() {
checkBranch();
const version = await inputVersion();
consola.info(`Update version ${currentVersion} -> ${version}`);
updateVersion(version);
2023-03-24 21:45:00 +08:00
consola.info('Changelog');
exec('pnpm changelog');
2023-03-23 20:09:00 +08:00
consola.info('Git add');
exec('git add .');
2023-04-05 09:29:20 +08:00
exec(`git commit -m "chore: release v${version}"`);
exec(`git tag -a v${version} -m "v${version}"`);
2023-03-23 20:09:00 +08:00
consola.info('Git push');
exec('git push');
consola.info('Git push tag');
exec(`git push origin v${version}`);
}
function checkBranch() {
const releaseBranch = 'main';
const currentBranch = exec('git branch --show-current', {
stdio: 'pipe',
2023-03-24 19:41:17 +08:00
})
.toString()
.trim();
2023-03-23 20:09:00 +08:00
if (currentBranch !== releaseBranch) {
2023-03-24 19:41:17 +08:00
exit(`请切回 ${releaseBranch} 分支进行发版!`);
2023-03-23 20:09:00 +08:00
}
}
async function inputVersion() {
const releases = createReleases();
let targetVersion: string;
const { release } = await enquirer.prompt<{ release: string }>({
type: 'select',
name: 'release',
message: '请选择版本类型',
choices: [...releases, 'custom'],
});
if (release === 'custom') {
const { version } = await enquirer.prompt<{
version: string;
}>({
type: 'input',
name: 'version',
message: '请输入自定义版本号',
initial: currentVersion,
});
targetVersion = version;
} else {
targetVersion = (release.match(/\((.+)\)/) as string[])[1];
}
if (
!semver.valid(targetVersion) ||
!semver.lt(currentVersion, targetVersion)
) {
2023-03-24 19:41:17 +08:00
exit(`无效的版本号: ${targetVersion}`);
2023-03-23 20:09:00 +08:00
}
const { yes: confirmRelease } = await enquirer.prompt<{ yes: boolean }>({
type: 'confirm',
name: 'yes',
2023-03-25 14:16:10 +08:00
message: `确定发版: v${targetVersion} `,
2023-03-23 20:09:00 +08:00
});
if (!confirmRelease) {
2023-03-25 14:16:10 +08:00
exit(`取消发版: v${targetVersion}`);
2023-03-23 20:09:00 +08:00
}
return targetVersion;
}
function createReleases() {
const types = [
['patch'],
['minor'],
['major'],
2023-04-04 10:40:28 +08:00
['prepatch', 'beta'],
['preminor', 'beta'],
['premajor', 'beta'],
['prerelease', 'beta'],
2023-03-23 20:09:00 +08:00
];
const releases: string[] = [];
for (const [type, preid] of types) {
releases.push(`${type} (${semver.inc(currentVersion, type, preid)})`);
}
return releases;
}
2023-03-24 19:41:17 +08:00
function exit(msg: string) {
updateVersion(currentVersion);
consola.error(msg || '已退出');
process.exit();
}
2023-03-23 20:09:00 +08:00
function updateVersion(version: string) {
pkg.version = version;
2023-04-19 21:36:06 +08:00
writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n');
2023-04-21 23:39:33 +08:00
writeFileSync(versionTSPath, `export const version = '${version}';`);
2023-03-23 20:09:00 +08:00
}