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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 333 additions and 401 deletions

View File

@ -75,7 +75,7 @@ The execution command is: `pnpm run [script]` or `npm run [script]`.
// Check types
"check:type": "turbo run typecheck",
// Clean the project (delete node_modules, dist, .turbo, etc.)
"clean": "vsh clean",
"clean": "node ./scripts/clean.mjs",
// Commit code
"commit": "czg",
// Start the project (by default, the dev scripts of all packages in the entire repository will run)

View File

@ -42,23 +42,6 @@ Check the dependency situation of the entire project and output `unused dependen
pnpm vsh check-dep
```
### vsh clean
Delete the project's `node_modules`, `dist`, `.turbo` directories, etc., to clean the project.
#### Usage
```bash
pnpm vsh clean
```
#### Options
| Option | Description |
| --- | --- |
| `-r,--recursive` | Recursively delete the entire project, default `true` |
| `--del-lock` | Whether to delete the `pnpm-lock.yaml` file, default `true` |
### vsh lint
Lint checks the project to see if the code in the project conforms to standards.

View File

@ -75,7 +75,7 @@ npm 脚本是项目常见的配置,用于执行一些常见的任务,比如
// 检查类型
"check:type": "turbo run typecheck",
// 清理项目删除node_modules、dist、.turbo等目录
"clean": "vsh clean",
"clean": "node ./scripts/clean.mjs",
// 提交代码
"commit": "czg",
// 启动项目默认会运行整个仓库所有包的dev脚本

View File

@ -42,16 +42,6 @@ pnpm vsh check-circular
pnpm vsh check-dep
```
### vsh clean
删除项目的`node_modules`、`dist`、`.turbo`等目录,清理项目。
#### 用法
```bash
pnpm vsh clean
```
#### 选项
| 选项 | 说明 |

View File

@ -143,7 +143,7 @@ const customConfig: Linter.Config[] = [
},
},
{
files: ['internal/**/**'],
files: ['internal/**/**', 'scripts/**/**'],
rules: {
'no-console': 'off',
},

View File

@ -39,7 +39,7 @@
"check:cspell": "cspell lint **/*.ts **/README.md .changeset/*.md --no-progress",
"check:dep": "vsh check-dep",
"check:type": "turbo run typecheck",
"clean": "vsh clean",
"clean": "node ./scripts/clean.mjs",
"commit": "czg",
"dev": "turbo-run dev",
"dev:antd": "pnpm -F @vben/web-antd run dev",

View File

@ -30,30 +30,30 @@ describe('preferences', () => {
expect(preferences).toEqual(defaultPreferences);
});
// it('initializes preferences with overrides', async () => {
// const overrides: any = {
// app: {
// locale: 'en-US',
// },
// };
// await preferenceManager.initPreferences({
// namespace: 'testNamespace',
// overrides,
// });
it('initializes preferences with overrides', async () => {
const overrides: any = {
app: {
locale: 'en-US',
},
};
await preferenceManager.initPreferences({
namespace: 'testNamespace',
overrides,
});
// // 等待防抖动操作完成
// // await new Promise((resolve) => setTimeout(resolve, 300)); // 等待100毫秒
// 等待防抖动操作完成
// await new Promise((resolve) => setTimeout(resolve, 300)); // 等待100毫秒
// const expected = {
// ...defaultPreferences,
// app: {
// ...defaultPreferences.app,
// ...overrides.app,
// },
// };
const expected = {
...defaultPreferences,
app: {
...defaultPreferences.app,
...overrides.app,
},
};
// expect(preferenceManager.getPreferences()).toEqual(expected);
// });
expect(preferenceManager.getPreferences()).toEqual(expected);
});
it('updates theme mode correctly', () => {
preferenceManager.updatePreferences({

View File

@ -172,7 +172,7 @@ class PreferenceManager {
const mergedPreference = merge(
{},
// overrides,
this.loadCachedPreferences() || defaultPreferences,
this.loadCachedPreferences() || {},
this.initialPreferences,
);

498
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@ -42,7 +42,7 @@ catalog:
'@types/html-minifier-terser': ^7.0.2
'@types/jsonwebtoken': ^9.0.7
'@types/lodash.clonedeep': ^4.5.9
'@types/node': ^22.7.0
'@types/node': ^22.7.2
'@types/nprogress': ^0.2.3
'@types/postcss-import': ^14.0.3
'@types/qrcode': ^1.5.5
@ -87,7 +87,7 @@ catalog:
eslint-plugin-command: ^0.2.5
eslint-plugin-eslint-comments: ^3.2.0
eslint-plugin-import-x: ^4.3.0
eslint-plugin-jsdoc: ^50.2.4
eslint-plugin-jsdoc: ^50.2.5
eslint-plugin-jsonc: ^2.16.0
eslint-plugin-n: ^17.10.3
eslint-plugin-no-only-tests: ^3.3.0
@ -111,9 +111,9 @@ catalog:
jsonwebtoken: ^9.0.2
lint-staged: ^15.2.10
lodash.clonedeep: ^4.5.0
lucide-vue-next: ^0.445.0
lucide-vue-next: ^0.446.0
medium-zoom: ^1.1.0
naive-ui: ^2.39.0
naive-ui: ^2.40.0
nanoid: ^5.0.7
nitropack: ^2.9.7
nprogress: ^0.2.0
@ -157,7 +157,7 @@ catalog:
unbuild: ^2.0.0
unplugin-element-plus: ^0.8.0
vee-validate: ^4.13.2
vite: ^5.4.7
vite: ^5.4.8
vite-plugin-compression: ^0.5.1
vite-plugin-dts: 4.2.1
vite-plugin-html: ^3.2.2
@ -172,6 +172,6 @@ catalog:
vue-i18n: ^10.0.3
vue-router: ^4.4.5
vue-tsc: ^2.1.6
watermark-js-plus: ^1.5.6
watermark-js-plus: ^1.5.7
zod: ^3.23.8
zod-defaults: ^0.1.3

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);