Skip to content

Commit

Permalink
✨feat: update ui for bash and powerful Result
Browse files Browse the repository at this point in the history
  • Loading branch information
BoyYangzai committed Jun 28, 2023
1 parent d3a60b6 commit 6f873d8
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 39 deletions.
14 changes: 14 additions & 0 deletions examples/diy/.doctor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { DoctorLevel } from "@doctors/core";
import { defineConfig } from "@doctors/npm-pkg";

export default defineConfig({
npmPkg: {
peerDepAndDepRepeat: {
level: DoctorLevel.WARN,
exclude: ["@doctors/core"],
},
checkPkgFilesExist: {
level: DoctorLevel.ERROR,
},
},
});
5 changes: 5 additions & 0 deletions examples/diy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
"doctor:diy": "doctor diy",
"test": "npm run doctor:diy"
},
"files": [
"dist",
"./bin1",
"./noExist"
],
"dependencies": {
"@doctors/diy": "workspace:^"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/npm-pkg/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"files": [
"dist1",
"./bin1",
"./noexist",
"./noExist",
"dist2/*.js",
"bin2/*.js"
],
Expand Down
73 changes: 43 additions & 30 deletions packages/core/src/generatePreset.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { ApplyPluginsType } from "@umijs/core/dist/types";
import { applyConfigFromSchema } from "./config";
import { DoctorLevel, IApi, RuleResItem } from "./types";
import { applyTypeEffect } from "./utils";
import {
applyTypeEffect,
toUpperUnderscore,
mergeObjectsByProp,
} from "./utils";
import { chalk } from "@umijs/utils";

function transformString(str: string) {
Expand Down Expand Up @@ -68,44 +72,53 @@ export default function generatePreset({
});

//----------------- checking ------------------
const webToolsRes = (
const checkedResult = (
await api.applyPlugins({
key: `addDoctor${transformString(command)}Check`,
type: ApplyPluginsType.add,
args: meta,
})
).filter(Boolean);

sort(webToolsRes.filter(Boolean)).forEach((i, index) => {
switch (i?.doctorLevel) {
case DoctorLevel.SUCCESS:
console.log(
`${chalk.green("pass🎉🎉")} Doctor rules ${index}: ${
i.label
}\n${chalk.green("Suggestion:")} ${i.description} \n`
);
break;
case DoctorLevel.WARN:
console.log(
`${chalk.yellowBright("warn")} Doctor rules ${index}: ${
i.label
}\n${chalk.yellowBright("Suggestion:")} ${i.description} \n`
);
break;
case DoctorLevel.ERROR:
console.log(
`${chalk.red("error!!")} Doctor rules ${index}: ${
i.label
}\n${chalk.red("Suggestion:")} ${i.description} \n`
);
break;
default:
break;
}
const mergedRes = sort(mergeObjectsByProp(checkedResult.filter(Boolean)));
mergedRes.forEach((rule, index) => {
console.log(
chalk.greenBright(
`${chalk.greenBright(
`${index++ < 10 ? "0" + index++ : index++}. ${toUpperUnderscore(
rule.label
)}`
)}\n`
)
);
rule.descriptions.forEach((i) => {
switch (i?.level) {
case DoctorLevel.SUCCESS:
console.log(
`${chalk.bgGreenBright(" DoctorLevel SUCCESS 🎉🎉 ")}`
);
console.log(`${chalk.greenBright(" WHY ")}${i.suggestion} \n`);
break;
break;
case DoctorLevel.WARN:
console.log(`${chalk.bgYellowBright("DoctorLevel WARN ")}`);
console.log(
`${chalk.greenBright(" SUGGESTION ")}${i.suggestion} \n`
);
break;
case DoctorLevel.ERROR:
console.log(`${chalk.bgRedBright(" DoctorLevel Error ")}`);
console.log(
`${chalk.greenBright(" SUGGESTION ")}${i.suggestion} \n`
);
break;
default:
break;
}
});
});

//----------------- check end ------------------
if (webToolsRes.some((i) => i.doctorLevel === DoctorLevel.ERROR)) {
if (mergedRes.some((i) => i.doctorLevel === DoctorLevel.ERROR)) {
process.exit(1);
}

Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ export type Nullify<T> = {
};

export interface RuleResItem {
descriptions: {
level: DoctorLevel;
suggestion: string;
}[];
label: string;
description: string;
doctorLevel: DoctorLevel | "success";
doctorLevel: DoctorLevel;
}
34 changes: 32 additions & 2 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { join } from "path";
import { IApi, PluginMeta } from "./types";
import { IApi, PluginMeta, RuleResItem } from "./types";
import { logger } from "@umijs/utils";

const fs = require("fs");
Expand Down Expand Up @@ -57,7 +57,7 @@ export function getDoctorDependencies() {
const firstIndex = arr.findIndex((o) => o.name === obj.name);
if (index !== firstIndex) {
logger.warn(
`there are repeated Plugins named ${obj.name} with ${arr[firstIndex].path} and ${arr[index].path}`
`there are repeated Plugins named ${obj.name} with ${arr[firstIndex].path} and ${arr[index].path}\n`
);
}
return index === firstIndex; // 只保留第一个出现的对象
Expand Down Expand Up @@ -118,3 +118,33 @@ export function applyTypeEffect(api: IApi, name: string) {
api.registerMethod({ name });
});
}

export function toUpperUnderscore(str) {
return str
.replace(/([A-Z])/g, "_$1") // 在大写字母前添加下划线
.toUpperCase(); // 转换为大写字母
}

export function mergeObjectsByProp(arr) {
const result: RuleResItem[] = [];
const map = new Map();
for (const obj of arr) {
const key = obj["label"];
if (map.has(key)) {
map
.get(key)
.descriptions.push({
suggestion: obj.description,
level: obj.doctorLevel,
});
} else {
const realItem = Object.assign({}, obj);
realItem.descriptions = [
{ suggestion: obj.description, level: obj.doctorLevel },
];
map.set(key, realItem);
}
}
map.forEach((value) => result.push(value));
return result;
}
8 changes: 4 additions & 4 deletions packages/npm-pkg/src/features/checkPkgFilesExist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ const outputFileType = ["js", "jsx", "ts", "tsx", "html", "css"];
export default (api: IApi) => {
api.addDoctorNpmPkgCheck(() => {
const userConfig = api.userConfig as ConfigSchema;
const absoulePath: string[] = [];
const absolutePath: string[] = [];
const warns: DoctorMeta[] = [];

api.pkg.files.forEach((file: string) => {
absoulePath.push(path.join(api.cwd, file));
api.pkg.files?.forEach((file: string) => {
absolutePath.push(path.join(api.cwd, file));
});

for (let path of absoulePath) {
for (let path of absolutePath) {
if (fs.existsSync(path)) {
const stat = fs.statSync(path);
if (stat.isDirectory()) {
Expand Down
2 changes: 1 addition & 1 deletion packages/npm-pkg/src/features/dupInPeerDependences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function dupInPeerDependences(api: IApi) {
!userConfig.npmPkg?.peerDepAndDepRepeat?.exclude?.includes(pkg)
) {
warns.push({
label: "dupInPeerDependences",
label: "depInPeerDependences",
description: `The package ${pkg} is both a peerDependency and a dependency,Please remove one from the package.json file base on project requirements`,
doctorLevel:
userConfig.npmPkg?.peerDepAndDepRepeat?.level ||
Expand Down

0 comments on commit 6f873d8

Please sign in to comment.