What is Lerna ?

Lerna is a multi-package management tool. It was originally designed to solve the problem of cross-library debugging. Later, more execution commands were derived to facilitate debugging and management.

According to the lerna file organization structure, all package packages are stored in the Packages directory, external only package.json configuration file can be retained. The Package package is a complete NPM project structure.

File organization structure

my-lerna-repo/
  package.json
  packages/
    package-1/
      package.json
    package-2/
      package.json
Copy the code

Commonly used key commands

Initialize or upgrade the project (init)

Create a Lerna project or upgrade an existing Git repository to lerna:

lerna init
Copy the code

Lerna will do two things:

  • inpackage.jsonIn thedevDependencyJoin lerna.
  • createlerna.jsonConfiguration file that stores the current LERna projectThe version number

Create a new package

lerna create newPackageName
Copy the code

Install dependencies (Bootstrap)

All packages have dependencies installed in their own directories, that is, all packages have node_modules directories corresponding to their package.json, as well as symlink individual packages and binary packages

Lerna bootstrap This command does the following: 1. 'NPM install' all external dependencies for each package; 2. Link all 'packages' interdependent Lerna' together; 3. Run 'NPM run prepublish' in all installed packages; 4. Run 'NPM run prepare' in all installed packages.
##### --ignoreTo ignore some dependencies, you can specify the command '--ignore' item, and of course you can configure it in 'lerna.json' to be more generic.
#### Add dependencies

``` bash
lerna add <package>[@version] [--dev] [--exact] [--peer]
Copy the code

The add operation can use all filter labels

# Adds the module-1 package to the packages in the 'prefix-' prefixed folders
lerna add module-1 packages/prefix-*

# Install module-1 to module-2 in devDependencies
lerna add module-1 --scope=module-2 --dev

# Install module-1 to module-2 in peerDependencies
lerna add module-1 --scope=module-2 --peer
Copy the code

Promotion installation depends on the reactive power

Each package has its own package.json, and node_modules directories are generated in each package when dependencies are installed, and often these packages have many duplicate dependencies. The reactor-based solution is to promote the dependencies to the topmost node_modules directory, thereby reducing the extra time and space involved in developing and building replica packages.

–hoist [glob]

This configuration item installs dependencies to the root directory and can specify a glob to avoid implementation in all packages. Any binaries in dependencies will be linked to the node_modules/.bin/ directory, which defaults to ** if glob is not specified (install all dependencies to the root directory).

–nohoist [glob]
lerna bootstrap --hoist --nohoist = babel- *
Copy the code

Hoist dependencies can be specified

lerna bootstrap --hoist
Copy the code
{
  "version": "0.0.0"."command": {
    "bootstrap": {
      "ignore": "component-*"}}}Copy the code

Run the script for each package

Run the lerna run command to execute the script of the same name in each package.json package:

Lerna run start # Execute all scripts containing startCopy the code

packages/xxx-pk1/package.json


{
  "scripts": {"start": "some start command..."}}Copy the code

packages/xxx-pk2/package.json

{
  "scripts": {"start": "some start command..."}}Copy the code

Start in both XXX-PK1 and XXX-PK2 will be executed.

Specify filter flags

Filter packets that do not need to be executed by filtering flags.

Lerna run start --scope xxx-pk1 #Copy the code

Note here that xxX-pk1 must match the name field in package.json, otherwise it will run incorrectly in the current version.

Parallel execution flags:--parallel

If some packages take longer to execute, specify — Parallel uses Child processes to process each package in parallel.

lerna run start --scope xxx-pk1 --scope xxx-pk2 --parallel
Copy the code

It is recommended that you use the –parallel flag in conjunction with the –scope flag to limit the scope, as too many child processes can degrade shell performance.

Lerna package version management

Git push is implemented with the version command:

Lerna version 1.0.1 # explicit lerna version patch # semver keyword lerna version # select from prompt(s)Copy the code

Here Lerna will do a couple of things:

  1. Verify that all packages are up to dategitTo confirm;
  2. Pop-up version selection prompt box, to specify the version number;
  3. Modify the package metadata to reflect the new version, and inrootPackage and eachpackagePackage to run the appropriateLifecycle script;
  4. CommitThose that have changedpackage.jsonFile and type the versiontag;
  5. PushgitRemote warehouse;

Before running the version command, you need to perform a Git commit to record the change. If not, the version command does not consider that a new version needs to be published this time, with the –force-publish exception.

Force all version numbers to change:--force-publish

Lerna version --force-publish = package-2, package-4Copy the code

Skip Commit/Tag:--no-git-tag-version

By default, package.json files with modified version numbers are submitted with version tags.

Skip Push:--no-push

Git push is done by default. –no-push disables git push:

lerna version --no-push
Copy the code

There are two modes: locked vs. independent

Lerna project has two modes: Fixed/Locked mode (the default) | Independent mode, and their corresponding package version management in two ways.

Fixed/Locked mode (default)

This mode synchronizes all package versions with a major version number, which is the version in lerna.json

{
  "version": "0.0.0"
}
Copy the code

Independent mode

In this mode, each package can maintain its own version number independently. To specify the independent running mode, specify the version configuration in lerna.json as follows:

{
  "version": "independent"
}
Copy the code

Optimized configuration in CI environment

–amend

Normally, the version command generates a new commit to record the change in package.json, incorporating the change into the current commit with the –amend option and tagging it, while ignoring the push operation.

lerna version --amend
# commit message is retained, and `git push` is skipped.
Copy the code

–yes

If the confirmation dialog box is cancelled, all operations pass by default.

Lerna package Publish

Lerna publish from-git # explicitly publish packages tagged in the current commit LERna publish from-package # explicitly publish packages where the latest version is not present in the registryCopy the code

Lerna never publishes packages marked as private (“private”: true in the package.json)

Publish configuration for each package

Each package changes some of the behavior of publish by specifying the package.json publishConfig field

access

Packages by scope by default (e.g. @my-scope/app) are restricted and require public access.

{
    "publishConfig": {
    "access": "public"}}Copy the code
registry

Specify the release repository for the package.

{
"publishConfig": {
    "registry": "http://my-awesome-registry.com/"}}Copy the code

Lerna. Json configuration

{
  "version": 1.1.3 ""."npmClient": "npm"."command": {
    "publish": {
      "ignoreChanges": ["ignored-file"."*.md"]."message": "chore(release): publish"."registry": "https://npm.pkg.github.com"
    },
    "bootstrap": {
      "ignore": "component-*"."npmClientArgs": ["--no-package-lock"]}},"packages": ["packages/*"]}Copy the code
  • Version: Indicates the version of the current project.

  • NpmClient: specifies the installation client. The installation mode is NPM Install. The YARN agent can be used to improve the installation speed.

  • Command. The publish. IgnoreChanges: specify a set of globs array, when released, ignore to bags;

  • Command. Publish. Message: The conference generates or overwrites (if there is –amend option) a COMMIT, where you can customize the commit message;

  • Command-publish. registry: specifies the publish repository;

  • Command. Bootstrap. ignore: Specifies a globs array to ignore dependent installations;

  • Command. The bootstrap. NpmClientArgs: : can give NPM install passing variables;

  • Packages: Specifies a set of globs arrays to determine the location of packages. Default: [“packages/*”];