Why do YOU need URM

I believe that many partners have encountered NPM installation is too slow, especially when our project depends on too much, often three minutes, five minutes or even longer installation time is undoubtedly very frustrating 😭, and even more, you will see the following error:

Not only does this affect our development efficiency, it can also cause a release failure (CI release). The package manager (NPM, YARN, etc.) can be used to fix this problem. Set up a domestic, faster, and more stable Registry.

However, this also introduces a new problem. The configuration file format of each front-end package manager is different. Even after YARN V2, the configuration file format has changed greatly compared to V1. Below are configuration files for different package managers with the same Registry configured, and you can see that they are in different formats.

If we set it manually, on the one hand, it is easy to get confused. For example, there is only a difference of “=” between NPM and YARN, but if we remember it incorrectly, the configuration will not take effect. On the other hand, Registry is a long string of urls, which is hard to remember with our head. Some people just keep a memo and copy it every time a new project comes along. That’s fine, but it would be much easier if we had tools to help us do that. Some people also think, since I only need to modify a Registry to speed up the installation of dependencies when there is no cache, then I can directly make all the projects in the computer use the same Registry, which is certainly possible. We put the configuration file in the root directory of the computer user folder to be effective for all projects.

However, in some scenarios, we need to use different Registries across projects. Such as some companies for security will build a network of private source of NPM, because the company cannot make some internal code release to the public for all can be installed, but we may need to take the computer home over the weekend, with some of their own projects, this time, the use of the source is not appropriate, and the company Intranet address does not necessarily can access at home.

There is also a scenario where different Registries need to be used across scopes. For example, the company has two lines of business, each with its own independent private source, and publishes packages under the scope of @foo and @bar respectively. We happen to do a public project, and need packages under the scope of these two scopes.

In order to facilitate the configuration of Registry for various package managers, I wrote this small tool called URM (Universal Registry Manager). It supports multiple front-end package managers (NPM, YARN, Berry) and is designed for multi-project and multi-scope scenarios. But before we begin to introduce this small tool, in order to let every small partner can better use it, I need to smooth some background knowledge with everyone. This includes:

  1. What is Registry? What is its role in installing dependencies?
  2. What is scope?
  3. How can the configuration of various package managers be modified? What is the priority of different modification methods?

If you already know it, you can go straight down to the introduction of URM. If you want to know more about it, you can go to the B station and watch this video I posted at 4:47-23:50.

The use of URM

Environmental requirements

Node.js v12 or later.

The installation

npm i -g @haochuan9421/urm
# or
yarn global add @haochuan9421/urm
Copy the code

Installation too slow? npm i -g @haochuan9421/urm –registry https://registry.nlark.com/

A diagram to understand URM usage

Detailed introduction

Package manager (NPM, YARN…) The registry configuration

Viewing the Current Configuration

View the Registry configuration for the package manager
urm current
# alias
urm cur
# current is the default command and can be omitted
urm
Copy the code

The new configuration

Add a new registry configuration as prompted
urm use
# alias
urm set
This command means to use the Taobao Registry when installing packages with scope "@vant" and save the configuration file to the current project
urm use taobao --scope @vant --where project
# shorthand
urm use taobao -s @vant -w project
Copy the code

Delete the configuration

  • A single delete
Select a Registry configuration to delete as prompted
urm unuse
# alias
urm unset
Copy the code

  • One-time emptying
Clear all deletable Registry configurations at once
urm clear
# Skip the confirmation prompt
urm clear -yes
# shorthand
urm clear -y
Copy the code

Switching working mode

  • Temporary switch
# Check the yarn Registry configuration
urm --mode yarn
# shorthand
urm -m yarn
Add a registry configuration to YARN v2 (Berry)
urm use -m yarn2
Delete the registry configuration for NPM
urm unuse -m npm
Copy the code
  • A permanent switch
Select a package manager as the default mode for URM as prompted.
By default, all operations work in default mode unless you specify another package manager by --mode
urm mode
Set the default working mode to YARN. Currently, the available values are NPM, YARN, and YARn2
urm mode yarn
Copy the code

A list of alternative Registry

Viewing the current list

The registry in the list can be selected when you perform urM use.
# With this list, it is greatly convenient to set up Registry without having to remember a long list of urls
urm list
# alias
urm ls
Copy the code

Add the registry

Enter the name and URL of Registry as prompted
urm list add
Skip the prompt and specify the name and URL of Registry directly
urm list add youzan https://npm.youzan.com/
Copy the code

Delete the registry

Select an item from the list as prompted
urm list del
# Skip the prompt and delete the registry whose name is Youzan directly from the list
urm list del youzan
Copy the code

Registry speed

# Test name is the access speed of The Taobao Registry by calculating the time of accessing '${registry}/-/ping' address
urm list ping taobao
All speed measurements
urm list ping
Copy the code

Reset the list

Restore the list to its default state. The default list provides several stable and fast registries for domestic access
urm list restore
Copy the code

Programmatic use

The local installation

npm i @haochuan9421/urm
# or
yarn add @haochuan9421/urm
Copy the code

The use of urm

const urm = require("@haochuan9421/urm");

(async() = > {// Get the urM-wrapped package Manager instance
  const npmConfig = urm.pmConfig.getPmConfig("npm"); // choices "npm", "yarn", "yarn2"
  // View the current Registry configuration of the package manager as an array
  const curRegistries = await npmConfig.getCurRegistries();
  // Set the Registry configuration for the package manager
  await npmConfig.setRegistry("@vant"."https://registry.nlark.com/", { whereType: "project" });
  // Delete the Registry configuration for the package manager
  await npmConfig.delRegistry("@vant", { whereType: "project" });
  // Clear all Registry configurations for the package manager
  awaitnpmConfig.clearRegistry(); }) ();Copy the code

For details, see the lib/pm-config folder. Different package managers implement different methods, but the interface is the same, which will help support other package managers later.

conclusion

Compared to other similar tools, URM has the following advantages:

  • Support for multiple front-end package managers, you do not need to install multiple tools to use different package managers.
  • More flexible, designed for scenarios across projects and scopes, rather than saving configuration files to the root of the user folder.
  • More reliable, managing configurations consistent with package manager source code. I developed URM after reading the related source code of NPM and YARN. Therefore, whether obtaining the current configuration, adding and deleting operations, they are basically consistent with the official.
  • Simple, easy to use, beautiful command line interface. Most of the operations are set up by memorizing a short command and selecting or answering a question.

In the development of URM, I also reference some similar tool, in this thank developer of these tools, I was standing on the shoulders giants, wrote a small tool, so I hope he can help to you front friend, friend if you think he can really bring help to your development, also might as well let us together to share with him more.