Skip to content

Commit

Permalink
perf: 优化版本号注入逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Nov 5, 2024
1 parent 62b8147 commit 08366a5
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 31 deletions.
2 changes: 1 addition & 1 deletion dist/buildinfo.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion dist/timestamp

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 .",
Expand Down
46 changes: 30 additions & 16 deletions scripts/plugins/version/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
};
8 changes: 8 additions & 0 deletions scripts/plugins/version/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import process from 'node:process';
import { createVersion } from './index';

const {
OUT_DIR = '.',
} = process.env;

createVersion(OUT_DIR).catch(console.error);
9 changes: 3 additions & 6 deletions src/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
WorkersConfig,
} from './config';
import { ConfigMerger } from './merger';
import { BUILD_TIMESTAMP, BUILD_VERSION } from './version';

export type AgentUserConfig = Record<string, any> &
DefineKeys &
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions src/config/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const BUILD_TIMESTAMP = 1730790946;
export const BUILD_VERSION = '62b8147';
7 changes: 2 additions & 5 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [
Expand Down Expand Up @@ -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: {
Expand All @@ -66,7 +66,4 @@ export default defineConfig({
minify: false,
outDir,
},
define: {
...versionDefine,
},
});

0 comments on commit 08366a5

Please sign in to comment.