chore: update dependencies and documentation, optimize build toolchain (#6060)
* chore: update packageManager version to pnpm@10.9.0 for compatibility improvements * chore: Update dependent versions and configurations to improve compatibility and stability - Update Node version to 22.1.0 - Updated pnpm version to 10.10.0 - Fixed syntax error in prettier command in lintstagedrc - Update dependent versions in pnpm-lock.yaml to ensure consistency - Update format and content in README documents to improve readability * fix: lint error
This commit is contained in:
@@ -4,77 +4,167 @@ import { extname } from 'node:path';
|
||||
|
||||
import { getStagedFiles } from '@vben/node-utils';
|
||||
|
||||
import { circularDepsDetect, printCircles } from 'circular-dependency-scanner';
|
||||
import { circularDepsDetect } from 'circular-dependency-scanner';
|
||||
|
||||
const IGNORE_DIR = [
|
||||
'dist',
|
||||
'.turbo',
|
||||
'output',
|
||||
'.cache',
|
||||
'scripts',
|
||||
'internal',
|
||||
'packages/effects/request/src/',
|
||||
'packages/@core/ui-kit/menu-ui/src/',
|
||||
'packages/@core/ui-kit/popup-ui/src/',
|
||||
].join(',');
|
||||
// 默认配置
|
||||
const DEFAULT_CONFIG = {
|
||||
allowedExtensions: ['.cjs', '.js', '.jsx', '.mjs', '.ts', '.tsx', '.vue'],
|
||||
ignoreDirs: [
|
||||
'dist',
|
||||
'.turbo',
|
||||
'output',
|
||||
'.cache',
|
||||
'scripts',
|
||||
'internal',
|
||||
'packages/effects/request/src/',
|
||||
'packages/@core/ui-kit/menu-ui/src/',
|
||||
'packages/@core/ui-kit/popup-ui/src/',
|
||||
],
|
||||
threshold: 0, // 循环依赖的阈值
|
||||
} as const;
|
||||
|
||||
const IGNORE = [`**/{${IGNORE_DIR}}/**`];
|
||||
// 类型定义
|
||||
type CircularDependencyResult = string[];
|
||||
|
||||
interface CheckCircularConfig {
|
||||
allowedExtensions?: string[];
|
||||
ignoreDirs?: string[];
|
||||
threshold?: number;
|
||||
}
|
||||
|
||||
interface CommandOptions {
|
||||
config?: CheckCircularConfig;
|
||||
staged: boolean;
|
||||
verbose: boolean;
|
||||
}
|
||||
|
||||
async function checkCircular({ staged, verbose }: CommandOptions) {
|
||||
const results = await circularDepsDetect({
|
||||
absolute: staged,
|
||||
cwd: process.cwd(),
|
||||
ignore: IGNORE,
|
||||
// 缓存机制
|
||||
const cache = new Map<string, CircularDependencyResult[]>();
|
||||
|
||||
/**
|
||||
* 格式化循环依赖的输出
|
||||
* @param circles - 循环依赖结果
|
||||
*/
|
||||
function formatCircles(circles: CircularDependencyResult[]): void {
|
||||
if (circles.length === 0) {
|
||||
console.log('✅ No circular dependencies found');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('⚠️ Circular dependencies found:');
|
||||
circles.forEach((circle, index) => {
|
||||
console.log(`\nCircular dependency #${index + 1}:`);
|
||||
circle.forEach((file) => console.log(` → ${file}`));
|
||||
});
|
||||
}
|
||||
|
||||
if (staged) {
|
||||
let files = await getStagedFiles();
|
||||
/**
|
||||
* 检查项目中的循环依赖
|
||||
* @param options - 检查选项
|
||||
* @param options.staged - 是否只检查暂存区文件
|
||||
* @param options.verbose - 是否显示详细信息
|
||||
* @param options.config - 自定义配置
|
||||
* @returns Promise<void>
|
||||
*/
|
||||
async function checkCircular({
|
||||
config = {},
|
||||
staged,
|
||||
verbose,
|
||||
}: CommandOptions): Promise<void> {
|
||||
try {
|
||||
// 合并配置
|
||||
const finalConfig = {
|
||||
...DEFAULT_CONFIG,
|
||||
...config,
|
||||
};
|
||||
|
||||
const allowedExtensions = new Set([
|
||||
'.cjs',
|
||||
'.js',
|
||||
'.jsx',
|
||||
'.mjs',
|
||||
'.ts',
|
||||
'.tsx',
|
||||
'.vue',
|
||||
]);
|
||||
// 生成忽略模式
|
||||
const ignorePattern = `**/{${finalConfig.ignoreDirs.join(',')}}/**`;
|
||||
|
||||
// 过滤文件列表
|
||||
files = files.filter((file) => allowedExtensions.has(extname(file)));
|
||||
// 检查缓存
|
||||
const cacheKey = `${staged}-${process.cwd()}-${ignorePattern}`;
|
||||
if (cache.has(cacheKey)) {
|
||||
const cachedResults = cache.get(cacheKey);
|
||||
if (cachedResults) {
|
||||
verbose && formatCircles(cachedResults);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const circularFiles: string[][] = [];
|
||||
// 检测循环依赖
|
||||
const results = await circularDepsDetect({
|
||||
absolute: staged,
|
||||
cwd: process.cwd(),
|
||||
ignore: [ignorePattern],
|
||||
});
|
||||
|
||||
for (const file of files) {
|
||||
for (const result of results) {
|
||||
const resultFiles = result.flat();
|
||||
if (resultFiles.includes(file)) {
|
||||
circularFiles.push(result);
|
||||
if (staged) {
|
||||
let files = await getStagedFiles();
|
||||
const allowedExtensions = new Set(finalConfig.allowedExtensions);
|
||||
|
||||
// 过滤文件列表
|
||||
files = files.filter((file) => allowedExtensions.has(extname(file)));
|
||||
|
||||
const circularFiles: CircularDependencyResult[] = [];
|
||||
|
||||
for (const file of files) {
|
||||
for (const result of results) {
|
||||
const resultFiles = result.flat();
|
||||
if (resultFiles.includes(file)) {
|
||||
circularFiles.push(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 更新缓存
|
||||
cache.set(cacheKey, circularFiles);
|
||||
verbose && formatCircles(circularFiles);
|
||||
} else {
|
||||
// 更新缓存
|
||||
cache.set(cacheKey, results);
|
||||
verbose && formatCircles(results);
|
||||
}
|
||||
verbose && printCircles(circularFiles);
|
||||
} else {
|
||||
verbose && printCircles(results);
|
||||
|
||||
// 如果发现循环依赖,只输出警告信息
|
||||
if (results.length > 0) {
|
||||
console.log(
|
||||
'\n⚠️ Warning: Circular dependencies found, please check and fix',
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(
|
||||
'❌ Error checking circular dependencies:',
|
||||
error instanceof Error ? error.message : error,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function defineCheckCircularCommand(cac: CAC) {
|
||||
/**
|
||||
* 定义检查循环依赖的命令
|
||||
* @param cac - CAC实例
|
||||
*/
|
||||
function defineCheckCircularCommand(cac: CAC): void {
|
||||
cac
|
||||
.command('check-circular')
|
||||
.option(
|
||||
'--staged',
|
||||
'Whether it is the staged commit mode, in which mode, if there is a circular dependency, an alarm will be given.',
|
||||
)
|
||||
.usage(`Analysis of project circular dependencies.`)
|
||||
.action(async ({ staged }) => {
|
||||
await checkCircular({ staged, verbose: true });
|
||||
.option('--staged', 'Only check staged files')
|
||||
.option('--verbose', 'Show detailed information')
|
||||
.option('--threshold <number>', 'Threshold for circular dependencies', {
|
||||
default: 0,
|
||||
})
|
||||
.option('--ignore-dirs <dirs>', 'Directories to ignore, comma separated')
|
||||
.usage('Analyze project circular dependencies')
|
||||
.action(async ({ ignoreDirs, staged, threshold, verbose }) => {
|
||||
const config: CheckCircularConfig = {
|
||||
threshold: Number(threshold),
|
||||
...(ignoreDirs && { ignoreDirs: ignoreDirs.split(',') }),
|
||||
};
|
||||
|
||||
await checkCircular({
|
||||
config,
|
||||
staged,
|
||||
verbose: verbose ?? true,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export { defineCheckCircularCommand };
|
||||
export { type CheckCircularConfig, defineCheckCircularCommand };
|
||||
|
Reference in New Issue
Block a user