index.ts 1.8 KB

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