fix: window system clean script execution problems (#4513)

* fix: fix window system clean script execution problems

* fix: lint error

* chore: remove test code
This commit is contained in:
vince
2024-09-26 11:59:19 +08:00
committed by GitHub
parent a46c85d77d
commit a72b8acaf9
13 changed files with 333 additions and 401 deletions

53
scripts/clean.mjs Normal file
View File

@@ -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}`);
}
})();

View File

@@ -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 };

View File

@@ -4,7 +4,6 @@ import { cac } from 'cac';
import { defineCheckCircularCommand } from './check-circular';
import { defineDepcheckCommand } from './check-dep';
import { defineCleanCommand } from './clean';
import { defineCodeWorkspaceCommand } from './code-workspace';
import { defineLintCommand } from './lint';
import { definePubLintCommand } from './publint';
@@ -18,9 +17,6 @@ try {
// vsh publint
definePubLintCommand(vsh);
// vsh clean
defineCleanCommand(vsh);
// vsh code-workspace
defineCodeWorkspaceCommand(vsh);