|
| 1 | +/* eslint-disable import/no-extraneous-dependencies,@typescript-eslint/camelcase, no-console */ |
| 2 | +import inquirer from 'inquirer'; |
| 3 | +import fs from 'fs'; |
| 4 | +import path from 'path'; |
| 5 | +import child_process from 'child_process'; |
| 6 | +import util from 'util'; |
| 7 | +import chalk from 'chalk'; |
| 8 | +import semverInc from 'semver/functions/inc'; |
| 9 | +import { ReleaseType } from 'semver'; |
| 10 | + |
| 11 | +import pkg from '../package.json'; |
| 12 | + |
| 13 | +const exec = util.promisify(child_process.exec); |
| 14 | + |
| 15 | +const run = async (command: string) => { |
| 16 | + console.log(chalk.green(command)); |
| 17 | + await exec(command); |
| 18 | +}; |
| 19 | + |
| 20 | +const currentVersion = pkg.version; |
| 21 | + |
| 22 | +const getNextVersions = (): { [key in ReleaseType]: string | null } => ({ |
| 23 | + major: semverInc(currentVersion, 'major'), |
| 24 | + minor: semverInc(currentVersion, 'minor'), |
| 25 | + patch: semverInc(currentVersion, 'patch'), |
| 26 | + premajor: semverInc(currentVersion, 'premajor'), |
| 27 | + preminor: semverInc(currentVersion, 'preminor'), |
| 28 | + prepatch: semverInc(currentVersion, 'prepatch'), |
| 29 | + prerelease: semverInc(currentVersion, 'prerelease'), |
| 30 | +}); |
| 31 | + |
| 32 | +const timeLog = (logInfo: string, type: 'start' | 'end') => { |
| 33 | + let info = ''; |
| 34 | + if (type === 'start') { |
| 35 | + info = `=> 开始任务:${logInfo}`; |
| 36 | + } else { |
| 37 | + info = `✨ 结束任务:${logInfo}`; |
| 38 | + } |
| 39 | + const nowDate = new Date(); |
| 40 | + console.log( |
| 41 | + `[${nowDate.toLocaleString()}.${nowDate |
| 42 | + .getMilliseconds() |
| 43 | + .toString() |
| 44 | + .padStart(3, '0')}] ${info} |
| 45 | + `, |
| 46 | + ); |
| 47 | +}; |
| 48 | + |
| 49 | +/** |
| 50 | + * 获取下一次版本号 |
| 51 | + */ |
| 52 | +async function prompt(): Promise<string> { |
| 53 | + const nextVersions = getNextVersions(); |
| 54 | + const { nextVersion } = await inquirer.prompt([ |
| 55 | + { |
| 56 | + type: 'list', |
| 57 | + name: 'nextVersion', |
| 58 | + message: `请选择将要发布的版本 (当前版本 ${currentVersion})`, |
| 59 | + choices: (Object.keys(nextVersions) as Array<ReleaseType>).map(level => ({ |
| 60 | + name: `${level} => ${nextVersions[level]}`, |
| 61 | + value: nextVersions[level], |
| 62 | + })), |
| 63 | + }, |
| 64 | + ]); |
| 65 | + return nextVersion; |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * 更新版本号 |
| 70 | + * @param nextVersion 新版本号 |
| 71 | + */ |
| 72 | +async function updateVersion(nextVersion: string) { |
| 73 | + pkg.version = nextVersion; |
| 74 | + timeLog('修改package.json版本号', 'start'); |
| 75 | + await fs.writeFileSync(path.resolve(__dirname, './../package.json'), JSON.stringify(pkg)); |
| 76 | + await run('npx prettier package.json --write'); |
| 77 | + timeLog('修改package.json版本号', 'end'); |
| 78 | +} |
| 79 | + |
| 80 | +async function generateChangelog() { |
| 81 | + timeLog('生成CHANGELOG.md', 'start'); |
| 82 | + await run(' npx conventional-changelog -p angular -i CHANGELOG.md -s -r 0'); |
| 83 | + timeLog('生成CHANGELOG.md', 'end'); |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * 将代码提交至git |
| 88 | + */ |
| 89 | +async function push(nextVersion: string) { |
| 90 | + timeLog('推送代码至git仓库', 'start'); |
| 91 | + await run('git add package.json CHANGELOG.md'); |
| 92 | + await run(`git commit -m "v${nextVersion}" -n`); |
| 93 | + await run('git push'); |
| 94 | + timeLog('推送代码至git仓库', 'end'); |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * 组件库打包 |
| 99 | + */ |
| 100 | +async function build() { |
| 101 | + timeLog('组件库打包', 'start'); |
| 102 | + await run('npm run build'); |
| 103 | + timeLog('组件库打包', 'end'); |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * 发布至npm |
| 108 | + */ |
| 109 | +async function publish() { |
| 110 | + timeLog('发布组件库', 'start'); |
| 111 | + await run('npm publish'); |
| 112 | + timeLog('发布组件库', 'end'); |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * 打tag提交至git |
| 117 | + */ |
| 118 | +async function tag(nextVersion: string) { |
| 119 | + timeLog('打tag并推送至git', 'start'); |
| 120 | + await run(`git tag v${nextVersion}`); |
| 121 | + await run(`git push origin tag v${nextVersion}`); |
| 122 | + timeLog('打tag并推送至git', 'end'); |
| 123 | +} |
| 124 | + |
| 125 | +async function main() { |
| 126 | + try { |
| 127 | + const nextVersion = await prompt(); |
| 128 | + const startTime = Date.now(); |
| 129 | + // =================== 更新版本号 =================== |
| 130 | + await updateVersion(nextVersion); |
| 131 | + // =================== 更新changelog =================== |
| 132 | + await generateChangelog(); |
| 133 | + // =================== 代码推送git仓库 =================== |
| 134 | + await push(nextVersion); |
| 135 | + // =================== 组件库打包 =================== |
| 136 | + await build(); |
| 137 | + // =================== 发布至npm =================== |
| 138 | + await publish(); |
| 139 | + // =================== 打tag并推送至git =================== |
| 140 | + await tag(nextVersion); |
| 141 | + console.log(`✨ 发布流程结束 共耗时${((Date.now() - startTime) / 1000).toFixed(3)}s`); |
| 142 | + } catch (error) { |
| 143 | + console.log('💣 发布失败,失败原因:', error); |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +main(); |
0 commit comments