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: add web-tools check, checkRegistry #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
79 changes: 79 additions & 0 deletions packages/web-tools/src/features/checkRegistry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { DoctorLevel } from "@doctors/core";
import { IApi } from "../type";
import { chalkByDoctorLevel } from "@doctors/utils";
import { execCommand } from "@doctors/utils";

const commonRegistryHosts = [
"registry.npmjs.org", // npm
"registry.yarnpkg.com", // yarn
"mirrors.cloud.tencent.com", // tencent
"r.cnpmjs.org", // cnpm
"registry.npmmirror.com", // taobao
"skimdb.npmjs.com", // npmMirror
];

const deprecatedRegistryHosts = [
"registry.npm.taobao.org", // taobao
];

async function getCurrentRegistry(packageManager: string) {
try {
const registry = await execCommand(`${packageManager} config get registry`);
return registry;
} catch (err) {
return undefined;
}
}

function getEachLevel(currentRegistry?: string): DoctorLevel {
let level = DoctorLevel.WARN;
if (!currentRegistry) {
return level;
}
deprecatedRegistryHosts.forEach((registry) => {
if (currentRegistry.includes(registry)) level = DoctorLevel.ERROR;
});
commonRegistryHosts.forEach((registry) => {
if (currentRegistry.includes(registry)) level = DoctorLevel.SUCCESS;
});
return level;
}

export default (api: IApi) => {
api.addDoctorWebToolsCheck(async () => {
const ruleLevel = (api.userConfig.tools?.nodeVersion ||
DoctorLevel.WARN) as DoctorLevel;

if (ruleLevel === DoctorLevel.OFF) return;
const registries = [
await getCurrentRegistry("npm"),
await getCurrentRegistry("yarn"),
await getCurrentRegistry("pnpm"),
];
// Array<string> = [npmRegistry, yarnRegistry, pnpmRegistry];

const eachLevel = registries.map((registry) => getEachLevel(registry));

let overallLevel = DoctorLevel.SUCCESS;
if (eachLevel.includes(DoctorLevel.ERROR)) {
overallLevel = DoctorLevel.ERROR;
} else if (eachLevel.includes(DoctorLevel.WARN)) {
overallLevel = DoctorLevel.WARN;
}

return {
label: "Package Manager Registry",
description: `Registry should be stable and recognized: \n\n${chalkByDoctorLevel(
eachLevel[0],
` NPM Registry: ${registries[0]}`
)}\n${chalkByDoctorLevel(
eachLevel[1],
` YARN Registry: ${registries[1]}`
)}\n${chalkByDoctorLevel(
eachLevel[2],
` PNPM Registry: ${registries[2]}`
)}`,
doctorLevel: overallLevel,
};
});
};
1 change: 1 addition & 0 deletions packages/web-tools/src/features/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export default [
require.resolve("./checkChrome"),
require.resolve("./checkDefaultBrowser"),
require.resolve("./checkGitSshKeyPersistent"),
require.resolve("./checkRegistry"),
];