@xiaozhi-client/cli 1.9.4-beta.12 → 1.9.4-beta.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xiaozhi-client/cli",
3
- "version": "1.9.4-beta.12",
3
+ "version": "1.9.4-beta.13",
4
4
  "private": false,
5
5
  "repository": {
6
6
  "type": "git",
package/src/global.d.ts CHANGED
@@ -16,4 +16,8 @@ declare global {
16
16
  }
17
17
  }
18
18
 
19
+ // 构建时注入的版本号常量
20
+ declare const __VERSION__: string;
21
+ declare const __APP_NAME__: string;
22
+
19
23
  export {};
package/src/index.ts CHANGED
@@ -39,8 +39,6 @@ async function initializeCLI(): Promise<void> {
39
39
  }
40
40
  }
41
41
 
42
- // 启动 CLI 应用
43
- // CLI 入口文件直接执行初始化(无需模块检测,兼容 npm 全局安装的符号链接)
44
42
  initializeCLI();
45
43
 
46
44
  export { initializeCLI };
@@ -2,10 +2,7 @@
2
2
  * 版本管理工具
3
3
  */
4
4
 
5
- import fs from "node:fs";
6
- import path from "node:path";
7
- import { fileURLToPath } from "node:url";
8
- import { FileError } from "../errors/index";
5
+ import { VERSION, APP_NAME } from "../version";
9
6
 
10
7
  /**
11
8
  * 版本信息接口
@@ -21,88 +18,21 @@ export interface VersionInfo {
21
18
  * 版本工具类
22
19
  */
23
20
  export class VersionUtils {
24
- private static cachedVersion: string | null = null;
25
-
26
21
  /**
27
- * 获取版本号
22
+ * 获取版本号(构建时注入)
28
23
  */
29
24
  static getVersion(): string {
30
- if (VersionUtils.cachedVersion) {
31
- return VersionUtils.cachedVersion;
32
- }
33
-
34
- try {
35
- // 在 ES 模块环境中获取当前目录
36
- const __filename = fileURLToPath(import.meta.url);
37
- const currentDir = path.dirname(__filename);
38
-
39
- // 尝试多个可能的 package.json 路径
40
- const possiblePaths = [
41
- // 构建后环境:dist/cli.js -> dist/package.json (优先)
42
- path.join(currentDir, "package.json"),
43
- // 构建后环境:dist/cli.js -> package.json
44
- path.join(currentDir, "..", "package.json"),
45
- // 开发环境:src/cli/utils/VersionUtils.ts -> package.json
46
- path.join(currentDir, "..", "..", "..", "package.json"),
47
- // 全局安装环境
48
- path.join(currentDir, "..", "..", "..", "..", "package.json"),
49
- ];
50
-
51
- for (const packagePath of possiblePaths) {
52
- if (fs.existsSync(packagePath)) {
53
- const packageJson = JSON.parse(fs.readFileSync(packagePath, "utf8"));
54
- if (packageJson.version) {
55
- VersionUtils.cachedVersion = packageJson.version;
56
- return packageJson.version;
57
- }
58
- }
59
- }
60
-
61
- // 如果都找不到,返回默认版本
62
- VersionUtils.cachedVersion = "unknown";
63
- return "unknown";
64
- } catch (error) {
65
- console.warn("无法从 package.json 读取版本信息:", error);
66
- VersionUtils.cachedVersion = "unknown";
67
- return "unknown";
68
- }
25
+ return VERSION;
69
26
  }
70
27
 
71
28
  /**
72
- * 获取完整版本信息
29
+ * 获取完整版本信息(构建时注入)
73
30
  */
74
31
  static getVersionInfo(): VersionInfo {
75
- try {
76
- const __filename = fileURLToPath(import.meta.url);
77
- const currentDir = path.dirname(__filename);
78
-
79
- const possiblePaths = [
80
- // 构建后环境:dist/cli.js -> dist/package.json (优先)
81
- path.join(currentDir, "package.json"),
82
- // 构建后环境:dist/cli.js -> package.json
83
- path.join(currentDir, "..", "package.json"),
84
- // 开发环境:src/cli/utils/VersionUtils.ts -> package.json
85
- path.join(currentDir, "..", "..", "..", "package.json"),
86
- // 全局安装环境
87
- path.join(currentDir, "..", "..", "..", "..", "package.json"),
88
- ];
89
-
90
- for (const packagePath of possiblePaths) {
91
- if (fs.existsSync(packagePath)) {
92
- const packageJson = JSON.parse(fs.readFileSync(packagePath, "utf8"));
93
- return {
94
- version: packageJson.version || "unknown",
95
- name: packageJson.name,
96
- description: packageJson.description,
97
- author: packageJson.author,
98
- };
99
- }
100
- }
101
-
102
- return { version: "unknown" };
103
- } catch (error) {
104
- throw new FileError("无法读取版本信息", "package.json");
105
- }
32
+ return {
33
+ version: VERSION,
34
+ name: APP_NAME,
35
+ };
106
36
  }
107
37
 
108
38
  /**
@@ -131,11 +61,4 @@ export class VersionUtils {
131
61
  const versionRegex = /^\d+\.\d+\.\d+(-[a-zA-Z0-9.-]+)?$/;
132
62
  return versionRegex.test(version);
133
63
  }
134
-
135
- /**
136
- * 清除版本缓存
137
- */
138
- static clearCache(): void {
139
- VersionUtils.cachedVersion = null;
140
- }
141
64
  }
package/src/version.ts ADDED
@@ -0,0 +1,5 @@
1
+ /**
2
+ * 版本号常量(构建时注入)
3
+ */
4
+ export const VERSION = __VERSION__;
5
+ export const APP_NAME = __APP_NAME__;
package/tsup.config.ts CHANGED
@@ -2,6 +2,10 @@ import { readFileSync, writeFileSync } from "node:fs";
2
2
  import { resolve } from "node:path";
3
3
  import { defineConfig } from "tsup";
4
4
 
5
+ // 读取根目录 package.json 获取版本号
6
+ const rootPkgPath = resolve("../../package.json");
7
+ const pkg = JSON.parse(readFileSync(rootPkgPath, "utf-8"));
8
+
5
9
  export default defineConfig({
6
10
  entry: {
7
11
  index: "src/index.ts",
@@ -19,8 +23,13 @@ export default defineConfig({
19
23
  platform: "node",
20
24
  esbuildOptions: (options) => {
21
25
  options.resolveExtensions = [".ts", ".js", ".json"];
22
- // 使用 esbuild 的别名机制
23
- // 由于 esbuild 的 alias 不支持通配符,我们需要注入多个具体的别名
26
+
27
+ // 构建时注入版本号常量
28
+ options.define = {
29
+ ...options.define, // 保留已有的 define
30
+ __VERSION__: JSON.stringify(pkg.version),
31
+ __APP_NAME__: JSON.stringify(pkg.name),
32
+ };
24
33
  },
25
34
  external: [
26
35
  // Node.js 内置模块