index.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { colors, consola } from '@vben/node-utils';
  2. import { cac } from 'cac';
  3. import { version } from '../package.json';
  4. import { defineCheckCircularCommand } from './check-circular';
  5. import { defineCheckDepCommand } from './check-dep';
  6. import { defineCodeWorkspaceCommand } from './code-workspace';
  7. import { defineLintCommand } from './lint';
  8. import { definePubLintCommand } from './publint';
  9. // 命令描述
  10. const COMMAND_DESCRIPTIONS = {
  11. 'check-circular': 'Check for circular dependencies',
  12. 'check-dep': 'Check for unused dependencies',
  13. 'code-workspace': 'Manage VS Code workspace settings',
  14. lint: 'Run linting on the project',
  15. publint: 'Check package.json files for publishing standards',
  16. } as const;
  17. /**
  18. * Initialize and run the CLI
  19. */
  20. async function main(): Promise<void> {
  21. try {
  22. const vsh = cac('vsh');
  23. // Register commands
  24. defineLintCommand(vsh);
  25. definePubLintCommand(vsh);
  26. defineCodeWorkspaceCommand(vsh);
  27. defineCheckCircularCommand(vsh);
  28. defineCheckDepCommand(vsh);
  29. // Set up CLI
  30. vsh.usage('vsh <command> [options]');
  31. vsh.help();
  32. vsh.version(version);
  33. // Parse arguments without auto-running to detect unknown commands
  34. // Note: cac v7 removed EventEmitter; use matchedCommand after parse instead
  35. vsh.parse(undefined, { run: false });
  36. if (!vsh.matchedCommand && vsh.args.length > 0) {
  37. const unknownCmd = String(vsh.args[0]);
  38. consola.error(
  39. colors.red(`Invalid command: ${unknownCmd}`),
  40. '\n',
  41. colors.yellow('Available commands:'),
  42. '\n',
  43. Object.entries(COMMAND_DESCRIPTIONS)
  44. .map(([name, desc]) => ` ${colors.cyan(name)} - ${desc}`)
  45. .join('\n'),
  46. );
  47. process.exit(1);
  48. }
  49. await vsh.runMatchedCommand();
  50. } catch (error) {
  51. consola.error(
  52. colors.red('An unexpected error occurred:'),
  53. '\n',
  54. error instanceof Error ? error.message : error,
  55. );
  56. process.exit(1);
  57. }
  58. }
  59. // Run the CLI
  60. main().catch((error) => {
  61. consola.error(
  62. colors.red('Failed to start CLI:'),
  63. '\n',
  64. error instanceof Error ? error.message : error,
  65. );
  66. process.exit(1);
  67. });