index.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 { defineDepcheckCommand } 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. defineDepcheckCommand(vsh);
  29. // Handle invalid commands
  30. vsh.on('command:*', ([cmd]) => {
  31. consola.error(
  32. colors.red(`Invalid command: ${cmd}`),
  33. '\n',
  34. colors.yellow('Available commands:'),
  35. '\n',
  36. Object.entries(COMMAND_DESCRIPTIONS)
  37. .map(([cmd, desc]) => ` ${colors.cyan(cmd)} - ${desc}`)
  38. .join('\n'),
  39. );
  40. process.exit(1);
  41. });
  42. // Set up CLI
  43. vsh.usage('vsh <command> [options]');
  44. vsh.help();
  45. vsh.version(version);
  46. // Parse arguments
  47. vsh.parse();
  48. } catch (error) {
  49. consola.error(
  50. colors.red('An unexpected error occurred:'),
  51. '\n',
  52. error instanceof Error ? error.message : error,
  53. );
  54. process.exit(1);
  55. }
  56. }
  57. // Run the CLI
  58. main().catch((error) => {
  59. consola.error(
  60. colors.red('Failed to start CLI:'),
  61. '\n',
  62. error instanceof Error ? error.message : error,
  63. );
  64. process.exit(1);
  65. });