Skip to content

Commit

Permalink
新增pr工作流
Browse files Browse the repository at this point in the history
  • Loading branch information
sj817 committed Nov 25, 2024
1 parent f7e6598 commit d060e4f
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 16 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: PR

# 监听 PR 相关事件
on:
pull_request:
# 监听 PR 被打开、重新打开和推送事件
types: [opened, reopened, synchronize]

# 赋予 release-please-action 权限
permissions:
contents: write
pull-requests: write
jobs:
# 设置 release-please 任务
release-please:
# 设置任务运行环境为 ubuntu-latest
runs-on: ubuntu-latest
steps:
# 检出代码
- uses: actions/checkout@v4
# 设置 Node.js 环境
- uses: actions/setup-node@v4
with:
# 设置 Node.js 版本
node-version: 20
# 设置 npm 源
registry-url: "https://registry.npmjs.org"
# 安装依赖 不安装对等依赖
- run: npm install --config.auto-install-peers=false --ignore-scripts
# 构建输出
- run: npm run build
# 获取当前 PR 编号并设置环境变量
- run: echo "PR_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_ENV
# 自动修改 package.json 的版本号、删除开发、对等依赖
- run: npm run pr --all
# 发布到 npm
- run: npm run pub
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
# 在 PR 上发布评论
- run: |
INSTALL_COMMAND="npm install ${{ env.PKG_NAME }}@${{ env.PKG_VERSION }}"
gh pr comment ${{ env.PR_NUMBER }} --body "🎉 构建完成!您可以通过以下命令安装此版本:\n\`\`\`\n${INSTALL_COMMAND}\n\`\`\`"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ jobs:
with:
# 设置发布类型为 node
release-type: node
# 设置包名
package-name: node-karin
# 设置发布PR分支
default-branch: dev
# 检出代码
Expand All @@ -34,23 +32,16 @@ jobs:
node-version: 20
# 设置 npm 源
registry-url: "https://registry.npmjs.org"
if: ${{ steps.release.outputs.release_created }}
# 安装jq工具 删除对等依赖 对等依赖是给karin自身使用的
- run: sudo apt-get install jq && jq 'del(.peerDependencies)' package.json > package.json.tmp && mv package.json.tmp package.json
if: ${{ steps.release.outputs.release_created }}
# 安装 pnpm
- run: npm install -g pnpm
if: ${{ steps.release.outputs.release_created }}
# 安装依赖 不安装对等依赖
- run: pnpm install --config.auto-install-peers=false --ignore-scripts
- run: npm install --config.auto-install-peers=false --ignore-scripts
env:
NODE_AUTH_TOKEN: ${{ secrets.RELEASE }}
if: ${{ steps.release.outputs.release_created }}
# 构建输出
- run: pnpm run build:npm
- run: npm run build
if: ${{ steps.release.outputs.release_created }}
# 删除开发依赖 使用jq工具删除
- run: jq 'del(.devDependencies)' package.json > package.json.tmp && mv package.json.tmp package.json
# 删除开发依赖、对等依赖
- run: npm run pr --clean
if: ${{ steps.release.outputs.release_created }}
# 发布到 npm
- run: pnpm run pub
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,9 @@
"scripts": {
".": "node lib/cli/start.js .",
"build": "tsc --project tsconfig.json && tsc-alias -p tsconfig.json",
"build:npm": "tsc --project tsconfig.json && tsc-alias -p tsconfig.json",
"dev": "node lib/cli/start.js dev",
"fix": "eslint lib/**/*.js --fix",
"fix:all": "eslint lib/**/*.js --fix && eslint lib/**/*.d.ts --fix",
"pr": "node lib/cli/pr.js",
"lint": "eslint src/**/*.ts --fix",
"init": "node lib/cli/init.js",
"pub": "npm publish --access public",
"sort": "npx sort-package-json && sort-json tsconfig.json",
Expand Down
79 changes: 79 additions & 0 deletions src/cli/pr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import fs from 'fs'

/**
* @description 获取package.json路径
*/
const getPkgPath = () => process.cwd() + '/package.json'

/**
* @description 读取package.json
*/
const readPkg = () => JSON.parse(fs.readFileSync(getPkgPath(), 'utf-8'))

/**
* @description 写入package.json
* @param pkg package.json
*/
const writePkg = (pkg: any) => fs.writeFileSync(getPkgPath(), JSON.stringify(pkg, null, 2))

/**
* @description 构建pr版本号
* @param pkg package.json
*/
const updateVersion = (pkg: any) => {
const list = pkg.version.split('.')
list[2] = `${Number(list[2])}-dev`
list.push(process.env.PR_NUMBER)
list.push(Date.now().toString())

pkg.version = list.join('.')
}

/**
* @description 设置环境变量
* @param pkg package.json
*/
const setEnvVariables = (pkg: any) => {
fs.appendFileSync(process.env.GITHUB_ENV!, `PKG_NAME=${pkg.name}\nPKG_VERSION=${pkg.version}\n`)
}

/**
* @description 更新版本号并设置环境变量
*/
const version = () => {
const pkg = readPkg()
updateVersion(pkg)
writePkg(pkg)
setEnvVariables(pkg)
}

/**
* @description 删除devDependencies和peerDependencies
*/
const clean = () => {
const pkg = readPkg()
delete pkg.devDependencies
delete pkg.peerDependencies
writePkg(pkg)
}

/**
* @description 执行所有操作
*/
const all = () => {
const pkg = readPkg()
updateVersion(pkg)
delete pkg.devDependencies
delete pkg.peerDependencies
writePkg(pkg)
setEnvVariables(pkg)
}

const cmd = process.argv[2]
if (cmd === '--version') {
version()
} else if (cmd === '--clean') {
clean()
} else if (cmd === '--all') {
all()
}

0 comments on commit d060e4f

Please sign in to comment.