# nrm 基本原理

nrm 的仓库地址在这里 https://github.com/Pana/nrm

镜像地址列表

https://github.com/Pana/nrm/blob/master/registries.json (opens new window)

{
  "npm": {
    "home": "https://www.npmjs.org",
    "registry": "https://registry.npmjs.org/"
  },
  "yarn": {
    "home": "https://yarnpkg.com",
    "registry": "https://registry.yarnpkg.com/"
  },
  "tencent": {
    "home": "https://mirrors.cloud.tencent.com/npm/",
    "registry": "https://mirrors.cloud.tencent.com/npm/"
  },
  "cnpm": {
    "home": "https://cnpmjs.org",
    "registry": "https://r.cnpmjs.org/"
  },
  "taobao": {
    "home": "https://npmmirror.com",
    "registry": "https://registry.npmmirror.com/"
  },
  "npmMirror": {
    "home": "https://skimdb.npmjs.com/",
    "registry": "https://skimdb.npmjs.com/registry/"
  }
}
// https://github.com/Pana/nrm/blob/master/constants.js
const REGISTRIES = require('./registries.json');
const { NRMRC, NPMRC, REGISTRY, REGISTRIES } = require('./constants');
async function getRegistries() {
  const customRegistries = await readFile(NRMRC);
  return Object.assign({}, REGISTRIES, customRegistries);
}

nrm ls 在命令行列出镜像源地址列表

async function onList() {
  const currentRegistry = await getCurrentRegistry();
  const registries = await getRegistries();
  const keys = Object.keys(registries);
  const length = Math.max(...keys.map(key => key.length)) + 3;

  const messages = keys.map(key => {
    const registry = registries[key];
    const prefix = isLowerCaseEqual(registry[REGISTRY], currentRegistry) ? chalk.green.bold('* ') : '  ';
    return prefix + key + geneDashLine(key, length) + registry[REGISTRY];
  });

  printMessages(messages);
}

nrm use [name] 将选择的镜像源配置写入 npmrc

async function onUse(name) {
  if (await isRegistryNotFound(name)) {
    return;
  }

  const registries = await getRegistries();
  const registry = registries[name];
  const npmrc = await readFile(NPMRC);
  await writeFile(NPMRC, Object.assign(npmrc, registry));

  printSuccess(`The registry has been changed to '${name}'.`);
}