From 153748c3c418e75f2af0f7c53f94e53d10c340a9 Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Mon, 13 Mar 2023 11:02:34 +0800 Subject: [PATCH 01/32] feat: init --- packages/plugin-cache/.fatherrc.ts | 5 +++++ packages/plugin-cache/README.md | 3 +++ packages/plugin-cache/package.json | 31 +++++++++++++++++++++++++++++ packages/plugin-cache/src/index.ts | 17 ++++++++++++++++ packages/plugin-cache/tsconfig.json | 8 ++++++++ 5 files changed, 64 insertions(+) create mode 100644 packages/plugin-cache/.fatherrc.ts create mode 100644 packages/plugin-cache/README.md create mode 100644 packages/plugin-cache/package.json create mode 100644 packages/plugin-cache/src/index.ts create mode 100644 packages/plugin-cache/tsconfig.json diff --git a/packages/plugin-cache/.fatherrc.ts b/packages/plugin-cache/.fatherrc.ts new file mode 100644 index 000000000000..3305dd5a74a7 --- /dev/null +++ b/packages/plugin-cache/.fatherrc.ts @@ -0,0 +1,5 @@ +import { defineConfig } from 'father'; + +export default defineConfig({ + extends: '../../.fatherrc.base.ts', +}); diff --git a/packages/plugin-cache/README.md b/packages/plugin-cache/README.md new file mode 100644 index 000000000000..021bb69b919b --- /dev/null +++ b/packages/plugin-cache/README.md @@ -0,0 +1,3 @@ +# @umijs/plugin-cache + +See our website [umijs](https://umijs.org) for more information. diff --git a/packages/plugin-cache/package.json b/packages/plugin-cache/package.json new file mode 100644 index 000000000000..68934a80c057 --- /dev/null +++ b/packages/plugin-cache/package.json @@ -0,0 +1,31 @@ +{ + "name": "@umijs/plugin-cache", + "version": "4.0.0-canary.20230309.4", + "description": "@umijs/plugin-cache", + "homepage": "https://github.com/umijs/umi/tree/master/packages/plugin-cache#readme", + "bugs": "https://github.com/umijs/umi/issues", + "repository": { + "type": "git", + "url": "https://github.com/umijs/umi" + }, + "license": "MIT", + "main": "dist/index.js", + "types": "dist/index.d.ts", + "files": [ + "dist" + ], + "scripts": { + "build": "umi-scripts father build", + "build:deps": "umi-scripts bundleDeps", + "dev": "umi-scripts father dev" + }, + "dependencies": { + "tsx": "^3.12.2" + }, + "publishConfig": { + "access": "public" + }, + "authors": [ + "chencheng (https://github.com/sorrycc)" + ] +} diff --git a/packages/plugin-cache/src/index.ts b/packages/plugin-cache/src/index.ts new file mode 100644 index 000000000000..26b46f4ae569 --- /dev/null +++ b/packages/plugin-cache/src/index.ts @@ -0,0 +1,17 @@ +import { IApi } from 'umi'; + +export default (api: IApi) => { + api.describe({ + key: 'cache', + config: {}, + }); + + api.registerCommand({ + name: 'cache', + description: 'run the script commands, manage umi cache', + configResolveMode: 'loose', + fn: ({ args }) => { + console.log(args); + }, + }); +}; diff --git a/packages/plugin-cache/tsconfig.json b/packages/plugin-cache/tsconfig.json new file mode 100644 index 000000000000..792172fb82f6 --- /dev/null +++ b/packages/plugin-cache/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "outDir": "./dist", + "rootDir": "./src" + }, + "include": ["src"] +} From 8135f931402083ee18467a3daf8a4e0a67963a39 Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Mon, 13 Mar 2023 11:13:44 +0800 Subject: [PATCH 02/32] chore: init --- packages/plugin-cache/README.md | 2 +- packages/plugin-cache/package.json | 3 --- packages/plugin-cache/src/index.test.ts | 5 +++++ 3 files changed, 6 insertions(+), 4 deletions(-) create mode 100644 packages/plugin-cache/src/index.test.ts diff --git a/packages/plugin-cache/README.md b/packages/plugin-cache/README.md index 021bb69b919b..dbbf78e77f4b 100644 --- a/packages/plugin-cache/README.md +++ b/packages/plugin-cache/README.md @@ -1,3 +1,3 @@ # @umijs/plugin-cache -See our website [umijs](https://umijs.org) for more information. +See our website [umijs](https://umijs.org) for more information. \ No newline at end of file diff --git a/packages/plugin-cache/package.json b/packages/plugin-cache/package.json index 68934a80c057..64e009184303 100644 --- a/packages/plugin-cache/package.json +++ b/packages/plugin-cache/package.json @@ -19,9 +19,6 @@ "build:deps": "umi-scripts bundleDeps", "dev": "umi-scripts father dev" }, - "dependencies": { - "tsx": "^3.12.2" - }, "publishConfig": { "access": "public" }, diff --git a/packages/plugin-cache/src/index.test.ts b/packages/plugin-cache/src/index.test.ts new file mode 100644 index 000000000000..2ca303b9a882 --- /dev/null +++ b/packages/plugin-cache/src/index.test.ts @@ -0,0 +1,5 @@ +import index from './index'; + +test('normal', () => { + expect(index()).toEqual('@umijs/plugin-cache'); +}); From a3a90aa9427c746ae8c4d1262f1a455039e00467 Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Mon, 13 Mar 2023 11:32:46 +0800 Subject: [PATCH 03/32] chore: init --- packages/plugin-cache/.fatherrc.ts | 5 ---- packages/plugin-cache/README.md | 3 -- packages/plugin-cache/package.json | 28 ------------------- packages/plugin-cache/src/index.test.ts | 5 ---- packages/plugin-cache/tsconfig.json | 8 ------ .../src/commands/cache.ts} | 0 packages/preset-umi/src/index.ts | 1 + 7 files changed, 1 insertion(+), 49 deletions(-) delete mode 100644 packages/plugin-cache/.fatherrc.ts delete mode 100644 packages/plugin-cache/README.md delete mode 100644 packages/plugin-cache/package.json delete mode 100644 packages/plugin-cache/src/index.test.ts delete mode 100644 packages/plugin-cache/tsconfig.json rename packages/{plugin-cache/src/index.ts => preset-umi/src/commands/cache.ts} (100%) diff --git a/packages/plugin-cache/.fatherrc.ts b/packages/plugin-cache/.fatherrc.ts deleted file mode 100644 index 3305dd5a74a7..000000000000 --- a/packages/plugin-cache/.fatherrc.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { defineConfig } from 'father'; - -export default defineConfig({ - extends: '../../.fatherrc.base.ts', -}); diff --git a/packages/plugin-cache/README.md b/packages/plugin-cache/README.md deleted file mode 100644 index dbbf78e77f4b..000000000000 --- a/packages/plugin-cache/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# @umijs/plugin-cache - -See our website [umijs](https://umijs.org) for more information. \ No newline at end of file diff --git a/packages/plugin-cache/package.json b/packages/plugin-cache/package.json deleted file mode 100644 index 64e009184303..000000000000 --- a/packages/plugin-cache/package.json +++ /dev/null @@ -1,28 +0,0 @@ -{ - "name": "@umijs/plugin-cache", - "version": "4.0.0-canary.20230309.4", - "description": "@umijs/plugin-cache", - "homepage": "https://github.com/umijs/umi/tree/master/packages/plugin-cache#readme", - "bugs": "https://github.com/umijs/umi/issues", - "repository": { - "type": "git", - "url": "https://github.com/umijs/umi" - }, - "license": "MIT", - "main": "dist/index.js", - "types": "dist/index.d.ts", - "files": [ - "dist" - ], - "scripts": { - "build": "umi-scripts father build", - "build:deps": "umi-scripts bundleDeps", - "dev": "umi-scripts father dev" - }, - "publishConfig": { - "access": "public" - }, - "authors": [ - "chencheng (https://github.com/sorrycc)" - ] -} diff --git a/packages/plugin-cache/src/index.test.ts b/packages/plugin-cache/src/index.test.ts deleted file mode 100644 index 2ca303b9a882..000000000000 --- a/packages/plugin-cache/src/index.test.ts +++ /dev/null @@ -1,5 +0,0 @@ -import index from './index'; - -test('normal', () => { - expect(index()).toEqual('@umijs/plugin-cache'); -}); diff --git a/packages/plugin-cache/tsconfig.json b/packages/plugin-cache/tsconfig.json deleted file mode 100644 index 792172fb82f6..000000000000 --- a/packages/plugin-cache/tsconfig.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "extends": "../../tsconfig.base.json", - "compilerOptions": { - "outDir": "./dist", - "rootDir": "./src" - }, - "include": ["src"] -} diff --git a/packages/plugin-cache/src/index.ts b/packages/preset-umi/src/commands/cache.ts similarity index 100% rename from packages/plugin-cache/src/index.ts rename to packages/preset-umi/src/commands/cache.ts diff --git a/packages/preset-umi/src/index.ts b/packages/preset-umi/src/index.ts index f28e3a98b5c0..1eea8d98103c 100644 --- a/packages/preset-umi/src/index.ts +++ b/packages/preset-umi/src/index.ts @@ -79,6 +79,7 @@ export default () => { require.resolve('./commands/verify-commit'), require.resolve('./commands/preview'), require.resolve('./commands/mfsu/mfsu'), + require.resolve('./commands/cache'), require.resolve('@umijs/plugin-run'), ].filter(Boolean), }; From d01a19b6546fce4ecb1f9f3d75be598344c576d6 Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Mon, 13 Mar 2023 11:54:26 +0800 Subject: [PATCH 04/32] feat: clean --- packages/preset-umi/src/commands/cache.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/preset-umi/src/commands/cache.ts b/packages/preset-umi/src/commands/cache.ts index 26b46f4ae569..009c9b9149c2 100644 --- a/packages/preset-umi/src/commands/cache.ts +++ b/packages/preset-umi/src/commands/cache.ts @@ -1,4 +1,6 @@ +import { join } from 'path'; import { IApi } from 'umi'; +import { fsExtra } from 'umi/plugin-utils'; export default (api: IApi) => { api.describe({ @@ -11,7 +13,10 @@ export default (api: IApi) => { description: 'run the script commands, manage umi cache', configResolveMode: 'loose', fn: ({ args }) => { - console.log(args); + if (args[0] === 'clean') { + const absTmpFilePath = join(api.paths.absNodeModulesPath, '.cache'); + fsExtra.removeSync(absTmpFilePath); + } }, }); }; From 81469c1f3b7a47af48b213bf3bc178850ae2ffb2 Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Mon, 13 Mar 2023 14:01:13 +0800 Subject: [PATCH 05/32] feat: ls and examples --- examples/umi-cache/.umirc.ts | 5 ++ examples/umi-cache/package.json | 14 ++++ examples/umi-cache/src/pages/index.tsx | 7 ++ packages/preset-umi/src/commands/cache.ts | 80 ++++++++++++++++++++++- pnpm-lock.yaml | 9 +++ 5 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 examples/umi-cache/.umirc.ts create mode 100644 examples/umi-cache/package.json create mode 100644 examples/umi-cache/src/pages/index.tsx diff --git a/examples/umi-cache/.umirc.ts b/examples/umi-cache/.umirc.ts new file mode 100644 index 000000000000..ed60bf0afe16 --- /dev/null +++ b/examples/umi-cache/.umirc.ts @@ -0,0 +1,5 @@ +export default { + cache: { + plies: 2, + }, +}; diff --git a/examples/umi-cache/package.json b/examples/umi-cache/package.json new file mode 100644 index 000000000000..260eb9f19bdf --- /dev/null +++ b/examples/umi-cache/package.json @@ -0,0 +1,14 @@ +{ + "name": "@example/umi-cache", + "private": true, + "author": "DaoYuan ", + "scripts": { + "build": "umi build", + "clean": "umi cache clean", + "dev": "umi dev", + "ls": "umi cache ls" + }, + "dependencies": { + "umi": "workspace:*" + } +} diff --git a/examples/umi-cache/src/pages/index.tsx b/examples/umi-cache/src/pages/index.tsx new file mode 100644 index 000000000000..083c932f3bab --- /dev/null +++ b/examples/umi-cache/src/pages/index.tsx @@ -0,0 +1,7 @@ +export default function HomePage() { + return ( +
+

Yay! Welcome to umi!

+
+ ); +} diff --git a/packages/preset-umi/src/commands/cache.ts b/packages/preset-umi/src/commands/cache.ts index 009c9b9149c2..8b334f1ecd10 100644 --- a/packages/preset-umi/src/commands/cache.ts +++ b/packages/preset-umi/src/commands/cache.ts @@ -5,7 +5,13 @@ import { fsExtra } from 'umi/plugin-utils'; export default (api: IApi) => { api.describe({ key: 'cache', - config: {}, + config: { + schema(Joi) { + return Joi.object({ + plies: Joi.string(), + }); + }, + }, }); api.registerCommand({ @@ -13,10 +19,78 @@ export default (api: IApi) => { description: 'run the script commands, manage umi cache', configResolveMode: 'loose', fn: ({ args }) => { - if (args[0] === 'clean') { - const absTmpFilePath = join(api.paths.absNodeModulesPath, '.cache'); + const plies: number = args._[1] || api.config.cache?.plies || 2; + const absTmpFilePath = join(api.paths.absNodeModulesPath, '.cache'); + if (args._[0] === 'clean') { fsExtra.removeSync(absTmpFilePath); } + if (args._[0] === 'ls') { + const dirObj = getDirectorySize({ + directoryPath: absTmpFilePath, + number: plies, + }); + dirObj.dirTextArr.reverse().forEach((item) => { + console.log(item); + }); + } }, }); }; + +/** + * 获取目录信息 + * @param directoryPath 目录路径 + * @param number 展示目录层数 + * @param index 标记层数 + * @returns + */ +interface GetDirectorySize { + directoryPath: string; + name?: string; + number?: number; + index?: number; +} +function getDirectorySize({ + directoryPath, + number = 2, + index = 1, + name = 'node_modules/.cache', +}: GetDirectorySize) { + const obj = { + size: 0, + dirTextArr: [], + }; + const files = fsExtra.readdirSync(directoryPath); + let block = ''; + for (let indexBlock = 0; indexBlock < index - 1; indexBlock++) { + block += ' '; + } + files.forEach(function (file) { + const filePath = join(directoryPath, file); + const stats = fsExtra.statSync(filePath); + + if (stats.isFile()) { + const fileSize = Math.floor(stats.size / 1024); + obj.size += fileSize; + if (index < number) { + const str = `${block} └── [${fileSize}kb]${file}`; + obj.dirTextArr.push(str); + } + } else if (stats.isDirectory()) { + const objChild = getDirectorySize({ + directoryPath: filePath, + index: index + 1, + number, + name: file, + }); + if (index < number) { + obj.dirTextArr = [...obj.dirTextArr, ...objChild.dirTextArr]; + } + obj.size += objChild.size; + } + }); + const str = `${block}└── [${obj.size}kb]${name}`; + + obj.dirTextArr.push(str); + return obj; +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b78115f9d35c..5bc041d0a3c8 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -848,6 +848,12 @@ importers: '@umijs/plugin-umi-request-compat': 1.0.0_umi@packages+umi umi: link:../../packages/umi + examples/umi-cache: + specifiers: + umi: workspace:* + dependencies: + umi: link:../../packages/umi + examples/umi-run: specifiers: umi: workspace:* @@ -1566,6 +1572,9 @@ importers: webpack: 5.75.0 webpack-virtual-modules: 0.5.0 + packages/plugin-cache: + specifiers: {} + packages/plugin-docs: specifiers: '@mdx-js/mdx': 2.1.1 From f0a3669f86fc1a9ee719f4b494feb572811f00f3 Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Mon, 13 Mar 2023 14:10:14 +0800 Subject: [PATCH 06/32] chore: lock --- pnpm-lock.yaml | 3 --- 1 file changed, 3 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5bc041d0a3c8..457a2fbef3fe 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1572,9 +1572,6 @@ importers: webpack: 5.75.0 webpack-virtual-modules: 0.5.0 - packages/plugin-cache: - specifiers: {} - packages/plugin-docs: specifiers: '@mdx-js/mdx': 2.1.1 From 8cf04c31fb1870ed39250abb038ed658cf88af83 Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Mon, 13 Mar 2023 14:21:57 +0800 Subject: [PATCH 07/32] chore: details --- packages/preset-umi/src/commands/cache.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/preset-umi/src/commands/cache.ts b/packages/preset-umi/src/commands/cache.ts index 8b334f1ecd10..966daafde22c 100644 --- a/packages/preset-umi/src/commands/cache.ts +++ b/packages/preset-umi/src/commands/cache.ts @@ -17,6 +17,15 @@ export default (api: IApi) => { api.registerCommand({ name: 'cache', description: 'run the script commands, manage umi cache', + details: ` +umi cache + +# clear cache directory +umi cache clean + +# display directory information, number is the number of directory levels +umi cache ls [number] +`, configResolveMode: 'loose', fn: ({ args }) => { const plies: number = args._[1] || api.config.cache?.plies || 2; @@ -56,7 +65,7 @@ function getDirectorySize({ index = 1, name = 'node_modules/.cache', }: GetDirectorySize) { - const obj = { + const obj: { size: number; dirTextArr: string[] } = { size: 0, dirTextArr: [], }; From a392ff97691567aa439684b98e9b466979b23aec Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Mon, 13 Mar 2023 14:32:00 +0800 Subject: [PATCH 08/32] chore: the same parameter as npm --- examples/umi-cache/.umirc.ts | 6 +----- packages/preset-umi/src/commands/cache.ts | 15 ++------------- 2 files changed, 3 insertions(+), 18 deletions(-) diff --git a/examples/umi-cache/.umirc.ts b/examples/umi-cache/.umirc.ts index ed60bf0afe16..ff8b4c56321a 100644 --- a/examples/umi-cache/.umirc.ts +++ b/examples/umi-cache/.umirc.ts @@ -1,5 +1 @@ -export default { - cache: { - plies: 2, - }, -}; +export default {}; diff --git a/packages/preset-umi/src/commands/cache.ts b/packages/preset-umi/src/commands/cache.ts index 966daafde22c..e4a7aba8ca6f 100644 --- a/packages/preset-umi/src/commands/cache.ts +++ b/packages/preset-umi/src/commands/cache.ts @@ -3,17 +3,6 @@ import { IApi } from 'umi'; import { fsExtra } from 'umi/plugin-utils'; export default (api: IApi) => { - api.describe({ - key: 'cache', - config: { - schema(Joi) { - return Joi.object({ - plies: Joi.string(), - }); - }, - }, - }); - api.registerCommand({ name: 'cache', description: 'run the script commands, manage umi cache', @@ -28,7 +17,7 @@ umi cache ls [number] `, configResolveMode: 'loose', fn: ({ args }) => { - const plies: number = args._[1] || api.config.cache?.plies || 2; + const plies: number = args.depth || 0; const absTmpFilePath = join(api.paths.absNodeModulesPath, '.cache'); if (args._[0] === 'clean') { fsExtra.removeSync(absTmpFilePath); @@ -36,7 +25,7 @@ umi cache ls [number] if (args._[0] === 'ls') { const dirObj = getDirectorySize({ directoryPath: absTmpFilePath, - number: plies, + number: plies + 2, }); dirObj.dirTextArr.reverse().forEach((item) => { console.log(item); From bd6a7fbdfcc0b623edf2b30728079cd99bc98853 Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Mon, 13 Mar 2023 14:46:35 +0800 Subject: [PATCH 09/32] chore: depth --- packages/preset-umi/src/commands/cache.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/preset-umi/src/commands/cache.ts b/packages/preset-umi/src/commands/cache.ts index e4a7aba8ca6f..83eda712eb05 100644 --- a/packages/preset-umi/src/commands/cache.ts +++ b/packages/preset-umi/src/commands/cache.ts @@ -17,7 +17,7 @@ umi cache ls [number] `, configResolveMode: 'loose', fn: ({ args }) => { - const plies: number = args.depth || 0; + const plies: number = args.depth || 1; const absTmpFilePath = join(api.paths.absNodeModulesPath, '.cache'); if (args._[0] === 'clean') { fsExtra.removeSync(absTmpFilePath); @@ -25,7 +25,7 @@ umi cache ls [number] if (args._[0] === 'ls') { const dirObj = getDirectorySize({ directoryPath: absTmpFilePath, - number: plies + 2, + number: plies + 1, }); dirObj.dirTextArr.reverse().forEach((item) => { console.log(item); From 25c8c215c53c1d032444e87ffbf8eaec11f4359c Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Mon, 13 Mar 2023 14:48:28 +0800 Subject: [PATCH 10/32] chore: Compatible 0 --- packages/preset-umi/src/commands/cache.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/preset-umi/src/commands/cache.ts b/packages/preset-umi/src/commands/cache.ts index 83eda712eb05..7ce0dd4244b1 100644 --- a/packages/preset-umi/src/commands/cache.ts +++ b/packages/preset-umi/src/commands/cache.ts @@ -17,7 +17,7 @@ umi cache ls [number] `, configResolveMode: 'loose', fn: ({ args }) => { - const plies: number = args.depth || 1; + const plies: number = args.depth === undefined ? 1 : args.depth; const absTmpFilePath = join(api.paths.absNodeModulesPath, '.cache'); if (args._[0] === 'clean') { fsExtra.removeSync(absTmpFilePath); From c0f233eab9fe080f30fd67dc05cd0c9cf3973640 Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Mon, 13 Mar 2023 15:05:32 +0800 Subject: [PATCH 11/32] chore: style --- packages/preset-umi/src/commands/cache.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/preset-umi/src/commands/cache.ts b/packages/preset-umi/src/commands/cache.ts index 7ce0dd4244b1..489da2741eae 100644 --- a/packages/preset-umi/src/commands/cache.ts +++ b/packages/preset-umi/src/commands/cache.ts @@ -12,8 +12,8 @@ umi cache # clear cache directory umi cache clean -# display directory information, number is the number of directory levels -umi cache ls [number] +# display directory information, --depth is the number of directory levels +umi cache ls [--depth] `, configResolveMode: 'loose', fn: ({ args }) => { @@ -71,7 +71,7 @@ function getDirectorySize({ const fileSize = Math.floor(stats.size / 1024); obj.size += fileSize; if (index < number) { - const str = `${block} └── [${fileSize}kb]${file}`; + const str = `${block} ├── [${fileSize}kb] ${file}`; obj.dirTextArr.push(str); } } else if (stats.isDirectory()) { @@ -87,7 +87,7 @@ function getDirectorySize({ obj.size += objChild.size; } }); - const str = `${block}└── [${obj.size}kb]${name}`; + const str = `${block}└── [${obj.size}kb] ${name}`; obj.dirTextArr.push(str); return obj; From 734ca604668057d92f3607349620bcc3be4f1842 Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Mon, 13 Mar 2023 17:12:57 +0800 Subject: [PATCH 12/32] chore: tree --- packages/preset-umi/package.json | 3 +- packages/preset-umi/src/commands/cache.ts | 39 +++++++++++++---------- pnpm-lock.yaml | 9 +++++- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/packages/preset-umi/package.json b/packages/preset-umi/package.json index 2ed596a60301..8fcee3b1c29a 100644 --- a/packages/preset-umi/package.json +++ b/packages/preset-umi/package.json @@ -53,7 +53,8 @@ "react-dom": "18.1.0", "react-router": "6.3.0", "react-router-dom": "6.3.0", - "regenerator-runtime": "0.13.11" + "regenerator-runtime": "0.13.11", + "treeify": "^1.1.0" }, "devDependencies": { "@manypkg/get-packages": "1.1.3", diff --git a/packages/preset-umi/src/commands/cache.ts b/packages/preset-umi/src/commands/cache.ts index 489da2741eae..c360bbac8628 100644 --- a/packages/preset-umi/src/commands/cache.ts +++ b/packages/preset-umi/src/commands/cache.ts @@ -1,4 +1,5 @@ import { join } from 'path'; +import treeify from 'treeify'; import { IApi } from 'umi'; import { fsExtra } from 'umi/plugin-utils'; @@ -17,19 +18,17 @@ umi cache ls [--depth] `, configResolveMode: 'loose', fn: ({ args }) => { - const plies: number = args.depth === undefined ? 1 : args.depth; const absTmpFilePath = join(api.paths.absNodeModulesPath, '.cache'); if (args._[0] === 'clean') { fsExtra.removeSync(absTmpFilePath); } if (args._[0] === 'ls') { + const plies: number = args.depth === undefined ? 1 : args.depth; const dirObj = getDirectorySize({ directoryPath: absTmpFilePath, number: plies + 1, }); - dirObj.dirTextArr.reverse().forEach((item) => { - console.log(item); - }); + console.log(treeify.asTree(dirObj.tree)); } }, }); @@ -54,15 +53,17 @@ function getDirectorySize({ index = 1, name = 'node_modules/.cache', }: GetDirectorySize) { - const obj: { size: number; dirTextArr: string[] } = { + const obj: { size: number; tree: any; treeCopy: any } = { size: 0, - dirTextArr: [], + tree: {}, + treeCopy: {}, }; - const files = fsExtra.readdirSync(directoryPath); - let block = ''; - for (let indexBlock = 0; indexBlock < index - 1; indexBlock++) { - block += ' '; + const isCreateTree = index < number; + if (isCreateTree) { + obj.treeCopy[name] = {}; } + + const files = fsExtra.readdirSync(directoryPath); files.forEach(function (file) { const filePath = join(directoryPath, file); const stats = fsExtra.statSync(filePath); @@ -70,9 +71,8 @@ function getDirectorySize({ if (stats.isFile()) { const fileSize = Math.floor(stats.size / 1024); obj.size += fileSize; - if (index < number) { - const str = `${block} ├── [${fileSize}kb] ${file}`; - obj.dirTextArr.push(str); + if (name in obj.treeCopy) { + obj.treeCopy[name][`[${fileSize}kb] ${file}`] = null; } } else if (stats.isDirectory()) { const objChild = getDirectorySize({ @@ -81,14 +81,19 @@ function getDirectorySize({ number, name: file, }); - if (index < number) { - obj.dirTextArr = [...obj.dirTextArr, ...objChild.dirTextArr]; + if (name in obj.treeCopy) { + obj.treeCopy[name][`[${objChild.size}kb] ${file}`] = objChild.tree; } obj.size += objChild.size; } }); - const str = `${block}└── [${obj.size}kb] ${name}`; + if (index > 1) { + if (name in obj.treeCopy) { + obj.tree = obj.treeCopy[name]; + } + } else { + obj.tree[`[${obj.size} kb] ${name}`] = obj.treeCopy[name]; + } - obj.dirTextArr.push(str); return obj; } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 457a2fbef3fe..ee0a11c41f2b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1718,6 +1718,7 @@ importers: react-router-dom: 6.3.0 regenerator-runtime: 0.13.11 sirv: 2.0.2 + treeify: ^1.1.0 vite: 4.1.4 dependencies: '@iconify/utils': 2.1.1 @@ -1749,6 +1750,7 @@ importers: react-router: 6.3.0_react@18.1.0 react-router-dom: 6.3.0_ef5jwxihqo6n7gxfmzogljlgcm regenerator-runtime: 0.13.11 + treeify: 1.1.0 devDependencies: '@manypkg/get-packages': 1.1.3 '@types/body-parser': 1.19.2 @@ -25095,7 +25097,7 @@ packages: pretty-format: 29.4.3 slash: 3.0.0 strip-json-comments: 3.1.1 - ts-node: 10.9.1_typescript@4.9.4 + ts-node: 10.9.1_awa2wsr5thmg3i7jqycphctjfq transitivePeerDependencies: - supports-color dev: true @@ -39092,6 +39094,11 @@ packages: hasBin: true dev: true + /treeify/1.1.0: + resolution: {integrity: sha512-1m4RA7xVAJrSGrrXGs0L3YTwyvBs2S8PbRHaLZAkFw7JR8oIFwYtysxlBZhYIa7xSyiYJKZ3iGrrk55cGA3i9A==} + engines: {node: '>=0.6'} + dev: false + /treeverse/2.0.0: resolution: {integrity: sha512-N5gJCkLu1aXccpOTtqV6ddSEi6ZmGkh3hjmbu1IjcavJK4qyOVQmi0myQKM7z5jVGmD68SJoliaVrMmVObhj6A==} engines: {node: ^12.13.0 || ^14.15.0 || >=16.0.0} From 7f807b18fd25eba6c2653db884ac7affb26c6c8a Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Mon, 13 Mar 2023 17:49:11 +0800 Subject: [PATCH 13/32] fix: ci --- packages/preset-umi/package.json | 1 + pnpm-lock.yaml | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/packages/preset-umi/package.json b/packages/preset-umi/package.json index 8fcee3b1c29a..d3eefdf67930 100644 --- a/packages/preset-umi/package.json +++ b/packages/preset-umi/package.json @@ -27,6 +27,7 @@ "dependencies": { "@iconify/utils": "2.1.1", "@svgr/core": "6.5.1", + "@types/treeify": "^1.0.0", "@umijs/ast": "workspace:*", "@umijs/babel-preset-umi": "workspace:*", "@umijs/bundler-utils": "workspace:*", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ee0a11c41f2b..8896e0335693 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1685,6 +1685,7 @@ importers: '@types/ini': 1.3.31 '@types/madge': ^5.0.0 '@types/multer': 1.4.7 + '@types/treeify': ^1.0.0 '@umijs/ast': workspace:* '@umijs/babel-preset-umi': workspace:* '@umijs/bundler-utils': workspace:* @@ -1723,6 +1724,7 @@ importers: dependencies: '@iconify/utils': 2.1.1 '@svgr/core': 6.5.1 + '@types/treeify': 1.0.0 '@umijs/ast': link:../ast '@umijs/babel-preset-umi': link:../babel-preset-umi '@umijs/bundler-utils': link:../bundler-utils @@ -12851,6 +12853,10 @@ packages: resolution: {integrity: sha512-Q5vtl1W5ue16D+nIaW8JWebSSraJVlK+EthKn7e7UcD4KWsaSJ8BqGPXNaPghgtcn/fhvrN17Tv8ksUsQpiplw==} dev: false + /@types/treeify/1.0.0: + resolution: {integrity: sha512-ONpcZAEYlbPx4EtJwfTyCDQJGUpKf4sEcuySdCVjK5Fj/3vHp5HII1fqa1/+qrsLnpYELCQTfVW/awsGJePoIg==} + dev: false + /@types/triple-beam/1.3.2: resolution: {integrity: sha512-txGIh+0eDFzKGC25zORnswy+br1Ha7hj5cMVwKIU7+s0U2AxxJru/jZSMU6OC9MJWP6+pc/hc6ZjyZShpsyY2g==} dev: true From 96a67aa9a70ca524cc1a66dcbb907c1b37a63963 Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Mon, 13 Mar 2023 18:01:41 +0800 Subject: [PATCH 14/32] fix: ci --- packages/preset-umi/src/commands/cache.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/preset-umi/src/commands/cache.ts b/packages/preset-umi/src/commands/cache.ts index c360bbac8628..f86614358e79 100644 --- a/packages/preset-umi/src/commands/cache.ts +++ b/packages/preset-umi/src/commands/cache.ts @@ -28,7 +28,7 @@ umi cache ls [--depth] directoryPath: absTmpFilePath, number: plies + 1, }); - console.log(treeify.asTree(dirObj.tree)); + console.log(treeify.asTree(dirObj.tree, true, true)); } }, }); From 266c0b4ebe5443ab7a987654c1788007c59aa5b7 Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Mon, 13 Mar 2023 19:07:37 +0800 Subject: [PATCH 15/32] perf: tree --- packages/preset-umi/src/commands/cache.ts | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/packages/preset-umi/src/commands/cache.ts b/packages/preset-umi/src/commands/cache.ts index f86614358e79..b7d394b536d5 100644 --- a/packages/preset-umi/src/commands/cache.ts +++ b/packages/preset-umi/src/commands/cache.ts @@ -53,14 +53,13 @@ function getDirectorySize({ index = 1, name = 'node_modules/.cache', }: GetDirectorySize) { - const obj: { size: number; tree: any; treeCopy: any } = { + const obj: { size: number; tree: any } = { size: 0, - tree: {}, - treeCopy: {}, + tree: undefined, }; const isCreateTree = index < number; if (isCreateTree) { - obj.treeCopy[name] = {}; + obj.tree = {}; } const files = fsExtra.readdirSync(directoryPath); @@ -71,8 +70,8 @@ function getDirectorySize({ if (stats.isFile()) { const fileSize = Math.floor(stats.size / 1024); obj.size += fileSize; - if (name in obj.treeCopy) { - obj.treeCopy[name][`[${fileSize}kb] ${file}`] = null; + if (obj.tree) { + obj.tree[`[${fileSize}kb] ${file}`] = null; } } else if (stats.isDirectory()) { const objChild = getDirectorySize({ @@ -81,18 +80,14 @@ function getDirectorySize({ number, name: file, }); - if (name in obj.treeCopy) { - obj.treeCopy[name][`[${objChild.size}kb] ${file}`] = objChild.tree; + if (obj.tree) { + obj.tree[`[${objChild.size}kb] ${file}`] = objChild.tree; } obj.size += objChild.size; } }); - if (index > 1) { - if (name in obj.treeCopy) { - obj.tree = obj.treeCopy[name]; - } - } else { - obj.tree[`[${obj.size} kb] ${name}`] = obj.treeCopy[name]; + if (index === 1) { + obj.tree[`[${obj.size} kb] ${name}`] = obj.tree; } return obj; From ad05fe5d6d3d0cd7833428074590d883c60c4d98 Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Mon, 13 Mar 2023 19:15:29 +0800 Subject: [PATCH 16/32] fix: first of tree --- packages/preset-umi/src/commands/cache.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/preset-umi/src/commands/cache.ts b/packages/preset-umi/src/commands/cache.ts index b7d394b536d5..9431d9a0674e 100644 --- a/packages/preset-umi/src/commands/cache.ts +++ b/packages/preset-umi/src/commands/cache.ts @@ -28,7 +28,10 @@ umi cache ls [--depth] directoryPath: absTmpFilePath, number: plies + 1, }); - console.log(treeify.asTree(dirObj.tree, true, true)); + const tree: any = {}; + const str = `[${dirObj.size}kb] node_modules/.cache`; + tree[str] = dirObj.tree; + console.log(treeify.asTree(tree, true, true)); } }, }); @@ -51,7 +54,6 @@ function getDirectorySize({ directoryPath, number = 2, index = 1, - name = 'node_modules/.cache', }: GetDirectorySize) { const obj: { size: number; tree: any } = { size: 0, @@ -86,9 +88,6 @@ function getDirectorySize({ obj.size += objChild.size; } }); - if (index === 1) { - obj.tree[`[${obj.size} kb] ${name}`] = obj.tree; - } return obj; } From 6fb7ca1380a7b5cd9e26e9cb1b289a141eaae45e Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Mon, 13 Mar 2023 19:17:35 +0800 Subject: [PATCH 17/32] fix: undefined show --- packages/preset-umi/src/commands/cache.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/preset-umi/src/commands/cache.ts b/packages/preset-umi/src/commands/cache.ts index 9431d9a0674e..c55ed66f1fff 100644 --- a/packages/preset-umi/src/commands/cache.ts +++ b/packages/preset-umi/src/commands/cache.ts @@ -57,7 +57,7 @@ function getDirectorySize({ }: GetDirectorySize) { const obj: { size: number; tree: any } = { size: 0, - tree: undefined, + tree: null, }; const isCreateTree = index < number; if (isCreateTree) { From ac427333ce4a781d195e5842ad966a70d2eb93e8 Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Mon, 13 Mar 2023 19:45:34 +0800 Subject: [PATCH 18/32] Update packages/preset-umi/src/commands/cache.ts Co-authored-by: Peach --- packages/preset-umi/src/commands/cache.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/preset-umi/src/commands/cache.ts b/packages/preset-umi/src/commands/cache.ts index c55ed66f1fff..3616687620bb 100644 --- a/packages/preset-umi/src/commands/cache.ts +++ b/packages/preset-umi/src/commands/cache.ts @@ -14,7 +14,7 @@ umi cache umi cache clean # display directory information, --depth is the number of directory levels -umi cache ls [--depth] +umi cache ls [--depth ] `, configResolveMode: 'loose', fn: ({ args }) => { From 1babcf2b65cbf36a4abe68972c244e5ad96733cf Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Mon, 13 Mar 2023 19:46:52 +0800 Subject: [PATCH 19/32] Update packages/preset-umi/src/commands/cache.ts Co-authored-by: Peach --- packages/preset-umi/src/commands/cache.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/preset-umi/src/commands/cache.ts b/packages/preset-umi/src/commands/cache.ts index 3616687620bb..c12e2be11361 100644 --- a/packages/preset-umi/src/commands/cache.ts +++ b/packages/preset-umi/src/commands/cache.ts @@ -23,7 +23,7 @@ umi cache ls [--depth ] fsExtra.removeSync(absTmpFilePath); } if (args._[0] === 'ls') { - const plies: number = args.depth === undefined ? 1 : args.depth; + const depth: number = args.depth ?? 1; const dirObj = getDirectorySize({ directoryPath: absTmpFilePath, number: plies + 1, From 7847ca6864c54f307fce064a02e0c2fb3e2c28f7 Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Mon, 13 Mar 2023 19:56:10 +0800 Subject: [PATCH 20/32] chore: for cr --- packages/preset-umi/src/commands/cache.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/preset-umi/src/commands/cache.ts b/packages/preset-umi/src/commands/cache.ts index c12e2be11361..4992db2bb4ee 100644 --- a/packages/preset-umi/src/commands/cache.ts +++ b/packages/preset-umi/src/commands/cache.ts @@ -18,15 +18,14 @@ umi cache ls [--depth ] `, configResolveMode: 'loose', fn: ({ args }) => { - const absTmpFilePath = join(api.paths.absNodeModulesPath, '.cache'); + const absCachePath = join(api.paths.absNodeModulesPath, '.cache'); if (args._[0] === 'clean') { - fsExtra.removeSync(absTmpFilePath); - } - if (args._[0] === 'ls') { + fsExtra.removeSync(absCachePath); + } else if (args._[0] === 'ls') { const depth: number = args.depth ?? 1; const dirObj = getDirectorySize({ - directoryPath: absTmpFilePath, - number: plies + 1, + directoryPath: absCachePath, + number: depth + 1, }); const tree: any = {}; const str = `[${dirObj.size}kb] node_modules/.cache`; @@ -63,8 +62,9 @@ function getDirectorySize({ if (isCreateTree) { obj.tree = {}; } - + fsExtra.ensureDirSync(directoryPath); const files = fsExtra.readdirSync(directoryPath); + files.forEach(function (file) { const filePath = join(directoryPath, file); const stats = fsExtra.statSync(filePath); From 503a523c174af8ea7114846a6cecfbeda9f82d46 Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Mon, 13 Mar 2023 20:37:49 +0800 Subject: [PATCH 21/32] chore: npm --- packages/preset-umi/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/preset-umi/package.json b/packages/preset-umi/package.json index d3eefdf67930..51c6139d4ddb 100644 --- a/packages/preset-umi/package.json +++ b/packages/preset-umi/package.json @@ -55,7 +55,7 @@ "react-router": "6.3.0", "react-router-dom": "6.3.0", "regenerator-runtime": "0.13.11", - "treeify": "^1.1.0" + "treeify": "1.1.0" }, "devDependencies": { "@manypkg/get-packages": "1.1.3", From f9d8214938a896f7a29de4f7c369949fe333ea0a Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Mon, 13 Mar 2023 21:47:49 +0800 Subject: [PATCH 22/32] fix: ci --- pnpm-lock.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8896e0335693..6ed099ad97a1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1719,7 +1719,7 @@ importers: react-router-dom: 6.3.0 regenerator-runtime: 0.13.11 sirv: 2.0.2 - treeify: ^1.1.0 + treeify: 1.1.0 vite: 4.1.4 dependencies: '@iconify/utils': 2.1.1 @@ -25103,7 +25103,7 @@ packages: pretty-format: 29.4.3 slash: 3.0.0 strip-json-comments: 3.1.1 - ts-node: 10.9.1_awa2wsr5thmg3i7jqycphctjfq + ts-node: 10.9.1_typescript@4.9.4 transitivePeerDependencies: - supports-color dev: true From 34b2b16a82a489481ae8a5b7dd335463baceb9f3 Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Mon, 17 Apr 2023 09:36:46 +0800 Subject: [PATCH 23/32] feat: mb --- packages/preset-umi/src/commands/cache.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/preset-umi/src/commands/cache.ts b/packages/preset-umi/src/commands/cache.ts index 4992db2bb4ee..61013f80f7e7 100644 --- a/packages/preset-umi/src/commands/cache.ts +++ b/packages/preset-umi/src/commands/cache.ts @@ -28,7 +28,7 @@ umi cache ls [--depth ] number: depth + 1, }); const tree: any = {}; - const str = `[${dirObj.size}kb] node_modules/.cache`; + const str = `[${getSize(dirObj.size)}] node_modules/.cache`; tree[str] = dirObj.tree; console.log(treeify.asTree(tree, true, true)); } @@ -73,7 +73,7 @@ function getDirectorySize({ const fileSize = Math.floor(stats.size / 1024); obj.size += fileSize; if (obj.tree) { - obj.tree[`[${fileSize}kb] ${file}`] = null; + obj.tree[`[${getSize(fileSize)}] ${file}`] = null; } } else if (stats.isDirectory()) { const objChild = getDirectorySize({ @@ -83,7 +83,7 @@ function getDirectorySize({ name: file, }); if (obj.tree) { - obj.tree[`[${objChild.size}kb] ${file}`] = objChild.tree; + obj.tree[`[${getSize(objChild.size)}] ${file}`] = objChild.tree; } obj.size += objChild.size; } @@ -91,3 +91,10 @@ function getDirectorySize({ return obj; } + +function getSize(size: number) { + if (size > 1024) { + return `${(size / 1024).toFixed(2)}MB`; + } + return `${size}KB`; +} From e78678b9cd9b555a3d57a7d6aedb21c5b8fbccd2 Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Mon, 17 Apr 2023 11:45:55 +0800 Subject: [PATCH 24/32] chore: pkg --- packages/preset-umi/package.json | 4 ++-- pnpm-lock.yaml | 21 ++++++++++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/packages/preset-umi/package.json b/packages/preset-umi/package.json index 2e16c2b5d701..4c2f2edae707 100644 --- a/packages/preset-umi/package.json +++ b/packages/preset-umi/package.json @@ -27,7 +27,6 @@ "dependencies": { "@iconify/utils": "2.1.1", "@svgr/core": "6.5.1", - "@types/treeify": "^1.0.0", "@umijs/ast": "workspace:*", "@umijs/babel-preset-umi": "workspace:*", "@umijs/bundler-utils": "workspace:*", @@ -58,7 +57,7 @@ "react-router": "6.3.0", "react-router-dom": "6.3.0", "regenerator-runtime": "0.13.11", - "treeify": "1.1.0" + "treeify": "^1.1.0" }, "devDependencies": { "@manypkg/get-packages": "1.1.3", @@ -66,6 +65,7 @@ "@types/ini": "1.3.31", "@types/madge": "^5.0.0", "@types/multer": "1.4.7", + "@types/treeify": "^1.0.0", "body-parser": "1.20.0", "ini": "3.0.0", "joi2types": "1.1.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 322a52bd3425..24ad6a177ae1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1159,10 +1159,10 @@ importers: version: link:../../packages/umi examples/umi-cache: - specifiers: - umi: workspace:* dependencies: - umi: link:../../packages/umi + umi: + specifier: workspace:* + version: link:../../packages/umi examples/umi-run: dependencies: @@ -2367,6 +2367,9 @@ importers: regenerator-runtime: specifier: 0.13.11 version: 0.13.11 + treeify: + specifier: ^1.1.0 + version: 1.1.0 devDependencies: '@manypkg/get-packages': specifier: 1.1.3 @@ -2383,6 +2386,9 @@ importers: '@types/multer': specifier: 1.4.7 version: 1.4.7 + '@types/treeify': + specifier: ^1.0.0 + version: 1.0.0 body-parser: specifier: 1.20.0 version: 1.20.0 @@ -13289,6 +13295,10 @@ packages: resolution: {integrity: sha512-Q5vtl1W5ue16D+nIaW8JWebSSraJVlK+EthKn7e7UcD4KWsaSJ8BqGPXNaPghgtcn/fhvrN17Tv8ksUsQpiplw==} dev: false + /@types/treeify@1.0.0: + resolution: {integrity: sha512-ONpcZAEYlbPx4EtJwfTyCDQJGUpKf4sEcuySdCVjK5Fj/3vHp5HII1fqa1/+qrsLnpYELCQTfVW/awsGJePoIg==} + dev: true + /@types/triple-beam@1.3.2: resolution: {integrity: sha512-txGIh+0eDFzKGC25zORnswy+br1Ha7hj5cMVwKIU7+s0U2AxxJru/jZSMU6OC9MJWP6+pc/hc6ZjyZShpsyY2g==} dev: true @@ -37318,6 +37328,11 @@ packages: resolution: {integrity: sha512-L0Orpi8qGpRG//Nd+H90vFB+3iHnue1zSSGmNOOCh1GLJ7rUKVwV2HvijphGQS2UmhUZewS9VgvxYIdgr+fG1A==} dev: true + /treeify@1.1.0: + resolution: {integrity: sha512-1m4RA7xVAJrSGrrXGs0L3YTwyvBs2S8PbRHaLZAkFw7JR8oIFwYtysxlBZhYIa7xSyiYJKZ3iGrrk55cGA3i9A==} + engines: {node: '>=0.6'} + dev: false + /treeverse@2.0.0: resolution: {integrity: sha512-N5gJCkLu1aXccpOTtqV6ddSEi6ZmGkh3hjmbu1IjcavJK4qyOVQmi0myQKM7z5jVGmD68SJoliaVrMmVObhj6A==} engines: {node: ^12.13.0 || ^14.15.0 || >=16.0.0} From dc227dd494c2d79c76822e4deb6310a672bf3816 Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Tue, 18 Apr 2023 12:36:05 +0800 Subject: [PATCH 25/32] fix: example --- examples/umi-cache/.umirc.ts | 1 - examples/umi-cache/package.json | 14 -------------- examples/umi-cache/src/pages/index.tsx | 7 ------- pnpm-lock.yaml | 6 ------ 4 files changed, 28 deletions(-) delete mode 100644 examples/umi-cache/.umirc.ts delete mode 100644 examples/umi-cache/package.json delete mode 100644 examples/umi-cache/src/pages/index.tsx diff --git a/examples/umi-cache/.umirc.ts b/examples/umi-cache/.umirc.ts deleted file mode 100644 index ff8b4c56321a..000000000000 --- a/examples/umi-cache/.umirc.ts +++ /dev/null @@ -1 +0,0 @@ -export default {}; diff --git a/examples/umi-cache/package.json b/examples/umi-cache/package.json deleted file mode 100644 index 260eb9f19bdf..000000000000 --- a/examples/umi-cache/package.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "name": "@example/umi-cache", - "private": true, - "author": "DaoYuan ", - "scripts": { - "build": "umi build", - "clean": "umi cache clean", - "dev": "umi dev", - "ls": "umi cache ls" - }, - "dependencies": { - "umi": "workspace:*" - } -} diff --git a/examples/umi-cache/src/pages/index.tsx b/examples/umi-cache/src/pages/index.tsx deleted file mode 100644 index 083c932f3bab..000000000000 --- a/examples/umi-cache/src/pages/index.tsx +++ /dev/null @@ -1,7 +0,0 @@ -export default function HomePage() { - return ( -
-

Yay! Welcome to umi!

-
- ); -} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 24ad6a177ae1..98e8cd8386ae 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1158,12 +1158,6 @@ importers: specifier: workspace:* version: link:../../packages/umi - examples/umi-cache: - dependencies: - umi: - specifier: workspace:* - version: link:../../packages/umi - examples/umi-run: dependencies: umi: From c6ce07c00c961f02d30071e572f8df4b3ad9b5fb Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Tue, 18 Apr 2023 13:16:31 +0800 Subject: [PATCH 26/32] perf: name --- packages/preset-umi/src/commands/cache.ts | 43 ++++++++--------------- 1 file changed, 15 insertions(+), 28 deletions(-) diff --git a/packages/preset-umi/src/commands/cache.ts b/packages/preset-umi/src/commands/cache.ts index 61013f80f7e7..979d702263ad 100644 --- a/packages/preset-umi/src/commands/cache.ts +++ b/packages/preset-umi/src/commands/cache.ts @@ -24,8 +24,8 @@ umi cache ls [--depth ] } else if (args._[0] === 'ls') { const depth: number = args.depth ?? 1; const dirObj = getDirectorySize({ - directoryPath: absCachePath, - number: depth + 1, + dir: absCachePath, + depth: depth + 1, }); const tree: any = {}; const str = `[${getSize(dirObj.size)}] node_modules/.cache`; @@ -36,37 +36,25 @@ umi cache ls [--depth ] }); }; -/** - * 获取目录信息 - * @param directoryPath 目录路径 - * @param number 展示目录层数 - * @param index 标记层数 - * @returns - */ interface GetDirectorySize { - directoryPath: string; - name?: string; - number?: number; - index?: number; + dir: string; + depth?: number; + current?: number; } -function getDirectorySize({ - directoryPath, - number = 2, - index = 1, -}: GetDirectorySize) { +function getDirectorySize({ dir, depth = 2, current = 1 }: GetDirectorySize) { const obj: { size: number; tree: any } = { size: 0, tree: null, }; - const isCreateTree = index < number; + const isCreateTree = current < depth; if (isCreateTree) { obj.tree = {}; } - fsExtra.ensureDirSync(directoryPath); - const files = fsExtra.readdirSync(directoryPath); + fsExtra.ensureDirSync(dir); + const files = fsExtra.readdirSync(dir); files.forEach(function (file) { - const filePath = join(directoryPath, file); + const filePath = join(dir, file); const stats = fsExtra.statSync(filePath); if (stats.isFile()) { @@ -77,10 +65,9 @@ function getDirectorySize({ } } else if (stats.isDirectory()) { const objChild = getDirectorySize({ - directoryPath: filePath, - index: index + 1, - number, - name: file, + dir: filePath, + current: current + 1, + depth, }); if (obj.tree) { obj.tree[`[${getSize(objChild.size)}] ${file}`] = objChild.tree; @@ -94,7 +81,7 @@ function getDirectorySize({ function getSize(size: number) { if (size > 1024) { - return `${(size / 1024).toFixed(2)}MB`; + return `${(size / 1024).toFixed(2)} MB`; } - return `${size}KB`; + return `${size} KB`; } From ad0f247e61af8adfd1ea9a41d18732ad15709c99 Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Tue, 18 Apr 2023 13:55:50 +0800 Subject: [PATCH 27/32] chore: idr --- packages/preset-umi/src/commands/cache.ts | 46 ++++++++++++++--------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/packages/preset-umi/src/commands/cache.ts b/packages/preset-umi/src/commands/cache.ts index 979d702263ad..20e830e90509 100644 --- a/packages/preset-umi/src/commands/cache.ts +++ b/packages/preset-umi/src/commands/cache.ts @@ -1,3 +1,4 @@ +import * as logger from '@umijs/utils/src/logger'; import { join } from 'path'; import treeify from 'treeify'; import { IApi } from 'umi'; @@ -19,30 +20,41 @@ umi cache ls [--depth ] configResolveMode: 'loose', fn: ({ args }) => { const absCachePath = join(api.paths.absNodeModulesPath, '.cache'); - if (args._[0] === 'clean') { - fsExtra.removeSync(absCachePath); - } else if (args._[0] === 'ls') { - const depth: number = args.depth ?? 1; - const dirObj = getDirectorySize({ - dir: absCachePath, - depth: depth + 1, - }); - const tree: any = {}; - const str = `[${getSize(dirObj.size)}] node_modules/.cache`; - tree[str] = dirObj.tree; - console.log(treeify.asTree(tree, true, true)); + if (fsExtra.existsSync(absCachePath)) { + if (args._[0] === 'clean') { + fsExtra.removeSync(absCachePath); + } else if (args._[0] === 'ls') { + const depth: number = args.depth ?? 1; + const dirObj = getDirectorySize({ + dir: absCachePath, + depth: depth + 1, + }); + const tree: any = {}; + const str = `[${getSize(dirObj.size)}] node_modules/.cache`; + tree[str] = dirObj.tree; + logger.info( + `[umi cache] dir info\n${treeify.asTree(tree, true, true)}`, + ); + } + } else { + logger.info('[umi cache] 当前没有缓存'); } }, }); }; -interface GetDirectorySize { +type Tree = null | { [key: string]: null | Tree }; + +interface IGetDirectorySize { dir: string; depth?: number; current?: number; } -function getDirectorySize({ dir, depth = 2, current = 1 }: GetDirectorySize) { - const obj: { size: number; tree: any } = { +function getDirectorySize({ dir, depth = 2, current = 1 }: IGetDirectorySize) { + const obj: { + size: number; + tree: Tree; + } = { size: 0, tree: null, }; @@ -50,10 +62,8 @@ function getDirectorySize({ dir, depth = 2, current = 1 }: GetDirectorySize) { if (isCreateTree) { obj.tree = {}; } - fsExtra.ensureDirSync(dir); const files = fsExtra.readdirSync(dir); - - files.forEach(function (file) { + files.forEach((file) => { const filePath = join(dir, file); const stats = fsExtra.statSync(filePath); From 70cba460dbb6b682d9a2d594dec3e93c46627d65 Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Tue, 18 Apr 2023 14:09:30 +0800 Subject: [PATCH 28/32] chore: api --- packages/preset-umi/src/commands/cache.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/preset-umi/src/commands/cache.ts b/packages/preset-umi/src/commands/cache.ts index 20e830e90509..599a506191be 100644 --- a/packages/preset-umi/src/commands/cache.ts +++ b/packages/preset-umi/src/commands/cache.ts @@ -1,8 +1,7 @@ -import * as logger from '@umijs/utils/src/logger'; +import { fsExtra, logger } from '@umijs/utils'; import { join } from 'path'; import treeify from 'treeify'; -import { IApi } from 'umi'; -import { fsExtra } from 'umi/plugin-utils'; +import { IApi } from '../types'; export default (api: IApi) => { api.registerCommand({ @@ -73,7 +72,7 @@ function getDirectorySize({ dir, depth = 2, current = 1 }: IGetDirectorySize) { if (obj.tree) { obj.tree[`[${getSize(fileSize)}] ${file}`] = null; } - } else if (stats.isDirectory()) { + } else { const objChild = getDirectorySize({ dir: filePath, current: current + 1, From 61047c6288540273ca1da84a124e30999638c6f4 Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Tue, 18 Apr 2023 14:23:22 +0800 Subject: [PATCH 29/32] chore: compiled --- packages/preset-umi/compiled/treeify/LICENSE | 21 ++++++++++++++ .../preset-umi/compiled/treeify/index.d.ts | 28 +++++++++++++++++++ packages/preset-umi/compiled/treeify/index.js | 1 + .../preset-umi/compiled/treeify/package.json | 1 + packages/preset-umi/package.json | 5 ++-- packages/preset-umi/src/commands/cache.ts | 2 +- pnpm-lock.yaml | 8 +++--- 7 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 packages/preset-umi/compiled/treeify/LICENSE create mode 100644 packages/preset-umi/compiled/treeify/index.d.ts create mode 100644 packages/preset-umi/compiled/treeify/index.js create mode 100644 packages/preset-umi/compiled/treeify/package.json diff --git a/packages/preset-umi/compiled/treeify/LICENSE b/packages/preset-umi/compiled/treeify/LICENSE new file mode 100644 index 000000000000..1df35f3a339f --- /dev/null +++ b/packages/preset-umi/compiled/treeify/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2012-2018 Luke Plaster + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/packages/preset-umi/compiled/treeify/index.d.ts b/packages/preset-umi/compiled/treeify/index.d.ts new file mode 100644 index 000000000000..938772cc99d6 --- /dev/null +++ b/packages/preset-umi/compiled/treeify/index.d.ts @@ -0,0 +1,28 @@ +// Type definitions for treeify 1.0 +// Project: https://github.com/notatestuser/treeify +// Definitions by: Mike North +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.1 + +export interface TreeObject { + [k: string]: TreeValue; +} +export type TreeValue = string | TreeObject; + +export function asTree( + treeObj: TreeObject, + showValues: boolean, + hideFunctions: boolean +): string; + +export function asLines( + treeObj: TreeObject, + showValues: boolean, + lineCallback: (line: string) => void +): string; +export function asLines( + treeObj: TreeObject, + showValues: boolean, + hideFunctions: boolean, + lineCallback: (line: string) => void +): string; diff --git a/packages/preset-umi/compiled/treeify/index.js b/packages/preset-umi/compiled/treeify/index.js new file mode 100644 index 000000000000..5fbf8806a081 --- /dev/null +++ b/packages/preset-umi/compiled/treeify/index.js @@ -0,0 +1 @@ +(function(){var e={290:function(e){(function(r,n){if(true){e.exports=n()}else{}})(this,(function(){function makePrefix(e,r){var n=r?"└":"├";if(e){n+="─ "}else{n+="──┐"}return n}function filterKeys(e,r){var n=[];for(var t in e){if(!e.hasOwnProperty(t)){continue}if(r&&typeof e[t]==="function"){continue}n.push(t)}return n}function growBranch(e,r,n,t,f,i,a){var o="",u=0,c,_,s=t.slice(0);if(s.push([r,n])&&t.length>0){t.forEach((function(e,n){if(n>0){o+=(e[1]?" ":"│")+" "}if(!_&&e[0]===r){_=true}}));o+=makePrefix(e,n)+e;f&&(typeof r!=="object"||r instanceof Date)&&(o+=": "+r);_&&(o+=" (circular ref.)");a(o)}if(!_&&typeof r==="object"){var p=filterKeys(r,i);p.forEach((function(e){c=++u===p.length;growBranch(e,r[e],c,s,f,i,a)}))}}var e={};e.asLines=function(e,r,n,t){var f=typeof n!=="function"?n:false;growBranch(".",e,false,[],r,f,t||n)};e.asTree=function(e,r,n){var t="";growBranch(".",e,false,[],r,n,(function(e){t+=e+"\n"}));return t};return e}))}};var r={};function __nccwpck_require__(n){var t=r[n];if(t!==undefined){return t.exports}var f=r[n]={exports:{}};var i=true;try{e[n].call(f.exports,f,f.exports,__nccwpck_require__);i=false}finally{if(i)delete r[n]}return f.exports}if(typeof __nccwpck_require__!=="undefined")__nccwpck_require__.ab=__dirname+"/";var n=__nccwpck_require__(290);module.exports=n})(); \ No newline at end of file diff --git a/packages/preset-umi/compiled/treeify/package.json b/packages/preset-umi/compiled/treeify/package.json new file mode 100644 index 000000000000..a352e42a354b --- /dev/null +++ b/packages/preset-umi/compiled/treeify/package.json @@ -0,0 +1 @@ +{"name":"treeify","version":"1.1.0","author":"Luke Plaster ","license":"MIT"} diff --git a/packages/preset-umi/package.json b/packages/preset-umi/package.json index 4c2f2edae707..a3dd277c1ca0 100644 --- a/packages/preset-umi/package.json +++ b/packages/preset-umi/package.json @@ -56,8 +56,7 @@ "react-dom": "18.1.0", "react-router": "6.3.0", "react-router-dom": "6.3.0", - "regenerator-runtime": "0.13.11", - "treeify": "^1.1.0" + "regenerator-runtime": "0.13.11" }, "devDependencies": { "@manypkg/get-packages": "1.1.3", @@ -72,6 +71,7 @@ "multer": "1.4.4", "os-locale": "^6.0.2", "sirv": "2.0.2", + "treeify": "^1.1.0", "vite": "4.2.0" }, "publishConfig": { @@ -88,6 +88,7 @@ "ini", "joi2types", "os-locale", + "treeify", "sirv" ], "externals": { diff --git a/packages/preset-umi/src/commands/cache.ts b/packages/preset-umi/src/commands/cache.ts index 599a506191be..0a32d53180cf 100644 --- a/packages/preset-umi/src/commands/cache.ts +++ b/packages/preset-umi/src/commands/cache.ts @@ -1,6 +1,6 @@ import { fsExtra, logger } from '@umijs/utils'; import { join } from 'path'; -import treeify from 'treeify'; +import treeify from '../../compiled/treeify'; import { IApi } from '../types'; export default (api: IApi) => { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 98e8cd8386ae..f82166c3edf8 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2361,9 +2361,6 @@ importers: regenerator-runtime: specifier: 0.13.11 version: 0.13.11 - treeify: - specifier: ^1.1.0 - version: 1.1.0 devDependencies: '@manypkg/get-packages': specifier: 1.1.3 @@ -2401,6 +2398,9 @@ importers: sirv: specifier: 2.0.2 version: 2.0.2 + treeify: + specifier: ^1.1.0 + version: 1.1.0 vite: specifier: 4.2.0 version: 4.2.0(@types/node@18.11.18)(less@4.1.3) @@ -37325,7 +37325,7 @@ packages: /treeify@1.1.0: resolution: {integrity: sha512-1m4RA7xVAJrSGrrXGs0L3YTwyvBs2S8PbRHaLZAkFw7JR8oIFwYtysxlBZhYIa7xSyiYJKZ3iGrrk55cGA3i9A==} engines: {node: '>=0.6'} - dev: false + dev: true /treeverse@2.0.0: resolution: {integrity: sha512-N5gJCkLu1aXccpOTtqV6ddSEi6ZmGkh3hjmbu1IjcavJK4qyOVQmi0myQKM7z5jVGmD68SJoliaVrMmVObhj6A==} From 02b4f2106baf4ec22c017dd63343382f5960f23b Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Wed, 19 Apr 2023 13:37:37 +0800 Subject: [PATCH 30/32] perf: yc --- packages/preset-umi/src/commands/cache.ts | 44 ++++++++++++++--------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/packages/preset-umi/src/commands/cache.ts b/packages/preset-umi/src/commands/cache.ts index 0a32d53180cf..741d4c1493d1 100644 --- a/packages/preset-umi/src/commands/cache.ts +++ b/packages/preset-umi/src/commands/cache.ts @@ -1,42 +1,52 @@ import { fsExtra, logger } from '@umijs/utils'; -import { join } from 'path'; -import treeify from '../../compiled/treeify'; +import { join, relative } from 'path'; +import treeify, { type TreeObject } from '../../compiled/treeify'; import { IApi } from '../types'; -export default (api: IApi) => { - api.registerCommand({ - name: 'cache', - description: 'run the script commands, manage umi cache', - details: ` +const details = ` umi cache - # clear cache directory umi cache clean - # display directory information, --depth is the number of directory levels umi cache ls [--depth ] -`, +`; + +export default (api: IApi) => { + api.registerCommand({ + name: 'cache', + description: 'run the script commands, manage umi cache', + details, configResolveMode: 'loose', fn: ({ args }) => { - const absCachePath = join(api.paths.absNodeModulesPath, '.cache'); + const absCachePath = join( + api.cwd, + api.config.cacheDirectoryPath || 'node_modules/.cache', + ); + const position = relative(api.cwd, absCachePath); if (fsExtra.existsSync(absCachePath)) { if (args._[0] === 'clean') { fsExtra.removeSync(absCachePath); + logger.ready(`[umi cache] cache directory is cleaned (${position})`); } else if (args._[0] === 'ls') { const depth: number = args.depth ?? 1; const dirObj = getDirectorySize({ dir: absCachePath, depth: depth + 1, }); - const tree: any = {}; - const str = `[${getSize(dirObj.size)}] node_modules/.cache`; + const tree: Tree = {}; + const str = `[${getSize(dirObj.size)}] ${position}`; tree[str] = dirObj.tree; logger.info( - `[umi cache] dir info\n${treeify.asTree(tree, true, true)}`, + `[umi cache] dir info\n${treeify.asTree( + tree as TreeObject, + true, + true, + )}`, ); } } else { - logger.info('[umi cache] 当前没有缓存'); + logger.warn(`[umi cache] unknown command ${args._[0]}`); + console.log(details); } }, }); @@ -57,8 +67,8 @@ function getDirectorySize({ dir, depth = 2, current = 1 }: IGetDirectorySize) { size: 0, tree: null, }; - const isCreateTree = current < depth; - if (isCreateTree) { + const needCreateTree = current < depth; + if (needCreateTree) { obj.tree = {}; } const files = fsExtra.readdirSync(dir); From 507b06a9834081034827c108e19a33bcd558f54f Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Wed, 19 Apr 2023 13:40:42 +0800 Subject: [PATCH 31/32] chore: npm --- packages/preset-umi/package.json | 4 ++-- pnpm-lock.yaml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/preset-umi/package.json b/packages/preset-umi/package.json index a3dd277c1ca0..7ecae5680bec 100644 --- a/packages/preset-umi/package.json +++ b/packages/preset-umi/package.json @@ -64,14 +64,14 @@ "@types/ini": "1.3.31", "@types/madge": "^5.0.0", "@types/multer": "1.4.7", - "@types/treeify": "^1.0.0", + "@types/treeify": "1.0.0", "body-parser": "1.20.0", "ini": "3.0.0", "joi2types": "1.1.1", "multer": "1.4.4", "os-locale": "^6.0.2", "sirv": "2.0.2", - "treeify": "^1.1.0", + "treeify": "1.1.0", "vite": "4.2.0" }, "publishConfig": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f82166c3edf8..b4bed3027d5a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2378,7 +2378,7 @@ importers: specifier: 1.4.7 version: 1.4.7 '@types/treeify': - specifier: ^1.0.0 + specifier: 1.0.0 version: 1.0.0 body-parser: specifier: 1.20.0 @@ -2399,7 +2399,7 @@ importers: specifier: 2.0.2 version: 2.0.2 treeify: - specifier: ^1.1.0 + specifier: 1.1.0 version: 1.1.0 vite: specifier: 4.2.0 From 8ea3f9d95fd076a946192fd7d6efa44335f2005b Mon Sep 17 00:00:00 2001 From: DaoYuan Date: Wed, 19 Apr 2023 17:45:14 +0800 Subject: [PATCH 32/32] chore: docs --- docs/docs/api/commands.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/docs/api/commands.md b/docs/docs/api/commands.md index 61ea8ceddabf..2e9ba1d5c2c1 100644 --- a/docs/docs/api/commands.md +++ b/docs/docs/api/commands.md @@ -17,6 +17,7 @@ Commands: build build app for production config umi config cli + cache run the script commands, manage umi cache dev dev server for development help show commands help lint lint source code using eslint and stylelint