@zhin.js/cli 1.0.0 → 1.0.1

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 (59) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/LICENSE +21 -0
  3. package/README.md +244 -56
  4. package/{dist → lib}/commands/build.d.ts.map +1 -1
  5. package/{dist → lib}/commands/build.js +3 -2
  6. package/lib/commands/build.js.map +1 -0
  7. package/{dist → lib}/commands/dev.d.ts.map +1 -1
  8. package/{dist → lib}/commands/dev.js +2 -1
  9. package/lib/commands/dev.js.map +1 -0
  10. package/{dist → lib}/commands/init.js +194 -140
  11. package/lib/commands/init.js.map +1 -0
  12. package/{dist → lib}/utils/logger.d.ts +1 -0
  13. package/lib/utils/logger.d.ts.map +1 -0
  14. package/lib/utils/logger.js +79 -0
  15. package/lib/utils/logger.js.map +1 -0
  16. package/{dist → lib}/utils/process.d.ts.map +1 -1
  17. package/{dist → lib}/utils/process.js +2 -1
  18. package/lib/utils/process.js.map +1 -0
  19. package/package.json +9 -7
  20. package/src/commands/build.ts +3 -2
  21. package/src/commands/dev.ts +2 -1
  22. package/src/commands/init.ts +200 -140
  23. package/src/commands/start.ts +1 -1
  24. package/src/utils/logger.ts +81 -10
  25. package/src/utils/process.ts +2 -1
  26. package/tsconfig.json +3 -4
  27. package/dist/commands/build.js.map +0 -1
  28. package/dist/commands/dev.js.map +0 -1
  29. package/dist/commands/init.js.map +0 -1
  30. package/dist/utils/logger.d.ts.map +0 -1
  31. package/dist/utils/logger.js +0 -18
  32. package/dist/utils/logger.js.map +0 -1
  33. package/dist/utils/process.js.map +0 -1
  34. package/tsconfig.tsbuildinfo +0 -1
  35. /package/{dist → lib}/cli.d.ts +0 -0
  36. /package/{dist → lib}/cli.d.ts.map +0 -0
  37. /package/{dist → lib}/cli.js +0 -0
  38. /package/{dist → lib}/cli.js.map +0 -0
  39. /package/{dist → lib}/commands/build.d.ts +0 -0
  40. /package/{dist → lib}/commands/dev.d.ts +0 -0
  41. /package/{dist → lib}/commands/init.d.ts +0 -0
  42. /package/{dist → lib}/commands/init.d.ts.map +0 -0
  43. /package/{dist → lib}/commands/start.d.ts +0 -0
  44. /package/{dist → lib}/commands/start.d.ts.map +0 -0
  45. /package/{dist → lib}/commands/start.js +0 -0
  46. /package/{dist → lib}/commands/start.js.map +0 -0
  47. /package/{dist → lib}/commands/stop.d.ts +0 -0
  48. /package/{dist → lib}/commands/stop.d.ts.map +0 -0
  49. /package/{dist → lib}/commands/stop.js +0 -0
  50. /package/{dist → lib}/commands/stop.js.map +0 -0
  51. /package/{dist → lib}/index.d.ts +0 -0
  52. /package/{dist → lib}/index.d.ts.map +0 -0
  53. /package/{dist → lib}/index.js +0 -0
  54. /package/{dist → lib}/index.js.map +0 -0
  55. /package/{dist → lib}/utils/env.d.ts +0 -0
  56. /package/{dist → lib}/utils/env.d.ts.map +0 -0
  57. /package/{dist → lib}/utils/env.js +0 -0
  58. /package/{dist → lib}/utils/env.js.map +0 -0
  59. /package/{dist → lib}/utils/process.d.ts +0 -0
@@ -152,27 +152,40 @@ async function createProjectStructure(projectPath: string, projectName: string,
152
152
  await fs.ensureDir(path.join(projectPath, 'dist'));
153
153
  await fs.ensureDir(path.join(projectPath, 'data'));
154
154
 
155
+ // 检查是否在工作区中
156
+ const isInWorkspace = await checkIfInWorkspace();
157
+ const versionSuffix = isInWorkspace ? 'workspace:*' : 'latest';
158
+
155
159
  // 创建 package.json
156
160
  const packageJson = {
157
161
  name: projectName,
162
+ private: true,
158
163
  version: '0.1.0',
159
164
  description: `${projectName} 机器人`,
160
165
  type: 'module',
161
166
  main: 'src/index.ts',
162
167
  scripts: {
163
- dev: options.runtime === 'bun' ? 'zhin dev --bun' : 'zhin dev',
168
+ dev: 'zhin dev',
164
169
  start: options.runtime === 'bun' ? 'zhin start --bun' : 'zhin start',
165
170
  daemon: options.runtime === 'bun' ? 'zhin start --bun --daemon' : 'zhin start --daemon',
166
171
  build: 'zhin build',
167
172
  stop: 'zhin stop'
168
173
  },
169
174
  dependencies: {
170
- '@zhin.js/core': 'workspace:*'
175
+ 'zhin.js': versionSuffix,
176
+ '@zhin.js/adapter-process': versionSuffix,
177
+ '@zhin.js/http': versionSuffix,
178
+ '@zhin.js/console': versionSuffix
171
179
  },
172
180
  devDependencies: {
173
- '@zhin.js/cli': 'workspace:*',
174
- 'typescript': '^5.0.0',
175
- ...(options.runtime === 'node' && { 'tsx': '^4.0.0' })
181
+ '@zhin.js/cli': versionSuffix,
182
+ '@zhin.js/types': versionSuffix,
183
+ 'typescript': 'latest',
184
+ ...(options.runtime === 'bun' ? {
185
+ 'bun': 'latest'
186
+ } : {
187
+ 'tsx': 'latest'
188
+ })
176
189
  },
177
190
  engines: {
178
191
  node: '>=18.0.0'
@@ -200,10 +213,14 @@ async function createProjectStructure(projectPath: string, projectName: string,
200
213
  declaration: false,
201
214
  sourceMap: true,
202
215
  baseUrl: './src',
203
- paths: {
204
- '@zhin.js/core': ['../../packages/core/src/index.ts'],
205
- '@zhin.js/core/*': ['../../packages/core/src/*']
206
- }
216
+ types: [
217
+ '@types/node',
218
+ '@zhin.js/types',
219
+ 'zhin.js',
220
+ '@zhin.js/http',
221
+ '@zhin.js/adapter-process',
222
+ '@zhin.js/console'
223
+ ]
207
224
  },
208
225
  include: ['src/**/*'],
209
226
  exclude: ['dist', 'node_modules']
@@ -215,7 +232,7 @@ async function createProjectStructure(projectPath: string, projectName: string,
215
232
  await createConfigFile(projectPath, options.config!);
216
233
 
217
234
  // 创建主入口文件
218
- const indexContent = `import { createApp } from '@zhin.js/core';
235
+ const indexContent = `import { createApp } from 'zhin.js';
219
236
 
220
237
  // 启动机器人
221
238
  async function main() {
@@ -230,10 +247,10 @@ async function main() {
230
247
  process.exit(0);
231
248
  };
232
249
 
233
- process.on('SIGINT', shutdown);
234
- process.on('SIGTERM', shutdown);
250
+ process.on('SIGINT', () => shutdown('SIGINT'));
251
+ process.on('SIGTERM', () => shutdown('SIGTERM'));
235
252
  } catch (error) {
236
- console.error('机器人启动失败:', error);
253
+ // console.error 已替换为注释
237
254
  process.exit(1);
238
255
  }
239
256
  }
@@ -246,45 +263,75 @@ main().catch(console.error);
246
263
 
247
264
  // 创建示例插件
248
265
  const pluginContent = `import {
266
+ useLogger,
267
+ onMessage,
268
+ addCommand,
269
+ addMiddleware,
270
+ MessageCommand,
271
+ useContext,
249
272
  onDispose,
250
- addMiddleware, useContext, sendMessage, beforeSend, onGroupMessage,
251
- } from '@zhin.js/core';
252
- import * as process from "node:process";
253
-
254
- onDispose(async ()=>{
255
- console.log('插件已销毁')
256
- })
257
-
258
- addMiddleware(async (message, next)=>{ // 添加中间件到插件
259
- // 在这里处理消息
260
- return next()
261
- })
262
-
263
- let hasChanged=false
264
- beforeSend((options)=>{
265
- if(!hasChanged){
266
- options.content='bar'
267
- hasChanged=true
268
- }
269
- return options
270
- })
273
+ } from 'zhin.js';
271
274
 
272
- onGroupMessage((m)=>{
273
- if(m.channel.id==='629336764'){
274
- m.reply('hello')
275
- }
276
- })
277
-
278
- // 依赖process上下文
279
- useContext('process',()=>{
280
- sendMessage({
281
- context:'process',
282
- bot:\`\${process.pid}\`,
283
- id:process.title,
284
- type:'private',
285
- content:'foo'
275
+ const logger = useLogger();
276
+
277
+ // 添加命令
278
+ addCommand(new MessageCommand('hello')
279
+ .action(async (message) => {
280
+ logger.info('Hello command called by:', message.sender.name);
281
+ return '你好!欢迎使用 Zhin 机器人框架!';
282
+ })
283
+ );
284
+
285
+ addCommand(new MessageCommand('status')
286
+ .action(() => {
287
+ const uptime = process.uptime() * 1000;
288
+ const memory = process.memoryUsage();
289
+ return [
290
+ '🤖 机器人状态',
291
+ \`⏱️ 运行时间: \${formatTime(uptime)}\`,
292
+ \`📊 内存使用: \${(memory.rss / 1024 / 1024).toFixed(2)}MB\`,
293
+ \`🔧 Node.js: \${process.version}\`
294
+ ].join('\\n');
286
295
  })
287
- })
296
+ );
297
+
298
+ // 添加中间件
299
+ addMiddleware(async (message, next) => {
300
+ logger.info(\`收到消息: \${message.raw}\`);
301
+ await next();
302
+ });
303
+
304
+ // 监听消息
305
+ onMessage(async (message) => {
306
+ if (message.raw.includes('帮助')) {
307
+ await message.reply('可用命令:hello, status\\n输入命令即可使用!');
308
+ }
309
+ });
310
+
311
+ // 使用 process 上下文
312
+ useContext('process', () => {
313
+ logger.info('Process 适配器已就绪,可以在控制台输入消息进行测试');
314
+ });
315
+
316
+ // 插件销毁时的清理
317
+ onDispose(() => {
318
+ logger.info('测试插件已销毁');
319
+ });
320
+
321
+ // 工具函数
322
+ function formatTime(ms: number): string {
323
+ const seconds = Math.floor(ms / 1000);
324
+ const minutes = Math.floor(seconds / 60);
325
+ const hours = Math.floor(minutes / 60);
326
+ const days = Math.floor(hours / 24);
327
+
328
+ if (days > 0) return \`\${days}天 \${hours % 24}小时\`;
329
+ if (hours > 0) return \`\${hours}小时 \${minutes % 60}分钟\`;
330
+ if (minutes > 0) return \`\${minutes}分钟 \${seconds % 60}秒\`;
331
+ return \`\${seconds}秒\`;
332
+ }
333
+
334
+ logger.info('测试插件已加载');
288
335
  `;
289
336
 
290
337
  await fs.writeFile(path.join(projectPath, 'src', 'plugins', 'test-plugin.ts'), pluginContent);
@@ -487,6 +534,30 @@ MIT License
487
534
  `;
488
535
  await fs.writeFile(path.join(projectPath, 'pnpm-workspace.yaml'), workspaceContent);
489
536
  }
537
+
538
+ // 创建环境变量示例文件
539
+ const envExampleContent = `# Zhin Bot 环境变量配置示例
540
+ # 复制为 .env 文件并根据需要修改
541
+
542
+ # 调试模式
543
+ DEBUG=true
544
+
545
+ # 插件目录 (可选)
546
+ # PLUGIN_DIR=./src/plugins
547
+
548
+ # KOOK 机器人配置 (如果使用 KOOK 适配器)
549
+ # KOOK_TOKEN=your-kook-token
550
+
551
+ # ICQQ 机器人配置 (如果使用 ICQQ 适配器)
552
+ # ICQQ_SCAN_UIN=your-qq-number
553
+ # ICQQ_LOGIN_UIN=your-qq-number
554
+ # ICQQ_SIGN_ADDR=http://localhost:8080
555
+
556
+ # OneBot 机器人配置 (如果使用 OneBot 适配器)
557
+ # BOT_URL=ws://localhost:8080
558
+ # ACCESS_TOKEN=your-access-token
559
+ `;
560
+ await fs.writeFile(path.join(projectPath, '.env.example'), envExampleContent);
490
561
  }
491
562
 
492
563
  async function createConfigFile(projectPath: string, format: string) {
@@ -498,7 +569,7 @@ async function createConfigFile(projectPath: string, format: string) {
498
569
  fileName = 'zhin.config.ts';
499
570
  break;
500
571
  case 'js':
501
- fileName = 'zhin.config.js';
572
+ fileName = 'zhin.config.ts';
502
573
  break;
503
574
  default:
504
575
  fileName = `zhin.config.${format}`;
@@ -515,12 +586,6 @@ function getConfigContent(format: string): string {
515
586
  {
516
587
  name: `${process.pid}`,
517
588
  context: 'process'
518
- },
519
- {
520
- name: '1689919782',
521
- context: 'icqq',
522
- log_level: 'off',
523
- platform: 4
524
589
  }
525
590
  ],
526
591
  plugin_dirs: [
@@ -528,8 +593,9 @@ function getConfigContent(format: string): string {
528
593
  'node_modules'
529
594
  ],
530
595
  plugins: [
531
- 'icqq',
532
- 'process',
596
+ 'adapter-process',
597
+ 'http',
598
+ 'console',
533
599
  'test-plugin'
534
600
  ],
535
601
  debug: false
@@ -542,10 +608,6 @@ function getConfigContent(format: string): string {
542
608
  bots:
543
609
  - name: \${process.pid}
544
610
  context: process
545
- - name: '1689919782'
546
- context: icqq
547
- log_level: off
548
- platform: 4
549
611
 
550
612
  # 插件目录
551
613
  plugin_dirs:
@@ -554,8 +616,9 @@ plugin_dirs:
554
616
 
555
617
  # 要加载的插件列表
556
618
  plugins:
557
- - icqq
558
- - process
619
+ - adapter-process
620
+ - http
621
+ - console
559
622
  - test-plugin
560
623
 
561
624
  # 调试模式
@@ -570,92 +633,80 @@ debug: false
570
633
  name = "\${process.pid}"
571
634
  context = "process"
572
635
 
573
- [[bots]]
574
- name = "1689919782"
575
- context = "icqq"
576
- log_level = "off"
577
- platform = 4
578
-
579
636
  # 插件目录
580
637
  plugin_dirs = ["./src/plugins", "node_modules"]
581
638
 
582
639
  # 要加载的插件列表
583
- plugins = ["icqq", "process", "test-plugin"]
640
+ plugins = ["adapter-process", "http", "console", "test-plugin"]
584
641
 
585
642
  # 调试模式
586
643
  debug = false
587
644
  `;
588
645
 
589
646
  case 'ts':
590
- return `import { defineConfig } from '@zhin.js/core';
647
+ return `import { defineConfig } from 'zhin.js';
591
648
 
592
- export default defineConfig(async (env)=>{
649
+ export default defineConfig(async (env) => {
593
650
  return {
594
651
  // 机器人配置
595
652
  bots: [
596
653
  {
597
654
  name: \`\${process.pid}\`,
598
655
  context: 'process'
599
- },
600
- {
601
- name: '1689919782',
602
- context: 'icqq',
603
- log_level: 'off',
604
- platform: 4
605
656
  }
606
657
  ],
658
+
607
659
  // 插件目录
608
660
  plugin_dirs: [
609
661
  env.PLUGIN_DIR || './src/plugins',
610
662
  'node_modules'
611
663
  ],
664
+
612
665
  // 要加载的插件列表
613
666
  plugins: [
614
- 'icqq',
615
- 'process',
667
+ 'adapter-process',
668
+ 'http',
669
+ 'console',
616
670
  'test-plugin'
617
671
  ],
618
672
 
619
673
  // 调试模式
620
674
  debug: env.DEBUG === 'true'
621
- }
622
- })
675
+ };
676
+ });
623
677
  `;
624
678
 
625
679
  case 'js':
626
- return `import { defineConfig } from '@zhin.js/core';
680
+ return `import { defineConfig } from 'zhin.js';
627
681
 
628
- export default defineConfig(async (env)=>{
682
+ export default defineConfig(async (env) => {
629
683
  return {
630
684
  // 机器人配置
631
685
  bots: [
632
686
  {
633
687
  name: \`\${process.pid}\`,
634
688
  context: 'process'
635
- },
636
- {
637
- name: '1689919782',
638
- context: 'icqq',
639
- log_level: 'off',
640
- platform: 4
641
689
  }
642
690
  ],
691
+
643
692
  // 插件目录
644
693
  plugin_dirs: [
645
694
  env.PLUGIN_DIR || './src/plugins',
646
695
  'node_modules'
647
696
  ],
697
+
648
698
  // 要加载的插件列表
649
699
  plugins: [
650
- 'icqq',
651
- 'process',
700
+ 'adapter-process',
701
+ 'http',
702
+ 'console',
652
703
  'test-plugin'
653
704
  ],
654
705
 
655
706
  // 调试模式
656
707
  debug: env.DEBUG === 'true'
657
- }
658
- })
708
+ };
709
+ });
659
710
  `;
660
711
 
661
712
  default:
@@ -672,12 +723,6 @@ function getConfigExample(format: string): string {
672
723
  {
673
724
  "name": "\${process.pid}",
674
725
  "context": "process"
675
- },
676
- {
677
- "name": "1689919782",
678
- "context": "icqq",
679
- "log_level": "off",
680
- "platform": 4
681
726
  }
682
727
  ],
683
728
  "plugin_dirs": [
@@ -685,8 +730,9 @@ function getConfigExample(format: string): string {
685
730
  "node_modules"
686
731
  ],
687
732
  "plugins": [
688
- "icqq",
689
- "process",
733
+ "adapter-process",
734
+ "http",
735
+ "console",
690
736
  "test-plugin"
691
737
  ],
692
738
  "debug": false
@@ -701,10 +747,6 @@ function getConfigExample(format: string): string {
701
747
  bots:
702
748
  - name: \${process.pid}
703
749
  context: process
704
- - name: '1689919782'
705
- context: icqq
706
- log_level: off
707
- platform: 4
708
750
 
709
751
  # 插件目录
710
752
  plugin_dirs:
@@ -713,8 +755,9 @@ plugin_dirs:
713
755
 
714
756
  # 要加载的插件列表
715
757
  plugins:
716
- - icqq
717
- - process
758
+ - adapter-process
759
+ - http
760
+ - console
718
761
  - test-plugin
719
762
 
720
763
  # 调试模式
@@ -730,17 +773,11 @@ debug: false
730
773
  name = "\${process.pid}"
731
774
  context = "process"
732
775
 
733
- [[bots]]
734
- name = "1689919782"
735
- context = "icqq"
736
- log_level = "off"
737
- platform = 4
738
-
739
776
  # 插件目录
740
777
  plugin_dirs = ["./src/plugins", "node_modules"]
741
778
 
742
779
  # 要加载的插件列表
743
- plugins = ["icqq", "process", "test-plugin"]
780
+ plugins = ["adapter-process", "http", "console", "test-plugin"]
744
781
 
745
782
  # 调试模式
746
783
  debug = false
@@ -748,79 +785,102 @@ debug = false
748
785
  `;
749
786
  case 'ts':
750
787
  return `\`\`\`typescript
751
- import { defineConfig } from '@zhin.js/core';
788
+ import { defineConfig } from 'zhin.js';
752
789
 
753
- export default defineConfig(async (env)=>{
790
+ export default defineConfig(async (env) => {
754
791
  return {
755
792
  // 机器人配置
756
793
  bots: [
757
794
  {
758
795
  name: \`\${process.pid}\`,
759
796
  context: 'process'
760
- },
761
- {
762
- name: '1689919782',
763
- context: 'icqq',
764
- log_level: 'off',
765
- platform: 4
766
797
  }
767
798
  ],
799
+
768
800
  // 插件目录
769
801
  plugin_dirs: [
770
802
  env.PLUGIN_DIR || './src/plugins',
771
803
  'node_modules'
772
804
  ],
805
+
773
806
  // 要加载的插件列表
774
807
  plugins: [
775
- 'icqq',
776
- 'process',
808
+ 'adapter-process',
809
+ 'http',
810
+ 'console',
777
811
  'test-plugin'
778
812
  ],
779
813
 
780
814
  // 调试模式
781
815
  debug: env.DEBUG === 'true'
782
- }
783
- })
816
+ };
817
+ });
784
818
  \`\`\`
785
819
  `;
786
820
  case 'js':
787
821
  return `\`\`\`javascript
788
- import { defineConfig } from '@zhin.js/core';
822
+ import { defineConfig } from 'zhin.js';
789
823
 
790
- export default defineConfig(async (env)=>{
824
+ export default defineConfig(async (env) => {
791
825
  return {
792
826
  // 机器人配置
793
827
  bots: [
794
828
  {
795
829
  name: \`\${process.pid}\`,
796
830
  context: 'process'
797
- },
798
- {
799
- name: '1689919782',
800
- context: 'icqq',
801
- log_level: 'off',
802
- platform: 4
803
831
  }
804
832
  ],
833
+
805
834
  // 插件目录
806
835
  plugin_dirs: [
807
836
  env.PLUGIN_DIR || './src/plugins',
808
837
  'node_modules'
809
838
  ],
839
+
810
840
  // 要加载的插件列表
811
841
  plugins: [
812
- 'icqq',
813
- 'process',
842
+ 'adapter-process',
843
+ 'http',
844
+ 'console',
814
845
  'test-plugin'
815
846
  ],
816
847
 
817
848
  // 调试模式
818
849
  debug: env.DEBUG === 'true'
819
- }
820
- })
850
+ };
851
+ });
821
852
  \`\`\`
822
853
  `;
823
854
  default:
824
855
  throw new Error(`不支持的配置格式: ${format}`);
825
856
  }
857
+ }
858
+
859
+ async function checkIfInWorkspace(): Promise<boolean> {
860
+ let currentDir = process.cwd();
861
+
862
+ while (currentDir !== path.dirname(currentDir)) {
863
+ // 检查 pnpm-workspace.yaml
864
+ const pnpmWorkspacePath = path.join(currentDir, 'pnpm-workspace.yaml');
865
+ if (fs.existsSync(pnpmWorkspacePath)) {
866
+ return true;
867
+ }
868
+
869
+ // 检查 package.json 中的 workspaces 字段
870
+ const packageJsonPath = path.join(currentDir, 'package.json');
871
+ if (fs.existsSync(packageJsonPath)) {
872
+ try {
873
+ const packageJson = fs.readJsonSync(packageJsonPath);
874
+ if (packageJson.workspaces) {
875
+ return true;
876
+ }
877
+ } catch {
878
+ // 忽略错误,继续向上查找
879
+ }
880
+ }
881
+
882
+ currentDir = path.dirname(currentDir);
883
+ }
884
+
885
+ return false;
826
886
  }
@@ -1,7 +1,7 @@
1
1
  import { Command } from 'commander';
2
2
  import { logger } from '../utils/logger.js';
3
3
  import { loadEnvFiles } from '../utils/env.js';
4
- import { spawn, ChildProcess } from 'child_process';
4
+ import { ChildProcess } from 'child_process';
5
5
  import fs from 'fs-extra';
6
6
  import path from 'path';
7
7
  import { startProcess } from '../utils/process.js';
@@ -1,21 +1,92 @@
1
+ import { getLogger, LogLevel } from '@zhin.js/logger'
2
+
3
+ // 创建CLI专用的logger
4
+ const cliLogger = getLogger('CLI')
5
+
6
+ // 根据环境变量设置日志级别
7
+ const logLevel = process.env.ZHIN_LOG_LEVEL || process.env.NODE_ENV === 'development' ? 'debug' : 'info'
8
+ switch (logLevel.toLowerCase()) {
9
+ case 'debug':
10
+ cliLogger.setLevel(LogLevel.DEBUG)
11
+ break
12
+ case 'warn':
13
+ cliLogger.setLevel(LogLevel.WARN)
14
+ break
15
+ case 'error':
16
+ cliLogger.setLevel(LogLevel.ERROR)
17
+ break
18
+ case 'silent':
19
+ cliLogger.setLevel(LogLevel.SILENT)
20
+ break
21
+ default:
22
+ cliLogger.setLevel(LogLevel.INFO)
23
+ }
24
+
1
25
  export const logger = {
2
- info: (message: string,...args: any[]) => {
3
- console.log('[INFO] [CLI]:', message,...args);
26
+ info: (message: string, ...args: any[]) => {
27
+ if (args.length > 0) {
28
+ // 对于多个参数,直接拼接到消息中,这样更自然
29
+ const fullMessage = message + ' ' + args.map(arg =>
30
+ typeof arg === 'string' ? arg : JSON.stringify(arg)
31
+ ).join(' ')
32
+ cliLogger.info(fullMessage)
33
+ } else {
34
+ cliLogger.info(message)
35
+ }
4
36
  },
5
37
 
6
- success: (message: string,...args: any[]) => {
7
- console.log('[SUCCESS] [CLI]:', message,...args);
38
+ success: (message: string, ...args: any[]) => {
39
+ if (args.length > 0) {
40
+ const fullMessage = message + ' ' + args.map(arg =>
41
+ typeof arg === 'string' ? arg : JSON.stringify(arg)
42
+ ).join(' ')
43
+ cliLogger.success(fullMessage)
44
+ } else {
45
+ cliLogger.success(message)
46
+ }
8
47
  },
9
48
 
10
- warn: (message: string,...args: any[]) => {
11
- console.log('[WARN] [CLI]:', message,...args);
49
+ warn: (message: string, ...args: any[]) => {
50
+ if (args.length > 0) {
51
+ const fullMessage = message + ' ' + args.map(arg =>
52
+ typeof arg === 'string' ? arg : JSON.stringify(arg)
53
+ ).join(' ')
54
+ cliLogger.warn(fullMessage)
55
+ } else {
56
+ cliLogger.warn(message)
57
+ }
12
58
  },
13
59
 
14
- error: (message: string,...args: any[]) => {
15
- console.log('[ERROR] [CLI]:', message,...args);
60
+ error: (message: string, ...args: any[]) => {
61
+ if (args.length > 0) {
62
+ const fullMessage = message + ' ' + args.map(arg =>
63
+ typeof arg === 'string' ? arg : JSON.stringify(arg)
64
+ ).join(' ')
65
+ cliLogger.error(fullMessage)
66
+ } else {
67
+ cliLogger.error(message)
68
+ }
16
69
  },
17
70
 
18
- log: (message: string,...args: any[]) => {
19
- console.log('[LOG] [CLI]:', message,...args);
71
+ log: (message: string, ...args: any[]) => {
72
+ if (args.length > 0) {
73
+ const fullMessage = message + ' ' + args.map(arg =>
74
+ typeof arg === 'string' ? arg : JSON.stringify(arg)
75
+ ).join(' ')
76
+ cliLogger.info(fullMessage)
77
+ } else {
78
+ cliLogger.info(message)
79
+ }
80
+ },
81
+
82
+ debug: (message: string, ...args: any[]) => {
83
+ if (args.length > 0) {
84
+ const fullMessage = message + ' ' + args.map(arg =>
85
+ typeof arg === 'string' ? arg : JSON.stringify(arg)
86
+ ).join(' ')
87
+ cliLogger.debug(fullMessage)
88
+ } else {
89
+ cliLogger.debug(message)
90
+ }
20
91
  }
21
92
  };