diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index ae59c35..d07d3dc 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -31,10 +31,7 @@ jobs: run: npm ci - name: Build - env: - AppVersion: ${{ github.ref_name }} - SimApiVersion: ${{ github.ref_name }} - run: npm run build + run: npm run build -- --define SimApiVersion="'${{github.ref_name}}'" - name: Publish to npm run: npm publish --access public \ No newline at end of file diff --git a/AICONTEXT.md b/AICONTEXT.md index b5fecd9..660bcec 100644 --- a/AICONTEXT.md +++ b/AICONTEXT.md @@ -108,17 +108,21 @@ npm run build **版本号注入:** -版本号通过 `vite.core.config.ts` 的 `define` 配置注入,从环境变量读取: +版本号通过 `vite.core.config.ts` 的 `define` 配置注入,从 npm config 读取: ```typescript define: { - 'AppVersion': JSON.stringify(process.env.AppVersion || process.env.npm_config_AppVersion || '0.0.0-develop'), - 'SimApiVersion': JSON.stringify(process.env.SimApiVersion || process.env.npm_config_SimApiVersion || '0.0.0-develop'), + 'AppVersion': JSON.stringify(process.env.npm_config_AppVersion || '0.0.0-develop'), + 'SimApiVersion': JSON.stringify(process.env.npm_config_SimApiVersion || '0.0.0-develop'), } ``` **本地构建示例:** ```bash +# 通过 npm config 传递(推荐) +npm run build -- --AppVersion=1.0.0 --SimApiVersion=1.0.0 + +# 或通过环境变量 # Windows PowerShell $env:AppVersion="1.0.0"; $env:SimApiVersion="1.0.0"; npm run build diff --git a/README.md b/README.md index e7ed164..f45e920 100644 --- a/README.md +++ b/README.md @@ -338,6 +338,10 @@ npm link # 本地调试 **本地构建:** ```bash +# 通过 npm config 传递 +npm run build -- --AppVersion=1.0.0 --SimApiVersion=1.0.0 + +# 或设置环境变量后构建 # Windows PowerShell $env:AppVersion="1.0.0"; $env:SimApiVersion="1.0.0"; npm run build @@ -348,8 +352,8 @@ AppVersion=1.0.0 SimApiVersion=1.0.0 npm run build **CI/CD 构建版本号:** ```bash env: - AppVersion: ${VERSION} - SimApiVersion: ${VERSION} + AppVersion: ${{ github.ref_name }} + SimApiVersion: ${{ github.ref_name }} ``` 如果未指定环境变量,版本号默认为 `0.0.0-develop`。 diff --git a/src/simapi.core.ts b/src/simapi.core.ts index 36707fe..b41c086 100644 --- a/src/simapi.core.ts +++ b/src/simapi.core.ts @@ -77,8 +77,6 @@ async function fetchPost( export class SimApiCore { debug: boolean = true - uiAppVersion?: string - auth: SimApiAuthConfig = { token_name: 'simapi-auth-token', check_url: '/auth/check', @@ -210,8 +208,8 @@ export class SimApiCore { if (resp?.data) { const d = resp.data const versions: SimApiVersions = { - uiApp: this.uiAppVersion ?? AppVersion, - uiSimApi: SimApiVersion, + uiApp: AppVersion ?? "0.0.0-develop", + uiSimApi: SimApiVersion ?? "0.0.0-develop", apiApp: d.App?.split('+')[0] ?? '0.0.0', apiSimApi: d.SimApi?.split('+')[0] ?? '0.0.0', apiAppFull: d.App ?? '0.0.0', @@ -226,8 +224,8 @@ export class SimApiCore { // 版本获取失败返回默认值 } return { - uiApp: AppVersion, - uiSimApi: SimApiVersion, + uiApp: "0.0.0-develop", + uiSimApi: "0.0.0-develop", apiApp: '0.0.0', apiSimApi: '0.0.0', apiAppFull: '0.0.0', diff --git a/vite.core.config.ts b/vite.core.config.ts index 93b98c7..0e0b407 100644 --- a/vite.core.config.ts +++ b/vite.core.config.ts @@ -1,6 +1,6 @@ import { defineConfig } from 'vite' -export default defineConfig(({ mode }) => ({ +export default defineConfig({ build: { outDir: 'dist', emptyOutDir: true, @@ -13,13 +13,5 @@ export default defineConfig(({ mode }) => ({ rollupOptions: { // 不再需要 external,使用原生 fetch } - }, - define: { - // 版本号从环境变量读取,构建时传入: - // npm run build:core -- --AppVersion=1.2.3 --SimApiVersion=2.3.4 - // 或: - // AppVersion=1.2.3 SimApiVersion=2.3.4 npm run build:core - 'AppVersion': JSON.stringify(process.env.AppVersion || process.env.npm_config_AppVersion || '0.0.0-develop'), - 'SimApiVersion': JSON.stringify(process.env.SimApiVersion || process.env.npm_config_SimApiVersion || '0.0.0-develop'), } -})) +})