提交新版本
This commit is contained in:
141
scripts/clean.mjs
Normal file
141
scripts/clean.mjs
Normal file
@@ -0,0 +1,141 @@
|
||||
import { promises as fs } from 'node:fs';
|
||||
import { join, normalize } from 'node:path';
|
||||
|
||||
const rootDir = process.cwd();
|
||||
|
||||
// 控制并发数量,避免创建过多的并发任务
|
||||
const CONCURRENCY_LIMIT = 10;
|
||||
|
||||
// 需要跳过的目录,避免进入这些目录进行清理
|
||||
const SKIP_DIRS = new Set(['.DS_Store', '.git', '.idea', '.vscode']);
|
||||
|
||||
/**
|
||||
* 处理单个文件/目录项
|
||||
* @param {string} currentDir - 当前目录路径
|
||||
* @param {string} item - 文件/目录名
|
||||
* @param {string[]} targets - 要删除的目标列表
|
||||
* @param {number} _depth - 当前递归深度
|
||||
* @returns {Promise<boolean>} - 是否需要进一步递归处理
|
||||
*/
|
||||
async function processItem(currentDir, item, targets, _depth) {
|
||||
// 跳过特殊目录
|
||||
if (SKIP_DIRS.has(item)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
const itemPath = normalize(join(currentDir, item));
|
||||
|
||||
if (targets.includes(item)) {
|
||||
// 匹配到目标目录或文件时直接删除
|
||||
await fs.rm(itemPath, { force: true, recursive: true });
|
||||
console.log(`✅ Deleted: ${itemPath}`);
|
||||
return false; // 已删除,无需递归
|
||||
}
|
||||
|
||||
// 使用 readdir 的 withFileTypes 选项,避免额外的 lstat 调用
|
||||
return true; // 可能需要递归,由调用方决定
|
||||
} catch (error) {
|
||||
// 更详细的错误信息
|
||||
if (error.code === 'ENOENT') {
|
||||
// 文件不存在,可能已被删除,这是正常情况
|
||||
return false;
|
||||
} else if (error.code === 'EPERM' || error.code === 'EACCES') {
|
||||
console.error(`❌ Permission denied: ${item} in ${currentDir}`);
|
||||
} else {
|
||||
console.error(
|
||||
`❌ Error handling item ${item} in ${currentDir}: ${error.message}`,
|
||||
);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归查找并删除目标目录(并发优化版本)
|
||||
* @param {string} currentDir - 当前遍历的目录路径
|
||||
* @param {string[]} targets - 要删除的目标列表
|
||||
* @param {number} depth - 当前递归深度,避免过深递归
|
||||
*/
|
||||
async function cleanTargetsRecursively(currentDir, targets, depth = 0) {
|
||||
// 限制递归深度,避免无限递归
|
||||
if (depth > 10) {
|
||||
console.warn(`Max recursion depth reached at: ${currentDir}`);
|
||||
return;
|
||||
}
|
||||
|
||||
let dirents;
|
||||
try {
|
||||
// 使用 withFileTypes 选项,一次性获取文件类型信息,避免后续 lstat 调用
|
||||
dirents = await fs.readdir(currentDir, { withFileTypes: true });
|
||||
} catch (error) {
|
||||
// 如果无法读取目录,可能已被删除或权限不足
|
||||
console.warn(`Cannot read directory ${currentDir}: ${error.message}`);
|
||||
return;
|
||||
}
|
||||
|
||||
// 分批处理,控制并发数量
|
||||
for (let i = 0; i < dirents.length; i += CONCURRENCY_LIMIT) {
|
||||
const batch = dirents.slice(i, i + CONCURRENCY_LIMIT);
|
||||
|
||||
const tasks = batch.map(async (dirent) => {
|
||||
const item = dirent.name;
|
||||
const shouldRecurse = await processItem(currentDir, item, targets, depth);
|
||||
|
||||
// 如果是目录且没有被删除,则递归处理
|
||||
if (shouldRecurse && dirent.isDirectory()) {
|
||||
const itemPath = normalize(join(currentDir, item));
|
||||
return cleanTargetsRecursively(itemPath, targets, depth + 1);
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
// 并发执行当前批次的任务
|
||||
const results = await Promise.allSettled(tasks);
|
||||
|
||||
// 检查是否有失败的任务(可选:用于调试)
|
||||
const failedTasks = results.filter(
|
||||
(result) => result.status === 'rejected',
|
||||
);
|
||||
if (failedTasks.length > 0) {
|
||||
console.warn(
|
||||
`${failedTasks.length} tasks failed in batch starting at index ${i} in directory: ${currentDir}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
(async function startCleanup() {
|
||||
// 要删除的目录及文件名称
|
||||
const targets = ['node_modules', 'dist', '.turbo', 'dist.zip'];
|
||||
const deleteLockFile = process.argv.includes('--del-lock');
|
||||
const cleanupTargets = [...targets];
|
||||
|
||||
if (deleteLockFile) {
|
||||
cleanupTargets.push('pnpm-lock.yaml');
|
||||
}
|
||||
|
||||
console.log(
|
||||
`🚀 Starting cleanup of targets: ${cleanupTargets.join(', ')} from root: ${rootDir}`,
|
||||
);
|
||||
|
||||
const startTime = Date.now();
|
||||
|
||||
try {
|
||||
// 先统计要删除的目标数量
|
||||
console.log('📊 Scanning for cleanup targets...');
|
||||
|
||||
await cleanTargetsRecursively(rootDir, cleanupTargets);
|
||||
|
||||
const endTime = Date.now();
|
||||
const duration = (endTime - startTime) / 1000;
|
||||
|
||||
console.log(
|
||||
`✨ Cleanup process completed successfully in ${duration.toFixed(2)}s`,
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(`💥 Unexpected error during cleanup: ${error.message}`);
|
||||
process.exit(1);
|
||||
}
|
||||
})();
|
||||
29
scripts/turbo-run/package.json
Normal file
29
scripts/turbo-run/package.json
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "@vben/turbo-run",
|
||||
"version": "5.5.9",
|
||||
"private": true,
|
||||
"license": "MIT",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"stub": "pnpm unbuild --stub"
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"bin": {
|
||||
"turbo-run": "./bin/turbo-run.mjs"
|
||||
},
|
||||
"main": "./dist/index.mjs",
|
||||
"module": "./dist/index.mjs",
|
||||
"exports": {
|
||||
".": {
|
||||
"default": "./dist/index.mjs"
|
||||
},
|
||||
"./package.json": "./package.json"
|
||||
},
|
||||
"dependencies": {
|
||||
"@clack/prompts": "catalog:",
|
||||
"@vben/node-utils": "workspace:*",
|
||||
"cac": "catalog:"
|
||||
}
|
||||
}
|
||||
31
scripts/vsh/package.json
Normal file
31
scripts/vsh/package.json
Normal file
@@ -0,0 +1,31 @@
|
||||
{
|
||||
"name": "@vben/vsh",
|
||||
"version": "5.5.9",
|
||||
"private": true,
|
||||
"license": "MIT",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"stub": "pnpm unbuild --stub"
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"bin": {
|
||||
"vsh": "./bin/vsh.mjs"
|
||||
},
|
||||
"main": "./dist/index.mjs",
|
||||
"module": "./dist/index.mjs",
|
||||
"exports": {
|
||||
".": {
|
||||
"default": "./dist/index.mjs"
|
||||
},
|
||||
"./package.json": "./package.json"
|
||||
},
|
||||
"dependencies": {
|
||||
"@vben/node-utils": "workspace:*",
|
||||
"cac": "catalog:",
|
||||
"circular-dependency-scanner": "catalog:",
|
||||
"depcheck": "catalog:",
|
||||
"publint": "catalog:"
|
||||
}
|
||||
}
|
||||
193
scripts/vsh/src/check-dep/index.ts
Normal file
193
scripts/vsh/src/check-dep/index.ts
Normal file
@@ -0,0 +1,193 @@
|
||||
import type { CAC } from 'cac';
|
||||
|
||||
import { getPackages } from '@vben/node-utils';
|
||||
|
||||
import depcheck from 'depcheck';
|
||||
|
||||
// 默认配置
|
||||
const DEFAULT_CONFIG = {
|
||||
// 需要忽略的依赖匹配
|
||||
ignoreMatches: [
|
||||
'vite',
|
||||
'vitest',
|
||||
'unbuild',
|
||||
'@vben/tsconfig',
|
||||
'@vben/vite-config',
|
||||
'@vben/tailwind-config',
|
||||
'@types/*',
|
||||
'@vben-core/design',
|
||||
],
|
||||
// 需要忽略的包
|
||||
ignorePackages: [
|
||||
'@vben/commitlint-config',
|
||||
'@vben/eslint-config',
|
||||
'@vben/node-utils',
|
||||
'@vben/prettier-config',
|
||||
'@vben/stylelint-config',
|
||||
'@vben/tailwind-config',
|
||||
'@vben/tsconfig',
|
||||
'@vben/vite-config',
|
||||
'@vben/vsh',
|
||||
],
|
||||
// 需要忽略的文件模式
|
||||
ignorePatterns: ['dist', 'node_modules', 'public'],
|
||||
};
|
||||
|
||||
interface DepcheckResult {
|
||||
dependencies: string[];
|
||||
devDependencies: string[];
|
||||
missing: Record<string, string[]>;
|
||||
}
|
||||
|
||||
interface DepcheckConfig {
|
||||
ignoreMatches?: string[];
|
||||
ignorePackages?: string[];
|
||||
ignorePatterns?: string[];
|
||||
}
|
||||
|
||||
interface PackageInfo {
|
||||
dir: string;
|
||||
packageJson: {
|
||||
name: string;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理依赖检查结果
|
||||
* @param unused - 依赖检查结果
|
||||
*/
|
||||
function cleanDepcheckResult(unused: DepcheckResult): void {
|
||||
// 删除file:前缀的依赖提示,该依赖是本地依赖
|
||||
Reflect.deleteProperty(unused.missing, 'file:');
|
||||
|
||||
// 清理路径依赖
|
||||
Object.keys(unused.missing).forEach((key) => {
|
||||
unused.missing[key] = (unused.missing[key] || []).filter(
|
||||
(item: string) => !item.startsWith('/'),
|
||||
);
|
||||
if (unused.missing[key].length === 0) {
|
||||
Reflect.deleteProperty(unused.missing, key);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化依赖检查结果
|
||||
* @param pkgName - 包名
|
||||
* @param unused - 依赖检查结果
|
||||
*/
|
||||
function formatDepcheckResult(pkgName: string, unused: DepcheckResult): void {
|
||||
const hasIssues =
|
||||
Object.keys(unused.missing).length > 0 ||
|
||||
unused.dependencies.length > 0 ||
|
||||
unused.devDependencies.length > 0;
|
||||
|
||||
if (!hasIssues) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('\n📦 Package:', pkgName);
|
||||
|
||||
if (Object.keys(unused.missing).length > 0) {
|
||||
console.log('❌ Missing dependencies:');
|
||||
Object.entries(unused.missing).forEach(([dep, files]) => {
|
||||
console.log(` - ${dep}:`);
|
||||
files.forEach((file) => console.log(` → ${file}`));
|
||||
});
|
||||
}
|
||||
|
||||
if (unused.dependencies.length > 0) {
|
||||
console.log('⚠️ Unused dependencies:');
|
||||
unused.dependencies.forEach((dep) => console.log(` - ${dep}`));
|
||||
}
|
||||
|
||||
if (unused.devDependencies.length > 0) {
|
||||
console.log('⚠️ Unused devDependencies:');
|
||||
unused.devDependencies.forEach((dep) => console.log(` - ${dep}`));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 运行依赖检查
|
||||
* @param config - 配置选项
|
||||
*/
|
||||
async function runDepcheck(config: DepcheckConfig = {}): Promise<void> {
|
||||
try {
|
||||
const finalConfig = {
|
||||
...DEFAULT_CONFIG,
|
||||
...config,
|
||||
};
|
||||
|
||||
const { packages } = await getPackages();
|
||||
|
||||
let hasIssues = false;
|
||||
|
||||
await Promise.all(
|
||||
packages.map(async (pkg: PackageInfo) => {
|
||||
// 跳过需要忽略的包
|
||||
if (finalConfig.ignorePackages.includes(pkg.packageJson.name)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const unused = await depcheck(pkg.dir, {
|
||||
ignoreMatches: finalConfig.ignoreMatches,
|
||||
ignorePatterns: finalConfig.ignorePatterns,
|
||||
});
|
||||
|
||||
cleanDepcheckResult(unused);
|
||||
|
||||
const pkgHasIssues =
|
||||
Object.keys(unused.missing).length > 0 ||
|
||||
unused.dependencies.length > 0 ||
|
||||
unused.devDependencies.length > 0;
|
||||
|
||||
if (pkgHasIssues) {
|
||||
hasIssues = true;
|
||||
formatDepcheckResult(pkg.packageJson.name, unused);
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
if (!hasIssues) {
|
||||
console.log('\n✅ Dependency check completed, no issues found');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(
|
||||
'❌ Dependency check failed:',
|
||||
error instanceof Error ? error.message : error,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义依赖检查命令
|
||||
* @param cac - CAC实例
|
||||
*/
|
||||
function defineDepcheckCommand(cac: CAC): void {
|
||||
cac
|
||||
.command('check-dep')
|
||||
.option(
|
||||
'--ignore-packages <packages>',
|
||||
'Packages to ignore, comma separated',
|
||||
)
|
||||
.option(
|
||||
'--ignore-matches <matches>',
|
||||
'Dependency patterns to ignore, comma separated',
|
||||
)
|
||||
.option(
|
||||
'--ignore-patterns <patterns>',
|
||||
'File patterns to ignore, comma separated',
|
||||
)
|
||||
.usage('Analyze project dependencies')
|
||||
.action(async ({ ignoreMatches, ignorePackages, ignorePatterns }) => {
|
||||
const config: DepcheckConfig = {
|
||||
...(ignorePackages && { ignorePackages: ignorePackages.split(',') }),
|
||||
...(ignoreMatches && { ignoreMatches: ignoreMatches.split(',') }),
|
||||
...(ignorePatterns && { ignorePatterns: ignorePatterns.split(',') }),
|
||||
};
|
||||
|
||||
await runDepcheck(config);
|
||||
});
|
||||
}
|
||||
|
||||
export { defineDepcheckCommand, type DepcheckConfig };
|
||||
Reference in New Issue
Block a user