Browse Source
* fix: fix window system clean script execution problems * fix: lint error * chore: remove test codepull/4519/head
committed by
GitHub
13 changed files with 336 additions and 404 deletions
File diff suppressed because it is too large
@ -0,0 +1,53 @@ |
|||||
|
import { promises as fs } from 'node:fs'; |
||||
|
import { join } from 'node:path'; |
||||
|
|
||||
|
const rootDir = process.cwd(); |
||||
|
|
||||
|
/** |
||||
|
* 递归查找并删除目标目录 |
||||
|
* @param {string} currentDir - 当前遍历的目录路径 |
||||
|
*/ |
||||
|
async function cleanTargetsRecursively(currentDir, targets) { |
||||
|
const items = await fs.readdir(currentDir); |
||||
|
|
||||
|
for (const item of items) { |
||||
|
try { |
||||
|
const itemPath = join(currentDir, item); |
||||
|
if (targets.includes(item)) { |
||||
|
// 匹配到目标目录或文件时直接删除
|
||||
|
await fs.rm(itemPath, { force: true, recursive: true }); |
||||
|
console.log(`Deleted: ${itemPath}`); |
||||
|
} |
||||
|
const stat = await fs.lstat(itemPath); |
||||
|
if (stat.isDirectory()) { |
||||
|
await cleanTargetsRecursively(itemPath, targets); |
||||
|
} |
||||
|
} catch { |
||||
|
// console.error(
|
||||
|
// `Error handling item ${item} in ${currentDir}: ${error.message}`,
|
||||
|
// );
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
(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}`, |
||||
|
); |
||||
|
|
||||
|
try { |
||||
|
await cleanTargetsRecursively(rootDir, cleanupTargets); |
||||
|
console.log('Cleanup process completed.'); |
||||
|
} catch (error) { |
||||
|
console.error(`Unexpected error during cleanup: ${error.message}`); |
||||
|
} |
||||
|
})(); |
||||
@ -1,88 +0,0 @@ |
|||||
import type { CAC } from 'cac'; |
|
||||
|
|
||||
import { join } from 'node:path'; |
|
||||
|
|
||||
import { colors, getPackages, rimraf, spinner } from '@vben/node-utils'; |
|
||||
|
|
||||
const CLEAN_DIRS = ['dist', 'node_modules', '.turbo']; |
|
||||
|
|
||||
interface CleanCommandOptions { |
|
||||
/** |
|
||||
* Whether to delete the project pnpm-lock.yaml file. |
|
||||
* @default true |
|
||||
*/ |
|
||||
delLock?: boolean; |
|
||||
/** |
|
||||
* Files that need to be cleared. |
|
||||
*/ |
|
||||
dirs?: string[]; |
|
||||
/** |
|
||||
* recursive clear. |
|
||||
* @default true |
|
||||
*/ |
|
||||
recursive?: boolean; |
|
||||
} |
|
||||
|
|
||||
async function runClean({ |
|
||||
delLock = false, |
|
||||
dirs = [], |
|
||||
recursive, |
|
||||
}: CleanCommandOptions) { |
|
||||
const cleanDirs = dirs.length === 0 ? CLEAN_DIRS : dirs; |
|
||||
|
|
||||
const cleanDirsText = JSON.stringify(cleanDirs); |
|
||||
|
|
||||
spinner( |
|
||||
{ |
|
||||
successText: colors.green(`clean up all \`${cleanDirsText}\` success.`), |
|
||||
title: `${colors.dim(cleanDirsText)} cleaning in progress...`, |
|
||||
}, |
|
||||
async () => { |
|
||||
await clean({ delLock, dirs: cleanDirs, recursive }); |
|
||||
}, |
|
||||
); |
|
||||
} |
|
||||
|
|
||||
async function clean({ delLock, dirs = [], recursive }: CleanCommandOptions) { |
|
||||
const { packages, rootDir } = await getPackages(); |
|
||||
|
|
||||
// Delete the project pnpm-lock.yaml file
|
|
||||
if (delLock) { |
|
||||
await rimraf(join(rootDir, 'pnpm-lock.yaml')); |
|
||||
} |
|
||||
|
|
||||
// Recursively delete the specified folders under all package directories
|
|
||||
if (recursive) { |
|
||||
await Promise.all( |
|
||||
packages.map((pkg) => { |
|
||||
const pkgRoot = dirs.map((dir) => join(pkg.dir, dir)); |
|
||||
return rimraf(pkgRoot, { preserveRoot: true }); |
|
||||
}), |
|
||||
); |
|
||||
} |
|
||||
|
|
||||
// Only delete the specified folders in the root directory
|
|
||||
await Promise.all( |
|
||||
dirs.map((dir) => rimraf(join(process.cwd(), dir), { preserveRoot: true })), |
|
||||
); |
|
||||
} |
|
||||
|
|
||||
function defineCleanCommand(cac: CAC) { |
|
||||
cac |
|
||||
.command('clean [dirs...]') |
|
||||
.usage( |
|
||||
`Delete all ['dist', 'node_modules', '.turbo'] directories under the project.`, |
|
||||
) |
|
||||
.option('-r,--recursive', 'Recursively clean all packages in a monorepo.', { |
|
||||
default: true, |
|
||||
}) |
|
||||
.option('--del-lock', 'Delete the project pnpm-lock.yaml file.', { |
|
||||
default: true, |
|
||||
}) |
|
||||
.action( |
|
||||
async (dirs, { delLock, recursive }) => |
|
||||
await runClean({ delLock, dirs, recursive }), |
|
||||
); |
|
||||
} |
|
||||
|
|
||||
export { defineCleanCommand }; |
|
||||
Loading…
Reference in new issue