befly 3.4.8 → 3.4.10

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/bin/index.ts CHANGED
@@ -27,18 +27,14 @@ import { syncMenuCommand } from '../commands/syncMenu.js';
27
27
  import { syncDevCommand } from '../commands/syncDev.js';
28
28
  import { Logger } from '../lib/logger.js';
29
29
  import { join } from 'pathe';
30
+ import { getPackageVersion } from '../commands/util.js';
30
31
 
31
32
  /**
32
33
  * 读取 package.json 版本号
33
34
  */
34
35
  function getVersion(): string {
35
- try {
36
- const pkgPath = join(import.meta.dir, '..', 'package.json');
37
- const pkg = require(pkgPath);
38
- return pkg.version || '0.0.0';
39
- } catch (error) {
40
- return '0.0.0';
41
- }
36
+ const coreDir = join(import.meta.dir, '..');
37
+ return getPackageVersion(coreDir);
42
38
  }
43
39
 
44
40
  /**
@@ -124,12 +120,13 @@ const program = new Command();
124
120
  program.name('befly').description('Befly CLI - 为 Befly 框架提供命令行工具').version(getVersion());
125
121
 
126
122
  /**
127
- * 包装命令处理函数,在执行前打印环境
123
+ * 包装命令处理函数,在执行后打印环境
128
124
  */
129
125
  function wrapCommand<T extends (...args: any[]) => any>(fn: T): T {
130
126
  return (async (...args: any[]) => {
127
+ const result = await fn(...args);
131
128
  Logger.printEnv();
132
- return await fn(...args);
129
+ return result;
133
130
  }) as T;
134
131
  }
135
132
 
package/bin/launcher.ts CHANGED
@@ -71,12 +71,15 @@ export async function launch(entryFile: string): Promise<void> {
71
71
  }
72
72
 
73
73
  // 确定环境名称
74
- const envArgIndex = process.argv.indexOf('--env');
74
+ const longEnvIndex = process.argv.indexOf('--env');
75
+ const shortEnvIndex = process.argv.indexOf('-e');
75
76
  let envInput = 'development'; // 默认环境
76
77
 
77
- if (envArgIndex !== -1 && process.argv[envArgIndex + 1]) {
78
- // 如果指定了 --env 参数
79
- envInput = process.argv[envArgIndex + 1];
78
+ // 检查 --env -e 参数
79
+ if (longEnvIndex !== -1 && process.argv[longEnvIndex + 1]) {
80
+ envInput = process.argv[longEnvIndex + 1];
81
+ } else if (shortEnvIndex !== -1 && process.argv[shortEnvIndex + 1]) {
82
+ envInput = process.argv[shortEnvIndex + 1];
80
83
  } else if (process.env.NODE_ENV) {
81
84
  // 如果设置了 NODE_ENV 环境变量
82
85
  envInput = process.env.NODE_ENV;
@@ -86,10 +89,10 @@ export async function launch(entryFile: string): Promise<void> {
86
89
  const envName = resolveEnvName(envInput);
87
90
  const envFile = `.env.${envName}`;
88
91
 
89
- // 过滤掉 --env 参数(已通过 --env-file 处理)
92
+ // 过滤掉 --env/-e 参数(已通过 --env-file 处理)
90
93
  const filteredArgs = process.argv.slice(2).filter((arg, i, arr) => {
91
- if (arg === '--env') return false;
92
- if (arr[i - 1] === '--env') return false;
94
+ if (arg === '--env' || arg === '-e') return false;
95
+ if (arr[i - 1] === '--env' || arr[i - 1] === '-e') return false;
93
96
  return true;
94
97
  });
95
98
 
@@ -98,9 +101,13 @@ export async function launch(entryFile: string): Promise<void> {
98
101
  cwd: process.cwd(),
99
102
  stdio: ['inherit', 'inherit', 'inherit'],
100
103
  env: {
101
- ...process.env,
104
+ // 不继承父进程的环境变量,只使用以下变量
102
105
  BEFLY_SUBPROCESS: '1', // 标记为子进程
103
- NODE_ENV: envName // 同时设置 NODE_ENV
106
+ NODE_ENV: envName, // 设置 NODE_ENV
107
+ PATH: process.env.PATH, // 保留 PATH
108
+ SYSTEMROOT: process.env.SYSTEMROOT, // Windows 需要
109
+ TEMP: process.env.TEMP, // Windows 需要
110
+ TMP: process.env.TMP // Windows 需要
104
111
  }
105
112
  });
106
113
 
package/commands/build.ts CHANGED
@@ -6,18 +6,7 @@ import { join } from 'pathe';
6
6
  import { existsSync } from 'node:fs';
7
7
  import ora from 'ora';
8
8
  import { Logger } from '../lib/logger.js';
9
-
10
- function getProjectRoot(): string {
11
- let current = process.cwd();
12
- const path = require('node:path');
13
- while (current !== path.parse(current).root) {
14
- if (existsSync(join(current, 'package.json'))) {
15
- return current;
16
- }
17
- current = path.dirname(current);
18
- }
19
- return process.cwd();
20
- }
9
+ import { getProjectRoot } from './util.js';
21
10
 
22
11
  interface BuildOptions {
23
12
  outdir: string;
package/commands/dev.ts CHANGED
@@ -6,6 +6,7 @@ import { join } from 'pathe';
6
6
  import { existsSync } from 'node:fs';
7
7
  import { Logger } from '../lib/logger.js';
8
8
  import { Befly } from '../main.js';
9
+ import { getProjectRoot } from './util.js';
9
10
 
10
11
  interface DevOptions {
11
12
  port: string;
@@ -14,17 +15,6 @@ interface DevOptions {
14
15
  verbose: boolean;
15
16
  }
16
17
 
17
- function getProjectRoot(): string {
18
- let current = process.cwd();
19
- while (current !== require('node:path').parse(current).root) {
20
- if (existsSync(join(current, 'package.json'))) {
21
- return current;
22
- }
23
- current = require('node:path').dirname(current);
24
- }
25
- return process.cwd();
26
- }
27
-
28
18
  export async function devCommand(options: DevOptions) {
29
19
  try {
30
20
  const projectRoot = getProjectRoot();
package/commands/index.ts CHANGED
@@ -6,18 +6,7 @@ import { join } from 'pathe';
6
6
  import { existsSync } from 'node:fs';
7
7
  import ora from 'ora';
8
8
  import { Logger } from '../lib/logger.js';
9
-
10
- function getProjectRoot(): string {
11
- let current = process.cwd();
12
- const path = require('node:path');
13
- while (current !== path.parse(current).root) {
14
- if (existsSync(join(current, 'package.json'))) {
15
- return current;
16
- }
17
- current = path.dirname(current);
18
- }
19
- return process.cwd();
20
- }
9
+ import { getProjectRoot } from './util.js';
21
10
 
22
11
  // ========== Build 命令 ==========
23
12
  interface BuildOptions {
package/commands/start.ts CHANGED
@@ -7,18 +7,7 @@ import { existsSync } from 'node:fs';
7
7
  import { Logger } from '../lib/logger.js';
8
8
  import { ClusterManager } from '../lifecycle/cluster.js';
9
9
  import { Befly } from '../main.js';
10
-
11
- function getProjectRoot(): string {
12
- let current = process.cwd();
13
- const path = require('node:path');
14
- while (current !== path.parse(current).root) {
15
- if (existsSync(join(current, 'package.json'))) {
16
- return current;
17
- }
18
- current = path.dirname(current);
19
- }
20
- return process.cwd();
21
- }
10
+ import { getProjectRoot } from './util.js';
22
11
 
23
12
  interface StartOptions {
24
13
  port: string;
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Commands 工具函数
3
+ * 提供命令间可复用的通用功能
4
+ */
5
+
6
+ import { join, parse, dirname } from 'pathe';
7
+ import { existsSync, readFileSync } from 'node:fs';
8
+
9
+ /**
10
+ * 获取项目根目录
11
+ * 向上查找包含 package.json 的目录
12
+ *
13
+ * @returns 项目根目录路径
14
+ */
15
+ export function getProjectRoot(): string {
16
+ let current = process.cwd();
17
+ const root = parse(current).root;
18
+
19
+ while (current !== root) {
20
+ if (existsSync(join(current, 'package.json'))) {
21
+ return current;
22
+ }
23
+ current = dirname(current);
24
+ }
25
+
26
+ return process.cwd();
27
+ }
28
+
29
+ /**
30
+ * 读取 package.json 文件内容
31
+ *
32
+ * @param pkgPath package.json 文件路径
33
+ * @returns package.json 的内容对象
34
+ */
35
+ export function readPackageJson(pkgPath: string): Record<string, any> {
36
+ try {
37
+ const content = readFileSync(pkgPath, 'utf-8');
38
+ return JSON.parse(content);
39
+ } catch (error) {
40
+ throw new Error(`读取 package.json 失败: ${error}`);
41
+ }
42
+ }
43
+
44
+ /**
45
+ * 获取指定目录的 package.json 版本号
46
+ *
47
+ * @param dir 目录路径
48
+ * @returns 版本号字符串
49
+ */
50
+ export function getPackageVersion(dir: string): string {
51
+ try {
52
+ const pkgPath = join(dir, 'package.json');
53
+ const pkg = readPackageJson(pkgPath);
54
+ return pkg.version || '0.0.0';
55
+ } catch (error) {
56
+ return '0.0.0';
57
+ }
58
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "befly",
3
- "version": "3.4.8",
3
+ "version": "3.4.10",
4
4
  "description": "Befly - 为 Bun 专属打造的 TypeScript API 接口框架核心引擎",
5
5
  "type": "module",
6
6
  "private": false,
@@ -81,5 +81,5 @@
81
81
  "ora": "^9.0.0",
82
82
  "pathe": "^2.0.3"
83
83
  },
84
- "gitHead": "8b7e6187e0a0ed12656ed2a2ff4107e0f76a7e31"
84
+ "gitHead": "cff7a359c94cef609c5d7fa0b34b114d34fbe04f"
85
85
  }