-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
130 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |