index.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import cac from 'cac';
  2. import { blue, lightGreen } from 'kolorist';
  3. import { version } from '../package.json';
  4. import { cleanup, genChangelog, generateRoute, gitCommit, gitCommitVerify, release, updatePkg } from './commands';
  5. import { loadCliOptions } from './config';
  6. import type { Lang } from './locales';
  7. type Command = 'cleanup' | 'update-pkg' | 'git-commit' | 'git-commit-verify' | 'changelog' | 'release' | 'gen-route';
  8. type CommandAction<A extends object> = (args?: A) => Promise<void> | void;
  9. type CommandWithAction<A extends object = object> = Record<Command, { desc: string; action: CommandAction<A> }>;
  10. interface CommandArg {
  11. /** Execute additional command after bumping and before git commit. Defaults to 'pnpm sa changelog' */
  12. execute?: string;
  13. /** Indicates whether to push the git commit and tag. Defaults to true */
  14. push?: boolean;
  15. /** Generate changelog by total tags */
  16. total?: boolean;
  17. /**
  18. * The glob pattern of dirs to clean up
  19. *
  20. * If not set, it will use the default value
  21. *
  22. * Multiple values use "," to separate them
  23. */
  24. cleanupDir?: string;
  25. /**
  26. * display lang of cli
  27. *
  28. * @default 'en-us'
  29. */
  30. lang?: Lang;
  31. }
  32. export async function setupCli() {
  33. const cliOptions = await loadCliOptions();
  34. const cli = cac(blue('soybean-admin'));
  35. cli
  36. .version(lightGreen(version))
  37. .option(
  38. '-e, --execute [command]',
  39. "Execute additional command after bumping and before git commit. Defaults to 'npx soy changelog'"
  40. )
  41. .option('-p, --push', 'Indicates whether to push the git commit and tag')
  42. .option('-t, --total', 'Generate changelog by total tags')
  43. .option(
  44. '-c, --cleanupDir <dir>',
  45. 'The glob pattern of dirs to cleanup, If not set, it will use the default value, Multiple values use "," to separate them'
  46. )
  47. .option('-l, --lang <lang>', 'display lang of cli', { default: 'en-us', type: [String] })
  48. .help();
  49. const commands: CommandWithAction<CommandArg> = {
  50. cleanup: {
  51. desc: 'delete dirs: node_modules, dist, etc.',
  52. action: async () => {
  53. await cleanup(cliOptions.cleanupDirs);
  54. }
  55. },
  56. 'update-pkg': {
  57. desc: 'update package.json dependencies versions',
  58. action: async () => {
  59. await updatePkg(cliOptions.ncuCommandArgs);
  60. }
  61. },
  62. 'git-commit': {
  63. desc: 'git commit, generate commit message which match Conventional Commits standard',
  64. action: async args => {
  65. await gitCommit(args?.lang);
  66. }
  67. },
  68. 'git-commit-verify': {
  69. desc: 'verify git commit message, make sure it match Conventional Commits standard',
  70. action: async args => {
  71. await gitCommitVerify(args?.lang, cliOptions.gitCommitVerifyIgnores);
  72. }
  73. },
  74. changelog: {
  75. desc: 'generate changelog',
  76. action: async args => {
  77. await genChangelog(cliOptions.changelogOptions, args?.total);
  78. }
  79. },
  80. release: {
  81. desc: 'release: update version, generate changelog, commit code',
  82. action: async args => {
  83. await release(args?.execute, args?.push);
  84. }
  85. },
  86. 'gen-route': {
  87. desc: 'generate route',
  88. action: async () => {
  89. await generateRoute();
  90. }
  91. }
  92. };
  93. for (const [command, { desc, action }] of Object.entries(commands)) {
  94. cli.command(command, lightGreen(desc)).action(action);
  95. }
  96. cli.parse();
  97. }
  98. setupCli();