diff --git a/dist/buildinfo.json b/dist/buildinfo.json index 8dd6e4dd..92762565 100644 --- a/dist/buildinfo.json +++ b/dist/buildinfo.json @@ -1 +1 @@ -{"sha":"5ff2ee1","timestamp":1730359461} \ No newline at end of file +{"sha":"62b8147","timestamp":1730790946} \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index f6cea018..d2639cb0 100644 --- a/dist/index.js +++ b/dist/index.js @@ -190,6 +190,9 @@ class ConfigMerger { } } +const BUILD_TIMESTAMP = 1730790946; +const BUILD_VERSION = "62b8147"; + function createAgentUserConfig() { return Object.assign( {}, @@ -211,8 +214,8 @@ const ENV_KEY_MAPPER = { WORKERS_AI_MODEL: "WORKERS_CHAT_MODEL" }; class Environment extends EnvironmentConfig { - BUILD_TIMESTAMP = 1730359461 ; - BUILD_VERSION = "5ff2ee1" ; + BUILD_TIMESTAMP = BUILD_TIMESTAMP; + BUILD_VERSION = BUILD_VERSION; I18N = loadI18n(); PLUGINS_ENV = {}; USER_CONFIG = createAgentUserConfig(); diff --git a/dist/timestamp b/dist/timestamp deleted file mode 100644 index ff5f01a3..00000000 --- a/dist/timestamp +++ /dev/null @@ -1 +0,0 @@ -1730359461 \ No newline at end of file diff --git a/package.json b/package.json index 012f4cd0..56b83e3e 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ ], "scripts": { "lint": "eslint --fix *.js *.ts src plugins scripts", + "version": "tsx ./scripts/plugins/version/main.ts", "build": "vite build", "build:local": "BUILD_MODE=local vite build", "build:docker": "npm run build:local && cd dist && docker build -t chatgpt-telegram-workers:latest .", diff --git a/scripts/plugins/version/index.ts b/scripts/plugins/version/index.ts index 7656caef..873df12e 100644 --- a/scripts/plugins/version/index.ts +++ b/scripts/plugins/version/index.ts @@ -2,29 +2,43 @@ import { execSync } from 'node:child_process'; import * as fs from 'node:fs/promises'; import path from 'node:path'; -let COMMIT_HASH = 'unknown'; const TIMESTAMP = Math.floor(Date.now() / 1000); +const COMMIT_HASH = ((): string => { + try { + return execSync('git rev-parse --short HEAD').toString().trim(); + } catch (e) { + console.warn(e); + } + return 'unknown'; +})(); -try { - COMMIT_HASH = execSync('git rev-parse --short HEAD').toString().trim(); -} catch (e) { - console.warn(e); +async function createVersionTs(outDir: string) { + await fs.writeFile( + path.resolve(outDir, 'src/config/version.ts'), + `export const BUILD_TIMESTAMP = ${TIMESTAMP};\nexport const BUILD_VERSION = '${COMMIT_HASH}';\n`, + ); +} + +async function createVersionJson(outDir: string) { + await fs.writeFile(path.resolve(outDir, 'dist/buildinfo.json'), JSON.stringify({ + sha: COMMIT_HASH, + timestamp: TIMESTAMP, + })); +} + +export async function createVersion(outDir: string) { + await createVersionTs(outDir); + await createVersionJson(outDir); } export function createVersionPlugin(targetDir: string) { return { - name: 'buildInfo', + name: 'version', + async buildStart() { + await createVersionTs(targetDir); + }, async closeBundle() { - await fs.writeFile(path.resolve(targetDir, 'timestamp'), TIMESTAMP.toString()); - await fs.writeFile(path.resolve(targetDir, 'buildinfo.json'), JSON.stringify({ - sha: COMMIT_HASH, - timestamp: TIMESTAMP, - })); + await createVersionJson(targetDir); }, }; } - -export const versionDefine = { - __BUILD_VERSION__: JSON.stringify(COMMIT_HASH), - __BUILD_TIMESTAMP__: TIMESTAMP.toString(), -}; diff --git a/scripts/plugins/version/main.ts b/scripts/plugins/version/main.ts new file mode 100644 index 00000000..67f13bdd --- /dev/null +++ b/scripts/plugins/version/main.ts @@ -0,0 +1,8 @@ +import process from 'node:process'; +import { createVersion } from './index'; + +const { + OUT_DIR = '.', +} = process.env; + +createVersion(OUT_DIR).catch(console.error); diff --git a/src/config/env.ts b/src/config/env.ts index 0f3b2a56..5aaa2751 100644 --- a/src/config/env.ts +++ b/src/config/env.ts @@ -14,6 +14,7 @@ import { WorkersConfig, } from './config'; import { ConfigMerger } from './merger'; +import { BUILD_TIMESTAMP, BUILD_VERSION } from './version'; export type AgentUserConfig = Record & DefineKeys & @@ -53,13 +54,9 @@ class Environment extends EnvironmentConfig { // -- 版本数据 -- // // 当前版本 - // eslint-disable-next-line ts/ban-ts-comment - // @ts-expect-error - BUILD_TIMESTAMP = typeof __BUILD_TIMESTAMP__ === 'number' ? __BUILD_TIMESTAMP__ : 0; + BUILD_TIMESTAMP = BUILD_TIMESTAMP; // 当前版本 commit id - // eslint-disable-next-line ts/ban-ts-comment - // @ts-expect-error - BUILD_VERSION = typeof __BUILD_VERSION__ === 'string' ? __BUILD_VERSION__ : 'unknown'; + BUILD_VERSION = BUILD_VERSION; // -- 基础配置 -- I18N = loadI18n(); diff --git a/src/config/version.ts b/src/config/version.ts new file mode 100644 index 00000000..4269dd2b --- /dev/null +++ b/src/config/version.ts @@ -0,0 +1,2 @@ +export const BUILD_TIMESTAMP = 1730790946; +export const BUILD_VERSION = '62b8147'; diff --git a/vite.config.ts b/vite.config.ts index 01bc6534..1867d656 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -7,7 +7,7 @@ import { defineConfig } from 'vite'; import checker from 'vite-plugin-checker'; import dts from 'vite-plugin-dts'; import { createDockerPlugin } from './scripts/plugins/docker'; -import { createVersionPlugin, versionDefine } from './scripts/plugins/version'; +import { createVersionPlugin } from './scripts/plugins/version'; const { BUILD_MODE } = process.env; const plugins: Plugin[] = [ @@ -50,10 +50,10 @@ switch (BUILD_MODE) { break; default: entry = 'src/index.ts'; - plugins.push(createVersionPlugin('dist')); break; } +plugins.push(createVersionPlugin(path.resolve(__dirname))); export default defineConfig({ plugins, build: { @@ -66,7 +66,4 @@ export default defineConfig({ minify: false, outDir, }, - define: { - ...versionDefine, - }, });