befly 3.5.1 → 3.5.3

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.
Files changed (109) hide show
  1. package/apis/admin/del.ts +1 -1
  2. package/apis/admin/info.ts +1 -1
  3. package/apis/admin/ins.ts +1 -1
  4. package/apis/admin/list.ts +1 -1
  5. package/apis/admin/roleDetail.ts +1 -1
  6. package/apis/admin/roleSave.ts +1 -1
  7. package/apis/admin/upd.ts +1 -1
  8. package/apis/api/all.ts +1 -1
  9. package/apis/auth/login.ts +1 -1
  10. package/apis/auth/logout.ts +1 -1
  11. package/apis/auth/register.ts +1 -1
  12. package/apis/auth/sendSmsCode.ts +3 -2
  13. package/apis/cache/refresh.ts +1 -1
  14. package/apis/dashboard/addonList.ts +1 -1
  15. package/apis/dashboard/changelog.ts +1 -1
  16. package/apis/dashboard/configStatus.ts +1 -1
  17. package/apis/dashboard/environmentInfo.ts +1 -1
  18. package/apis/dashboard/performanceMetrics.ts +1 -1
  19. package/apis/dashboard/permissionStats.ts +1 -1
  20. package/apis/dashboard/serviceStatus.ts +2 -2
  21. package/apis/dashboard/systemInfo.ts +3 -2
  22. package/apis/dashboard/systemOverview.ts +1 -1
  23. package/apis/dashboard/systemResources.ts +1 -1
  24. package/apis/dict/all.ts +1 -1
  25. package/apis/dict/del.ts +1 -1
  26. package/apis/dict/detail.ts +1 -1
  27. package/apis/dict/ins.ts +1 -1
  28. package/apis/dict/list.ts +1 -1
  29. package/apis/dict/upd.ts +1 -1
  30. package/apis/menu/all.ts +1 -1
  31. package/apis/menu/del.ts +1 -1
  32. package/apis/menu/ins.ts +1 -1
  33. package/apis/menu/list.ts +1 -1
  34. package/apis/menu/upd.ts +1 -1
  35. package/apis/role/apiDetail.ts +1 -1
  36. package/apis/role/apiSave.ts +1 -1
  37. package/apis/role/del.ts +1 -1
  38. package/apis/role/detail.ts +1 -1
  39. package/apis/role/ins.ts +1 -1
  40. package/apis/role/list.ts +1 -1
  41. package/apis/role/menuDetail.ts +1 -1
  42. package/apis/role/menuSave.ts +1 -1
  43. package/apis/role/save.ts +1 -1
  44. package/apis/role/upd.ts +1 -1
  45. package/bin/index.ts +18 -131
  46. package/checks/conflict.ts +1 -1
  47. package/checks/table.ts +6 -8
  48. package/commands/index.ts +2 -98
  49. package/commands/sync.ts +61 -29
  50. package/commands/syncApi.ts +23 -53
  51. package/commands/syncDb/apply.ts +1 -1
  52. package/commands/syncDb/constants.ts +2 -2
  53. package/commands/syncDb/ddl.ts +1 -1
  54. package/commands/syncDb/helpers.ts +1 -1
  55. package/commands/syncDb/index.ts +33 -33
  56. package/commands/syncDb/schema.ts +2 -2
  57. package/commands/syncDb/sqlite.ts +1 -1
  58. package/commands/syncDb/state.ts +1 -1
  59. package/commands/syncDb/table.ts +1 -1
  60. package/commands/syncDb/tableCreate.ts +1 -1
  61. package/commands/syncDb/types.ts +1 -1
  62. package/commands/syncDb/version.ts +2 -2
  63. package/commands/syncDb.ts +6 -6
  64. package/commands/syncDev.ts +37 -35
  65. package/commands/syncMenu.ts +26 -77
  66. package/commands/util.ts +1 -1
  67. package/lib/cipher.ts +1 -1
  68. package/lib/database.ts +2 -11
  69. package/lib/dbHelper.ts +1 -1
  70. package/lib/jwt.ts +2 -2
  71. package/lib/logger.ts +9 -7
  72. package/lib/middleware.ts +2 -2
  73. package/lib/redisHelper.ts +2 -2
  74. package/lib/sqlBuilder.ts +1 -1
  75. package/lib/validator.ts +1 -1
  76. package/lib/xml.ts +1 -1
  77. package/lifecycle/bootstrap.ts +2 -2
  78. package/lifecycle/checker.ts +9 -26
  79. package/lifecycle/lifecycle.ts +6 -35
  80. package/lifecycle/loader.ts +11 -101
  81. package/main.ts +2 -2
  82. package/package.json +2 -4
  83. package/paths.ts +1 -1
  84. package/plugins/cache.ts +1 -1
  85. package/plugins/db.ts +3 -3
  86. package/plugins/logger.ts +1 -1
  87. package/plugins/redis.ts +2 -2
  88. package/router/api.ts +2 -2
  89. package/router/root.ts +2 -2
  90. package/router/static.ts +2 -2
  91. package/types/addon.d.ts +1 -1
  92. package/types/api.d.ts +1 -1
  93. package/types/befly.d.ts +1 -1
  94. package/types/common.d.ts +1 -1
  95. package/types/context.d.ts +1 -1
  96. package/types/crypto.d.ts +1 -1
  97. package/types/database.d.ts +1 -1
  98. package/types/env.ts +67 -0
  99. package/types/index.ts +5 -0
  100. package/types/jwt.d.ts +1 -1
  101. package/types/logger.d.ts +1 -1
  102. package/types/plugin.d.ts +1 -1
  103. package/types/redis.d.ts +1 -1
  104. package/types/tool.d.ts +1 -1
  105. package/types/util.d.ts +1 -1
  106. package/types/validator.d.ts +1 -1
  107. package/util.ts +2 -2
  108. package/commands/build.ts +0 -62
  109. package/config/env.ts +0 -202
package/apis/admin/del.ts CHANGED
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 删除管理员
3
3
  */
4
4
 
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 获取用户信息接口
3
3
  *
4
4
  * @returns 返回 admin 表的字段(不含 password)以及关联的角色信息(小驼峰格式):
package/apis/admin/ins.ts CHANGED
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 添加管理员
3
3
  */
4
4
 
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 获取管理员列表
3
3
  */
4
4
 
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 获取管理员的角色(单角色模式)
3
3
  */
4
4
 
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 保存管理员的角色(单角色模式)
3
3
  */
4
4
 
package/apis/admin/upd.ts CHANGED
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 更新管理员信息
3
3
  */
4
4
 
package/apis/api/all.ts CHANGED
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 获取所有接口列表
3
3
  * 说明:用于角色权限配置,返回所有接口信息
4
4
  */
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 管理员登录接口
3
3
  */
4
4
 
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 退出登录接口
3
3
  */
4
4
 
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 管理员注册接口
3
3
  */
4
4
 
@@ -1,8 +1,9 @@
1
- /**
1
+ /**
2
2
  * 发送短信验证码接口
3
3
  */
4
4
 
5
5
  import { Yes } from '../../util.js';
6
+ import { Env } from '../../env.js';
6
7
  import adminTable from '../../tables/admin.json';
7
8
 
8
9
  export default {
@@ -28,7 +29,7 @@ export default {
28
29
  }
29
30
 
30
31
  // 开发环境下返回验证码(生产环境应该删除)
31
- const isDev = process.env.NODE_ENV === 'development';
32
+ const isDev = Env.NODE_ENV === 'development';
32
33
 
33
34
  return Yes('验证码已发送', isDev ? { code } : null);
34
35
  }
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 刷新所有缓存到 Redis
3
3
  * 说明:手动触发缓存刷新,无需重启服务器
4
4
  */
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 获取插件列表
3
3
  */
4
4
 
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 获取更新日志
3
3
  */
4
4
 
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 获取系统配置状态
3
3
  */
4
4
 
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 获取运行环境信息
3
3
  */
4
4
 
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 获取性能指标
3
3
  */
4
4
 
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 获取权限统计信息
3
3
  */
4
4
 
@@ -1,9 +1,9 @@
1
- /**
1
+ /**
2
2
  * 获取服务状态
3
3
  */
4
4
 
5
5
  import { Yes } from '../../util.js';
6
- import { Env } from '../../config/env.js';
6
+ import { Env } from '../../env.js';
7
7
 
8
8
  export default {
9
9
  name: '获取服务状态',
@@ -1,8 +1,9 @@
1
- /**
1
+ /**
2
2
  * 获取系统信息
3
3
  */
4
4
 
5
5
  import { Yes } from '../../util.js';
6
+ import { Env } from '../../env.js';
6
7
 
7
8
  export default {
8
9
  name: '获取系统信息',
@@ -14,7 +15,7 @@ export default {
14
15
  const uptime = Math.floor(process.uptime() * 1000);
15
16
 
16
17
  // 环境
17
- const environment = process.env.NODE_ENV || 'development';
18
+ const environment = Env.NODE_ENV || 'development';
18
19
 
19
20
  return Yes('获取成功', {
20
21
  environment: environment,
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 获取系统概览数据
3
3
  */
4
4
 
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 获取系统资源使用情况
3
3
  */
4
4
 
package/apis/dict/all.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Yes, No } from '../../util.js';
1
+ import { Yes, No } from '../../util.js';
2
2
 
3
3
  /**
4
4
  * 获取所有字典(不分页)
package/apis/dict/del.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Field, Yes, No } from '../../util.js';
1
+ import { Field, Yes, No } from '../../util.js';
2
2
  export default {
3
3
  name: '删除字典',
4
4
  handler: async (befly, ctx) => {
@@ -1,4 +1,4 @@
1
- import { Yes, No } from '../../util.js';
1
+ import { Yes, No } from '../../util.js';
2
2
 
3
3
  export default {
4
4
  name: '获取字典详情',
package/apis/dict/ins.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Yes, No } from '../../util.js';
1
+ import { Yes, No } from '../../util.js';
2
2
  import adminDictTable from '../../tables/dict.json';
3
3
 
4
4
  export default {
package/apis/dict/list.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Yes, No } from '../../util.js';
1
+ import { Yes, No } from '../../util.js';
2
2
  export default {
3
3
  name: '获取字典列表',
4
4
  handler: async (befly, ctx) => {
package/apis/dict/upd.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Yes, No } from '../../util.js';
1
+ import { Yes, No } from '../../util.js';
2
2
  import adminDictTable from '../../tables/dict.json';
3
3
 
4
4
  export default {
package/apis/menu/all.ts CHANGED
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 获取当前用户的菜单权限
3
3
  * 说明:
4
4
  * 1. 从 Redis 缓存读取所有菜单(如果缓存不存在则从数据库查询并缓存)
package/apis/menu/del.ts CHANGED
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 删除菜单
3
3
  */
4
4
 
package/apis/menu/ins.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Yes, No } from '../../util.js';
1
+ import { Yes, No } from '../../util.js';
2
2
  import adminMenuTable from '../../tables/menu.json';
3
3
 
4
4
  export default {
package/apis/menu/list.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Yes, No } from '../../util.js';
1
+ import { Yes, No } from '../../util.js';
2
2
  export default {
3
3
  name: '获取菜单列表',
4
4
  handler: async (befly, ctx) => {
package/apis/menu/upd.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Yes, No } from '../../util.js';
1
+ import { Yes, No } from '../../util.js';
2
2
  import adminMenuTable from '../../tables/menu.json';
3
3
 
4
4
  export default {
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 获取角色的接口权限
3
3
  */
4
4
 
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 保存角色的接口权限
3
3
  */
4
4
 
package/apis/role/del.ts CHANGED
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 删除角色
3
3
  */
4
4
 
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 获取用户的角色(单角色模式)
3
3
  */
4
4
 
package/apis/role/ins.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Yes, No } from '../../util.js';
1
+ import { Yes, No } from '../../util.js';
2
2
  import adminRoleTable from '../../tables/role.json';
3
3
 
4
4
  /**
package/apis/role/list.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Yes } from '../../util.js';
1
+ import { Yes } from '../../util.js';
2
2
 
3
3
  export default {
4
4
  name: '获取角色列表',
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 获取角色的菜单权限
3
3
  */
4
4
 
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 保存角色的菜单权限
3
3
  */
4
4
 
package/apis/role/save.ts CHANGED
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 保存用户的角色(单角色模式)
3
3
  */
4
4
 
package/apis/role/upd.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Yes, No } from '../../util.js';
1
+ import { Yes, No } from '../../util.js';
2
2
  import adminRoleTable from '../../tables/role.json';
3
3
 
4
4
  export default {
package/bin/index.ts CHANGED
@@ -1,147 +1,34 @@
1
- #!/usr/bin/env bun
1
+ #!/usr/bin/env bun
2
2
  /**
3
3
  * Befly CLI - 命令行工具入口
4
- * Befly 框架提供项目管理和脚本执行功能
4
+ * 只提供 sync 命令,用于同步所有数据
5
+ *
6
+ * 使用方法:
7
+ * befly sync # 使用当前环境(默认 development)
5
8
  *
6
9
  * 环境变量加载:
7
- * 1. Bun 自动加载:根据 NODE_ENV 自动加载 .env.{NODE_ENV} 文件
8
- * 2. 手动指定:bun --env-file=.env.xxx befly <command>
9
- * 3. 默认环境:如未设置 NODE_ENV,Bun 加载 .env.development
10
+ * Bun 自动根据 NODE_ENV 加载对应的 .env 文件:
11
+ * - NODE_ENV=development → .env.development
12
+ * - NODE_ENV=production .env.production
13
+ * - NODE_ENV=test → .env.test
10
14
  */
11
15
 
12
- import { Command } from 'commander';
13
- import { buildCommand } from '../commands/build.js';
14
16
  import { syncCommand } from '../commands/sync.js';
15
- import { syncDbCommand } from '../commands/syncDb.js';
16
- import { syncApiCommand } from '../commands/syncApi.js';
17
- import { syncMenuCommand } from '../commands/syncMenu.js';
18
- import { syncDevCommand } from '../commands/syncDev.js';
19
17
  import { Logger } from '../lib/logger.js';
20
- import { join } from 'pathe';
21
- import { getPackageVersion } from '../commands/util.js';
22
-
23
- /**
24
- * 读取 package.json 版本号
25
- */
26
- function getVersion(): string {
27
- const coreDir = join(import.meta.dir, '..');
28
- return getPackageVersion(coreDir);
29
- }
30
-
31
- /**
32
- * Bun 版本要求
33
- */
34
- const REQUIRED_BUN_VERSION = '1.3.0';
35
18
 
36
19
  /**
37
- * 比较版本号
20
+ * 主函数
38
21
  */
39
- function compareVersions(v1: string, v2: string): number {
40
- const parts1 = v1.split('.').map(Number);
41
- const parts2 = v2.split('.').map(Number);
42
-
43
- for (let i = 0; i < Math.max(parts1.length, parts2.length); i++) {
44
- const num1 = parts1[i] || 0;
45
- const num2 = parts2[i] || 0;
46
-
47
- if (num1 > num2) return 1;
48
- if (num1 < num2) return -1;
49
- }
50
-
51
- return 0;
52
- }
53
-
54
- /**
55
- * 获取 Bun 版本
56
- */
57
- function getBunVersion(): string | null {
22
+ async function main() {
23
+ // 执行 sync 命令
58
24
  try {
59
- if (typeof Bun !== 'undefined' && Bun.version) {
60
- return Bun.version;
61
- }
62
-
63
- const proc = Bun.spawnSync(['bun', '--version'], {
64
- stdout: 'pipe',
65
- stderr: 'pipe'
66
- });
67
-
68
- if (proc.exitCode === 0) {
69
- const version = proc.stdout.toString().trim();
70
- return version;
71
- }
72
-
73
- return null;
74
- } catch {
75
- return null;
76
- }
77
- }
78
-
79
- /**
80
- * 检查 Bun 版本
81
- */
82
- function checkBunVersion(): void {
83
- const currentVersion = getBunVersion();
84
-
85
- if (!currentVersion) {
86
- Logger.error('未检测到 Bun 运行时');
87
- Logger.info('\nBefly CLI 需要 Bun v1.3.0 或更高版本');
88
- Logger.info('请访问 https://bun.sh 安装 Bun\n');
89
- Logger.info('安装命令:');
90
- Logger.info(' Windows (PowerShell): powershell -c "irm bun.sh/install.ps1 | iex"');
91
- Logger.info(' macOS/Linux: curl -fsSL https://bun.sh/install | bash\n');
92
- process.exit(1);
93
- }
94
-
95
- const comparison = compareVersions(currentVersion, REQUIRED_BUN_VERSION);
96
-
97
- if (comparison < 0) {
98
- Logger.error(`Bun 版本过低: ${currentVersion}`);
99
- Logger.info(`\n需要 Bun v${REQUIRED_BUN_VERSION} 或更高版本`);
100
- Logger.info('请升级 Bun:\n');
101
- Logger.info(' bun upgrade\n');
25
+ await syncCommand();
26
+ Logger.printEnv();
27
+ } catch (error: any) {
28
+ Logger.error('命令执行失败:', error.message || error);
102
29
  process.exit(1);
103
30
  }
104
31
  }
105
32
 
106
- // 检查 Bun 版本
107
- checkBunVersion();
108
-
109
- const program = new Command();
110
-
111
- program.name('befly').description('Befly CLI - 为 Befly 框架提供命令行工具').version(getVersion());
112
-
113
- /**
114
- * 包装命令处理函数,在执行后打印环境
115
- */
116
- function wrapCommand<T extends (...args: any[]) => any>(fn: T): T {
117
- return (async (...args: any[]) => {
118
- const result = await fn(...args);
119
- Logger.printEnv();
120
- return result;
121
- }) as T;
122
- }
123
-
124
- // build 命令 - 构建项目
125
- program.command('build').description('构建项目').option('-o, --outdir <path>', '输出目录', 'dist').option('--minify', '压缩代码', false).option('--sourcemap', '生成 sourcemap', false).action(wrapCommand(buildCommand));
126
-
127
- // sync 命令 - 一次性执行所有同步
128
- program.command('sync').description('一次性执行所有同步操作(syncApi + syncMenu + syncDev)').option('--plan', '计划模式,只显示不执行', false).option('-e, --env <environment>', '指定环境 (development, production, test)').action(wrapCommand(syncCommand));
129
-
130
- // syncDb 命令 - 同步数据库
131
- program.command('syncDb').description('同步数据库表结构').option('-t, --table <name>', '指定表名').option('--dry-run', '预览模式,只显示不执行', false).option('-e, --env <environment>', '指定环境 (development, production, test)').action(wrapCommand(syncDbCommand));
132
-
133
- // syncApi 命令 - 同步 API 接口
134
- program.command('syncApi').description('同步 API 接口到数据库').option('--plan', '计划模式,只显示不执行', false).option('-e, --env <environment>', '指定环境 (development, production, test)').action(wrapCommand(syncApiCommand));
135
-
136
- // syncMenu 命令 - 同步菜单
137
- program.command('syncMenu').description('同步菜单配置到数据库').option('--plan', '计划模式,只显示不执行', false).option('-e, --env <environment>', '指定环境 (development, production, test)').action(wrapCommand(syncMenuCommand));
138
-
139
- // syncDev 命令 - 同步开发者账号
140
- program.command('syncDev').description('同步开发者管理员账号').option('--plan', '计划模式,只显示不执行', false).option('-e, --env <environment>', '指定环境 (development, production, test)').action(wrapCommand(syncDevCommand));
141
-
142
- // 显示建议和错误
143
- program.showSuggestionAfterError();
144
- program.showHelpAfterError();
145
-
146
- // 解析命令行参数
147
- program.parse();
33
+ // 运行主函数
34
+ main();
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 资源冲突检测
3
3
  * 在系统启动前检测表名、API 路由、插件名等资源是否存在冲突
4
4
  */
package/checks/table.ts CHANGED
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * 表规则检查器 - TypeScript 版本
3
3
  * 验证表定义文件的格式和规则
4
4
  */
@@ -221,7 +221,7 @@ export default async function (): Promise<boolean> {
221
221
 
222
222
  if (fileValid) {
223
223
  validFiles++;
224
- Logger.info(`${fileType}表 ${fileName} 验证通过(${fileRules} 个字段)`);
224
+ // Logger.info(`${fileType}表 ${fileName} 验证通过(${fileRules} 个字段)`);
225
225
  } else {
226
226
  invalidFiles++;
227
227
  }
@@ -232,17 +232,15 @@ export default async function (): Promise<boolean> {
232
232
  }
233
233
 
234
234
  // 输出统计信息
235
- Logger.info(`表定义检查完成:`);
236
- Logger.info(` 总文件数: ${totalFiles}`);
237
- Logger.info(` 总规则数: ${totalRules}`);
238
- Logger.info(` 通过文件: ${validFiles}`);
239
- Logger.info(` 失败文件: ${invalidFiles}`);
235
+ // Logger.info(` 总文件数: ${totalFiles}`);
236
+ // Logger.info(` 总规则数: ${totalRules}`);
237
+ // Logger.info(` 通过文件: ${validFiles}`);
238
+ // Logger.info(` 失败文件: ${invalidFiles}`);
240
239
 
241
240
  if (invalidFiles > 0) {
242
241
  Logger.warn(`表定义检查失败,请修复上述错误后重试`);
243
242
  return false;
244
243
  } else {
245
- Logger.info(`所有表定义检查通过 ✓`);
246
244
  return true;
247
245
  }
248
246
  } catch (error: any) {