Skip to content

Commit b6d5a5d

Browse files
committed
fix ci
1 parent e91ddd3 commit b6d5a5d

7 files changed

Lines changed: 68 additions & 24 deletions

File tree

.github/workflows/publish.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ jobs:
3131
run: npm ci
3232

3333
- name: Build
34-
run: npm run build -- --define=$VERSION
34+
env:
35+
AppVersion: ${{ github.ref_name }}
36+
SimApiVersion: ${{ github.ref_name }}
37+
run: npm run build
3538

3639
- name: Publish to npm
3740
run: npm publish --access public

AICONTEXT.md

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ SimApiBusinessCallback // { [code]: (data) => void },支持数字码或 'co
5454
- 改 Token 存储:替换 `localStorage``sessionStorage` 或内存变量,修改 `getToken()`/`setToken()`/`removeToken()`
5555
- 改登录/登出逻辑:修改 `login()`/`logout()` 方法
5656
- 改超时处理:修改 `fetchWithTimeout()` 函数
57-
- 版本管理:版本号在库构建时通过 `scripts/replace-version.js` 注入,源码中使用占位符 `0.0.0-version-placeholder`
57+
- **版本管理**:版本号通过 `declare const` 声明常量,构建时通过 Vite 的 `define` 注入。未指定时默认为 `0.0.0-dev`
5858

5959
**autoInit 设计约束**
6060

@@ -104,11 +104,34 @@ npm run build
104104
→ vite build vite.core.config.ts 输出 dist/index.mjs / index.cjs
105105
→ vite build vite.pinia.config.ts 输出 dist/pinia.mjs
106106
→ tsc -p tsconfig.build.json 输出 *.d.ts 类型声明
107+
```
108+
109+
**版本号注入:**
110+
111+
版本号通过 `vite.core.config.ts``define` 配置注入,从环境变量读取:
112+
113+
```typescript
114+
define: {
115+
'AppVersion': JSON.stringify(process.env.AppVersion || process.env.npm_config_AppVersion || '0.0.0-develop'),
116+
'SimApiVersion': JSON.stringify(process.env.SimApiVersion || process.env.npm_config_SimApiVersion || '0.0.0-develop'),
117+
}
118+
```
119+
120+
**本地构建示例:**
121+
```bash
122+
# Windows PowerShell
123+
$env:AppVersion="1.0.0"; $env:SimApiVersion="1.0.0"; npm run build
124+
125+
# Linux/Mac
126+
AppVersion=1.0.0 SimApiVersion=1.0.0 npm run build
127+
```
107128

108-
npm run build:version
109-
→ node scripts/replace-version.js 替换版本占位符
110-
→ npm run build 构建
111-
→ node scripts/restore-version.js 恢复占位符
129+
**GitHub Actions 自动发布:**
130+
```yaml
131+
env:
132+
AppVersion: ${{ github.ref_name }}
133+
SimApiVersion: ${{ github.ref_name }}
134+
run: npm run build
112135
```
113136
114137
**dist 输出是平铺的**,core 和 pinia 的编译产物全部在同一目录:
@@ -142,4 +165,4 @@ dist/
142165
- **无 Cookie**:所有请求不发送 Cookie,Token 通过请求头传递
143166
- **零依赖**:不需要安装 axios,使用原生 fetch
144167
- 删除了 Angular 支持,如需恢复参考 git 历史
145-
- 版本号通过构建脚本注入,GitHub Actions 发布时会自动替换 `0.0.0-version-placeholder` 占位符
168+
- **版本号管理**:使用 `declare const` + Vite `define` 注入,不再需要 sed 替换脚本

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,28 @@ npm run build
332332
npm link # 本地调试
333333
```
334334

335+
### 版本号注入
336+
337+
库使用 `declare const` 声明版本常量,构建时通过 Vite 的 `define` 注入版本号:
338+
339+
**本地构建:**
340+
```bash
341+
# Windows PowerShell
342+
$env:AppVersion="1.0.0"; $env:SimApiVersion="1.0.0"; npm run build
343+
344+
# Linux/Mac
345+
AppVersion=1.0.0 SimApiVersion=1.0.0 npm run build
346+
```
347+
348+
**CI/CD 构建版本号:**
349+
```bash
350+
env:
351+
AppVersion: ${VERSION}
352+
SimApiVersion: ${VERSION}
353+
```
354+
355+
如果未指定环境变量,版本号默认为 `0.0.0-develop`
356+
335357
---
336358

337359
## 从旧版迁移

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
"build": "npm run build:core && npm run build:pinia && npm run types",
3939
"build:core": "vite build --config vite.core.config.ts",
4040
"build:pinia": "vite build --config vite.pinia.config.ts",
41-
"build:version": "node scripts/replace-version.js && npm run build && node scripts/restore-version.js",
4241
"dev": "vite build --config vite.core.config.ts --watch",
4342
"types": "tsc --declaration --emitDeclarationOnly --project tsconfig.build.json",
4443
"lint": "tsc --noEmit"

src/simapi.core.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,13 @@
1111
*/
1212

1313
import {
14-
AppVersion,
15-
SimApiVersion,
1614
type SimApiVersions,
1715
type SimApiAuthConfig,
1816
type SimApiApiConfig,
1917
type SimApiOptions,
2018
type SimApiBaseResponse,
2119
} from './types'
2220

23-
export { SimApiVersion, AppVersion } from './types'
2421
export type {
2522
SimApiVersions,
2623
SimApiAuthConfig,
@@ -29,6 +26,8 @@ export type {
2926
SimApiBaseResponse,
3027
} from './types'
3128

29+
declare const AppVersion: string;
30+
declare const SimApiVersion: string;
3231
// ── Helper: Fetch with Timeout ────────────────────────────────────────
3332

3433
function fetchWithTimeout(
@@ -120,9 +119,6 @@ export class SimApiCore {
120119
if (config.debug !== undefined) {
121120
this.debug = config.debug
122121
}
123-
if (config.uiAppVersion !== undefined) {
124-
this.uiAppVersion = config.uiAppVersion
125-
}
126122
if (config.endpoints) {
127123
this.api.endpoints = { ...this.api.endpoints, ...config.endpoints }
128124
}
@@ -135,9 +131,6 @@ export class SimApiCore {
135131
if (options.debug !== undefined) {
136132
this.debug = options.debug
137133
}
138-
if (options.uiAppVersion !== undefined) {
139-
this.uiAppVersion = options.uiAppVersion
140-
}
141134
if (options.auth) {
142135
this.auth = { ...this.auth, ...options.auth }
143136
}

src/types.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
* SimApi 类型定义
33
*/
44

5-
// 版本号占位符,构建时由 GitHub Action 替换
6-
export const SimApiVersion = '0.0.0-version-placeholder'
7-
export const AppVersion = '0.0.0-version-placeholder'
8-
95
/** 版本信息 */
106
export interface SimApiVersions {
117
uiApp: string
@@ -56,7 +52,7 @@ export interface SimApiApiConfig {
5652
/** SimApi 完整配置 */
5753
export interface SimApiOptions {
5854
debug?: boolean
59-
/** UI 应用版本,如果不指定则使用库内置的占位符版本 */
55+
/** UI 应用版本,如果不指定则使用库内置的版本号 */
6056
uiAppVersion?: string
6157
auth?: Partial<SimApiAuthConfig>
6258
api?: Partial<SimApiApiConfig>

vite.core.config.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { defineConfig } from 'vite'
22

3-
export default defineConfig({
3+
export default defineConfig(({ mode }) => ({
44
build: {
55
outDir: 'dist',
66
emptyOutDir: true,
@@ -13,5 +13,13 @@ export default defineConfig({
1313
rollupOptions: {
1414
// 不再需要 external,使用原生 fetch
1515
}
16+
},
17+
define: {
18+
// 版本号从环境变量读取,构建时传入:
19+
// npm run build:core -- --AppVersion=1.2.3 --SimApiVersion=2.3.4
20+
// 或:
21+
// AppVersion=1.2.3 SimApiVersion=2.3.4 npm run build:core
22+
'AppVersion': JSON.stringify(process.env.AppVersion || process.env.npm_config_AppVersion || '0.0.0-develop'),
23+
'SimApiVersion': JSON.stringify(process.env.SimApiVersion || process.env.npm_config_SimApiVersion || '0.0.0-develop'),
1624
}
17-
})
25+
}))

0 commit comments

Comments
 (0)