Skip to content

Commit d3899df

Browse files
committed
✨refactor(feature): 增加切换NPM源地址命令
1 parent 4fc61ba commit d3899df

File tree

5 files changed

+91
-2
lines changed

5 files changed

+91
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ npm i -g code-genius
2828
| fix | --pattern \<pattern\> | './src' | 运行 eslint 静态扫描和修复代码中存在的问题 |
2929
| format | --pattern \<pattern\> | ./src' | 运行 prettier 格式化代码风格 |
3030
| depcheck | -- | -- | 运行 npm-check 检查过时的、不正确的和未使用的依赖项 |
31+
| registry | -- | -- | 切换 NPM 镜像地址 |
3132

3233
## 执照
3334

src/command/npm-registry.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { printInfo, printWarring } from "./../shared/index";
2+
import enquirer from "enquirer";
3+
import { execCommand } from "../shared";
4+
import { npmRegisters } from "../shared/config";
5+
6+
interface PromptResult {
7+
registry: string;
8+
}
9+
10+
export const npmRegistry = async () => {
11+
printInfo("当前 NPM 镜像地址(全局):");
12+
await execCommand("npm", ["config", "get", "registry", "-g"], {
13+
stdio: "inherit",
14+
});
15+
printInfo("当前 NPM 镜像地址(非全局)");
16+
await execCommand("npm", ["config", "get", "registry"], {
17+
stdio: "inherit",
18+
});
19+
printWarring("PS: 非全局查询结果受`.npmrc`影响会不准确。");
20+
21+
const registersChoices = npmRegisters.map(({ key, value }) => {
22+
const formatKey = `${key}:`.padEnd(15);
23+
return {
24+
name: value,
25+
message: `${formatKey}${value}`,
26+
};
27+
});
28+
29+
const result = await enquirer.prompt<PromptResult>([
30+
{
31+
name: "registry",
32+
type: "select",
33+
message: "请选择新 NPM 镜像",
34+
choices: registersChoices,
35+
},
36+
]);
37+
await execCommand("npm", ["config", "set", "registry", result.registry], {
38+
stdio: "inherit",
39+
});
40+
41+
printInfo("最新 NPM 镜像地址(全局):");
42+
await execCommand("npm", ["config", "get", "registry", "-g"], {
43+
stdio: "inherit",
44+
});
45+
printInfo("最新 NPM 镜像地址(非全局)");
46+
await execCommand("npm", ["config", "get", "registry"], {
47+
stdio: "inherit",
48+
});
49+
printWarring("PS: 非全局查询结果受`.npmrc`影响会不准确。");
50+
};

src/setup.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { gitInitSimpleHooks } from "./command/git-init-hooks";
1818
import { clear } from "./command/clear";
1919
import { npmRun } from "./command/npm-run";
2020
import { npmDepCheck } from "./command/npm-dep-check";
21+
import { npmRegistry } from "./command/npm-registry";
2122

2223
export const commandSet: CommandSet = {
2324
gitCommitCmd: (cli: CAC) => {
@@ -63,7 +64,7 @@ export const commandSet: CommandSet = {
6364
cli
6465
.command(
6566
"depcheck",
66-
"运行 npm-check 检查过时的、不正确的和未使用的依赖项",
67+
"运行 npm-check 检查过时的、不正确的和未使用的依赖项"
6768
)
6869
.action(async () => {
6970
await npmDepCheck();
@@ -74,6 +75,11 @@ export const commandSet: CommandSet = {
7475
await npmRun();
7576
});
7677
},
78+
npmRegistryCmd: (cli: CAC) => {
79+
cli.command("registry", "切换 NPM 镜像地址").action(async () => {
80+
await npmRegistry();
81+
});
82+
},
7783
eslintFixCmd: (cli: CAC) => {
7884
cli
7985
.command("fix", "运行 eslint 静态扫描和修复代码中存在的问题")

src/shared/config.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CommitScope, CommitType } from "./types";
1+
import { CommitScope, CommitType, KeyValue } from "./types";
22

33
export const clearGlob = ["./dist/"];
44

@@ -95,3 +95,30 @@ export const gitCommitScopes: Array<CommitScope> = [
9595
description: "用户界面",
9696
},
9797
];
98+
99+
export const npmRegisters: Array<KeyValue> = [
100+
{
101+
key: "npm",
102+
value: "https://registry.npmjs.org/",
103+
},
104+
{
105+
key: "yarn",
106+
value: "https://registry.yarnpkg.com/",
107+
},
108+
{
109+
key: "tencent",
110+
value: "https://mirrors.cloud.tencent.com/npm/",
111+
},
112+
{
113+
key: "cnpm",
114+
value: "https://r.cnpmjs.org/",
115+
},
116+
{
117+
key: "taobao",
118+
value: "https://registry.npmmirror.com/",
119+
},
120+
{
121+
key: "npmMirror",
122+
value: "https://skimdb.npmjs.com/registry/",
123+
},
124+
];

src/shared/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ export interface CommitScope {
1515
description: string;
1616
}
1717

18+
export interface KeyValue {
19+
key: string;
20+
value: string;
21+
}
22+
1823
export interface EsLintOptions {
1924
eslintrc: string;
2025
ignorePath: string;

0 commit comments

Comments
 (0)