index.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import type { CAC } from 'cac';
  2. import { extname } from 'node:path';
  3. import { getStagedFiles } from '@vben/node-utils';
  4. import { circularDepsDetect, printCircles } from 'circular-dependency-scanner';
  5. const IGNORE_DIR = [
  6. 'dist',
  7. '.turbo',
  8. 'output',
  9. '.cache',
  10. 'scripts',
  11. 'internal',
  12. 'packages/@core/forward/request/src/',
  13. 'packages/@core/ui-kit/menu-ui/src/',
  14. ].join(',');
  15. const IGNORE = [`**/{${IGNORE_DIR}}/**`];
  16. interface CommandOptions {
  17. staged: boolean;
  18. verbose: boolean;
  19. }
  20. async function checkCircular({ staged, verbose }: CommandOptions) {
  21. const results = await circularDepsDetect({
  22. absolute: staged,
  23. cwd: process.cwd(),
  24. ignore: IGNORE,
  25. });
  26. if (staged) {
  27. let files = await getStagedFiles();
  28. const allowedExtensions = new Set([
  29. '.cjs',
  30. '.js',
  31. '.jsx',
  32. '.mjs',
  33. '.ts',
  34. '.tsx',
  35. '.vue',
  36. ]);
  37. // 过滤文件列表
  38. files = files.filter((file) => allowedExtensions.has(extname(file)));
  39. const circularFiles: string[][] = [];
  40. for (const file of files) {
  41. for (const result of results) {
  42. const resultFiles = result.flat();
  43. if (resultFiles.includes(file)) {
  44. circularFiles.push(result);
  45. }
  46. }
  47. }
  48. verbose && printCircles(circularFiles);
  49. } else {
  50. verbose && printCircles(results);
  51. }
  52. }
  53. function defineCheckCircularCommand(cac: CAC) {
  54. cac
  55. .command('check-circular')
  56. .option(
  57. '--staged',
  58. 'Whether it is the staged commit mode, in which mode, if there is a circular dependency, an alarm will be given.',
  59. )
  60. .usage(`Analysis of project circular dependencies.`)
  61. .action(async ({ staged }) => {
  62. await checkCircular({ staged, verbose: true });
  63. });
  64. }
  65. export { defineCheckCircularCommand };