Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: umi cache #10730

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
153748c
feat: init
txp1035 Mar 13, 2023
8135f93
chore: init
txp1035 Mar 13, 2023
a3a90aa
chore: init
txp1035 Mar 13, 2023
d01a19b
feat: clean
txp1035 Mar 13, 2023
81469c1
feat: ls and examples
txp1035 Mar 13, 2023
f0a3669
chore: lock
txp1035 Mar 13, 2023
8cf04c3
chore: details
txp1035 Mar 13, 2023
a392ff9
chore: the same parameter as npm
txp1035 Mar 13, 2023
bd6a7fb
chore: depth
txp1035 Mar 13, 2023
25c8c21
chore: Compatible 0
txp1035 Mar 13, 2023
c0f233e
chore: style
txp1035 Mar 13, 2023
734ca60
chore: tree
txp1035 Mar 13, 2023
7f807b1
fix: ci
txp1035 Mar 13, 2023
96a67aa
fix: ci
txp1035 Mar 13, 2023
266c0b4
perf: tree
txp1035 Mar 13, 2023
ad05fe5
fix: first of tree
txp1035 Mar 13, 2023
6fb7ca1
fix: undefined show
txp1035 Mar 13, 2023
ac42733
Update packages/preset-umi/src/commands/cache.ts
txp1035 Mar 13, 2023
1babcf2
Update packages/preset-umi/src/commands/cache.ts
txp1035 Mar 13, 2023
7847ca6
chore: for cr
txp1035 Mar 13, 2023
503a523
chore: npm
txp1035 Mar 13, 2023
f9d8214
fix: ci
txp1035 Mar 13, 2023
34b2b16
feat: mb
txp1035 Apr 17, 2023
4742c12
chore(merge): Merge branch 'master' into umicache
txp1035 Apr 17, 2023
0ea392f
chore(merge): Merge branch 'master' into umicache
txp1035 Apr 17, 2023
e78678b
chore: pkg
txp1035 Apr 17, 2023
dc227dd
fix: example
txp1035 Apr 18, 2023
c6ce07c
perf: name
txp1035 Apr 18, 2023
ad0f247
chore: idr
txp1035 Apr 18, 2023
70cba46
chore: api
txp1035 Apr 18, 2023
61047c6
chore: compiled
txp1035 Apr 18, 2023
02b4f21
perf: yc
txp1035 Apr 19, 2023
507b06a
chore: npm
txp1035 Apr 19, 2023
8ea3f9d
chore: docs
txp1035 Apr 19, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/docs/api/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions packages/preset-umi/compiled/treeify/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2012-2018 Luke Plaster <[email protected]>

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.
28 changes: 28 additions & 0 deletions packages/preset-umi/compiled/treeify/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Type definitions for treeify 1.0
// Project: https://github.com/notatestuser/treeify
// Definitions by: Mike North <https://github.com/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;
1 change: 1 addition & 0 deletions packages/preset-umi/compiled/treeify/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/preset-umi/compiled/treeify/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"treeify","version":"1.1.0","author":"Luke Plaster <[email protected]>","license":"MIT"}
3 changes: 3 additions & 0 deletions packages/preset-umi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@
"@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",
"multer": "1.4.4",
"os-locale": "^6.0.2",
"sirv": "2.0.2",
"treeify": "1.1.0",
"vite": "4.2.0"
},
"publishConfig": {
Expand All @@ -86,6 +88,7 @@
"ini",
"joi2types",
"os-locale",
"treeify",
"sirv"
],
"externals": {
Expand Down
106 changes: 106 additions & 0 deletions packages/preset-umi/src/commands/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { fsExtra, logger } from '@umijs/utils';
import { join, relative } from 'path';
import treeify, { type TreeObject } from '../../compiled/treeify';
import { IApi } from '../types';

const details = `
umi cache
# clear cache directory
umi cache clean
# display directory information, --depth is the number of directory levels
umi cache ls [--depth <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.cwd,
api.config.cacheDirectoryPath || 'node_modules/.cache',
);
const position = relative(api.cwd, absCachePath);
if (fsExtra.existsSync(absCachePath)) {
if (args._[0] === 'clean') {
fsExtra.removeSync(absCachePath);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里要不要顺便把 .umi 文件夹删除?临时文件严格来说也算是缓存文件?就顺便把 umi clean 命令做了?
@sorrycc cc

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: Tree = {};
const str = `[${getSize(dirObj.size)}] ${position}`;
tree[str] = dirObj.tree;
logger.info(
`[umi cache] dir info\n${treeify.asTree(
tree as TreeObject,
true,
true,
)}`,
);
}
} else {
logger.warn(`[umi cache] unknown command ${args._[0]}`);
console.log(details);
}
},
});
};

type Tree = null | { [key: string]: null | Tree };

interface IGetDirectorySize {
dir: string;
depth?: number;
current?: number;
}
function getDirectorySize({ dir, depth = 2, current = 1 }: IGetDirectorySize) {
const obj: {
size: number;
tree: Tree;
} = {
size: 0,
tree: null,
};
const needCreateTree = current < depth;
if (needCreateTree) {
obj.tree = {};
}
const files = fsExtra.readdirSync(dir);
files.forEach((file) => {
const filePath = join(dir, file);
const stats = fsExtra.statSync(filePath);

if (stats.isFile()) {
const fileSize = Math.floor(stats.size / 1024);
obj.size += fileSize;
if (obj.tree) {
obj.tree[`[${getSize(fileSize)}] ${file}`] = null;
}
} else {
const objChild = getDirectorySize({
dir: filePath,
current: current + 1,
depth,
});
if (obj.tree) {
obj.tree[`[${getSize(objChild.size)}] ${file}`] = objChild.tree;
}
obj.size += objChild.size;
}
});

return obj;
}

function getSize(size: number) {
if (size > 1024) {
return `${(size / 1024).toFixed(2)} MB`;
}
return `${size} KB`;
}
1 change: 1 addition & 0 deletions packages/preset-umi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,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),
};
Expand Down
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.