|
| 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 | +}; |
0 commit comments