Skip to content

Commit

Permalink
feat: 在自动发布前, 自动注入新版本到打包代码中
Browse files Browse the repository at this point in the history
  • Loading branch information
wjgogogo committed Dec 14, 2023
1 parent 0684ab5 commit fe82a90
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/auto-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ jobs:
- name: Install dependencies
run: yarn

- name: Build
run: yarn build
# - name: Build
# run: yarn build

# 自动发布完成后 触发 github.release.published 事件
# 如果是 action 自带的 机器人 token, 出于安全考虑, github 会禁止循环触发, 使用真实用户的 token 可解决这个问题
Expand Down
16 changes: 13 additions & 3 deletions .releaserc.base.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const path = require('path');

module.exports = {
extends: 'semantic-release-monorepo',
branches: [
'latest',
{ name: 'beta', channel: 'beta', prerelease: true },
{ name: 'alpha', channel: 'alpha', prerelease: true },
{ name: 'next', channel: 'next', prerelease: true },
],
extends: 'semantic-release-monorepo',
plugins: [
[
'@semantic-release/commit-analyzer',
Expand All @@ -26,11 +28,19 @@ module.exports = {
'@semantic-release/npm',
[
'@semantic-release/git',
{
{
message: 'chore(release): 🤖 ${nextRelease.gitTag} [skip ci]',
},
},
],
'@semantic-release/github',
[
'@semantic-release/exec',
{
prepareCmd:
`node ${path.resolve(__dirname, './scripts/add-version.js')} ` +
'${nextRelease.gitTag}',
},
],
],
preset: 'angular',
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"@rollup/plugin-typescript": "^8.2.5",
"@rushstack/eslint-patch": "^1.1.3",
"@semantic-release/changelog": "^6.0.1",
"@semantic-release/exec": "^6.0.3",
"@semantic-release/git": "^10.0.1",
"@size-limit/esbuild": "^11.0.0",
"@size-limit/esbuild-why": "^11.0.0",
Expand Down
42 changes: 42 additions & 0 deletions scripts/add-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');

const nextVersion = process.argv[2];

const packageEntry = process.cwd();

const packageName = path.basename(packageEntry);
const scrEntry = path.resolve(packageEntry, './src/index.ts');

function generateNextVersion() {
const versionCode = `\nexport const version = '${nextVersion}';\n`;
fs.writeFileSync(scrEntry, versionCode, { encoding: 'utf8', flag: 'a+' });
}

function build() {
// 直接运行 yarn build 要报错,用一下蠢办法
execSync(`yarn clean`, {
stdio: 'inherit',
});

execSync(`yarn build:umd & yarn build:cjs & yarn build:esm`, {
stdio: 'inherit',
});

execSync(`yarn dts:build && yarn dts:extract`, {
stdio: 'inherit',
});
}

function restoreVersionChange() {
execSync(`git restore ${scrEntry}`, { stdio: 'inherit' });

Check warning

Code scanning / CodeQL

Shell command built from environment values Medium

This shell command depends on an uncontrolled
absolute path
.
This shell command depends on an uncontrolled
absolute path
.
}

console.log(`🔖 ${packageName} 添加 nextVersion: ${nextVersion}\n`);

generateNextVersion();
build();
restoreVersionChange();

console.log(`✅ ${packageName} nextVersion(${nextVersion}) 添加成功 \n`);
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2550,6 +2550,18 @@
resolved "https://registry.yarnpkg.com/@semantic-release/error/-/error-3.0.0.tgz#30a3b97bbb5844d695eb22f9d3aa40f6a92770c2"
integrity sha512-5hiM4Un+tpl4cKw3lV4UgzJj+SmfNIDCLLw0TepzQxz9ZGV5ixnqkzIVF+3tp0ZHgcMKE+VNGHJjEeyFG2dcSw==

"@semantic-release/exec@^6.0.3":
version "6.0.3"
resolved "https://registry.npmmirror.com/@semantic-release/exec/-/exec-6.0.3.tgz#d212fdf19633bdfb553de6cb6c7f8781933224db"
integrity sha512-bxAq8vLOw76aV89vxxICecEa8jfaWwYITw6X74zzlO0mc/Bgieqx9kBRz9z96pHectiTAtsCwsQcUyLYWnp3VQ==
dependencies:
"@semantic-release/error" "^3.0.0"
aggregate-error "^3.0.0"
debug "^4.0.0"
execa "^5.0.0"
lodash "^4.17.4"
parse-json "^5.0.0"

"@semantic-release/git@^10.0.1":
version "10.0.1"
resolved "https://registry.yarnpkg.com/@semantic-release/git/-/git-10.0.1.tgz#c646e55d67fae623875bf3a06a634dd434904498"
Expand Down

0 comments on commit fe82a90

Please sign in to comment.