This article will focus on why lerNA is used and how lerNA is used.
Why Lerna
First, the official explanation:
- Splitting up large codebases into separate independently versioned packages is extremely useful for code sharing. However, making changes across many repositories is messy and difficult to track, and testing across repositories becomes complicated very quickly.
- To solve these (and many other) problems, some projects will organize their codebases into multi-package repositories (sometimes called monorepos). Projects like Babel, React, Angular, Ember, Meteor, Jest, and many others develop all of their packages within a single repository.
- Lerna is a tool that optimizes the workflow around managing multi-package repositories with git and npm.
- Lerna can also reduce the time and space requirements for numerous copies of packages in development and build environments – normally a downside of dividing a project into many separate NPM packages. See the hoist documentation for details.
This means that some of the projects we develop are interdependent, and they are in different warehouses, which makes development, maintenance and testing very difficult. To solve these problems, some projects organize these interdependent projects into multiple packages in the same repository (commonly known as monorepos). Babel, React, Angular, Ember, Meteor, Jest, and so on are all maintained in the same repository. Lerna’s job is to use Git and NPM to optimize the management workflow of multi-package libraries.
How to use Lerna
Lerna’s model for managing projects
Lerna has two modes of managing projects:
-
Fixed/Locked mode (default) : All packages share one version number.
-
Independent mode: Specifies the — Independent parameter at initialization:
lerna init --independent
Copy the code
In this mode, you can specify a version number for each package individually.
Learn provides us with the following commands:
- lerna publish
- lerna version
- lerna bootstrap
- lerna list
- lerna changed
- lerna diff
- lerna exec
- lerna run
- lerna init
- lerna add
- lerna clean
- lerna import
- lerna link
- lerna create
- lerna info
Let’s use these commands in a concrete demo.
lerna init
Effect: Initializes the project
# Adopt standalone mode
$ mkdir lerna-example && cd The $_ && npm install lerna -D && npx lerna init --independent
Copy the code
The generated project directory is as follows:
lerna-example/
packages/
package.json
lerna.json
Copy the code
Lerna. json
{
"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: Version of the current project, “independent” string if in independent mode
- NpmClient: The default value is NPM and yarn can be used
- Command. The publish. IgnoreChanges: specify which document updates do not need to be released, such as modifying the README.
- Command-publish. message: Specifies a custom COMMIT message when a version is released
- Command. The publish. Registry: specify remote warehouse
- Command. The bootstrap. Ignore: perform lerna ignored when the bootstrap file
- Command. The bootstrap. NpmClientArgs: perform lerna bootstrap command to execute the NPM install this parameter is passed to the NPM install.
- Command. Bootstrap. Scope: Determines which packages need bootstrap when lerna bootstrap is executed
- Packages: Specifies where packages are stored
There is nothing under our packages yet, so let’s create some packages.
lerna create
Use lerna create to create a new package, or you can create it manually
$ lerna create animal
$ lerna create cat
$ lerna create dog
Copy the code
Here we create animal Cat Dog with three packages:
. ├ ─ ─ lerna. Json ├ ─ ─ package. The json └ ─ ─ packages ├ ─ ─ animal │ ├ ─ ─ the README. Md │ ├ ─ ─ __tests__ │ │ └ ─ ─ animal. The test. The js │ ├ ─ ─ Lib │ │ └ ─ ─ animal. Js │ └ ─ ─ package. The json ├ ─ ─ the cat │ ├ ─ ─ the README. Md │ ├ ─ ─ __tests__ │ │ └ ─ ─ the test. The js │ ├ ─ ─ lib │ │ └ ─ ─ The js │ └ ─ ─ package. Json └ ─ ─ dog ├ ─ ─ the README. Md ├ ─ ─ __tests__ │ └ ─ ─ t test. The js ├ ─ ─ lib │ └ ─ ─ t js └ ─ ─ package. The jsonCopy the code
lerna add
Use: learn Add to add local or remote packages as package dependencies
# add module-1 packages/prefix-* # add module-1 packages/prefix-* # add module-1 packages/prefix-* # $lerna add module-1 --scope=module-2 $lerna add module-1 --scope=module-2 --dev # add module-1 to peerDependencies $lerna add module-1 --scope=module-2 --peer # add module-1 to peerDependencies $lerna add $lerna add $lerna add $lerna add $lerna add babel-coreCopy the code
$lerna add animal as dog/cat dependencyCopy the code
If you look at dog and cat dependencies, you’ll see that animal is added.
lerna version
Role: Update the version of the package before publication (lerna publish includes this step)
Usage:
$lerna version 1.0.1Specify a specific version directly
$ lerna version patch # Use semantic keywords
$ lerna version # Select by interactive command
Copy the code
Lerna Version does this for us behind the scenes:
- Identify packages that have been updated since the last release;
- Prompt to select a new version;
- Modify the package metadata to reflect the latest release (change the package version number), run the lifecycle script in the root directory and in each package;
- Tag the submission;
- Push to the remote repository.
Commit local changes and run lerna version. By default, lerna version pushes local commits and tags to remote repositories. This is just a local demonstration that you can disable push by specifying –no-push.
$ lerna version --no-push
info cli using localVersion of Lerna Lerna notice CLI v4.0.0 Lerna info versioning independent Lerna info LookingforChanged packages since [email protected]? Select a new versionforCat (currently 1.0.0) (Use arrow keys) ❯ Patch (1.0.1) Minor (1.1.0) Major (2.0.0) Prepatch (1.0.1-alpha.0) Preminor (1.0-alpha.0) Premajor (2.0.0-alpha.0) Custom Prerelease Custom Version...Copy the code
lerna publish
Purpose: Publish local packages
Usage:
# release packages that have been updated since the last release.
$ lerna publish
Lerna version is used to tag packages with lerna version
$ lerna publish from-git
Release only versions that are not in the remote NPM repository. This applies to scenarios where lerna Publish did not publish all successfully.
$ lerna publish from-package
Copy the code
Verdaccio is used to build a local NPM warehouse in order to truly simulate the packet sending process
# global install
$ npm install -g verdaccio
$ verdaccio
Copy the code
Visit http://localhost:4873/ to see the startup screen.
-
Change the publishconfig.registry in each package package.json to http://localhost:4873/
-
Or create a.npmrc file at the root of the project, rewrite the NPM repository address to the local repository address: registry=”http://localhost:4873/”, and remove the publishConfig field from each package package.json
Add a line of console.log(“bark”) to dog.js, and when it’s ready commit the local changes, then execute:
$ lerna publish --no-push
info cli using localVersion of Lerna Lerna notice CLI v4.0.0 Lerna info versioning independent Lerna info LookingforChanged packages since [email protected]? Select a new versionforDog (currently 1.0.4) (Use arrow keys) ❯ Patch (1.0.5) Minor (1.1.0) Major (2.0.0) Prepatch (1.0.5-alpha.0) Preminor (1.0-alpha.0) Premajor (2.0.0-alpha.0) Custom Prerelease Custom Version...Copy the code
The command line will let us do version selection and validation, which will release our package to the NPM repository.
Check out http://localhost:4873/ to see the package we just released.
lerna list
Function: Lists all local packages
$ lerna list
info cli using localVersion of Lerna Lerna Notice CLI V4.0.0 Lerna info Versioning Independent Animal Cat Dog Lerna success found 3 packagesCopy the code
lerna info
Function: Displays local environment information
$ lerna info
info cli using localVersion of Lerna LERna notice CLI v4.0.0 LERna info Versioning independent Environment info: System: OS: MacOS 11.5.2 CPU: (8) x64 Intel(R) Core(TM) i7-7920HQ CPU @ 3.10GHz Binaries: Node: 14.16.0 - ~ /. NVM/versions/node/v14.16.0 / bin/node Yarn: 1.22.4 - ~ /. Yarn/bin/Yarn NPM: 6.14.11 - ~ /. NVM/versions/node/v14.16.0 / bin/NPM Utilities: Git: 2.24.3 - / usr/bin/Git npmPackages: lerna: ^ 4.0.0 = > 4.0.0Copy the code
lerna changed
Effect: Lists packages that have changed since the last release
To modify the file, run the following command:
$ lerna changed
info cli using localVersion of LERna LERna Notice CLI V4.0.0 LERna info Versioning independent LERna info All packages changed animal cat dog lerna success found 3 packages ready to publishCopy the code
lerna clean
Action: Removes node_modules from all packages except the node_modules from the root directory
$ lerna clean
info cli using localVersion of Lerna Lerna Notice CLI V4.0.0 Lerna info Versioning independent Lerna info Removing the following directories: lerna info clean packages/animal/node_modules lerna info clean packages/cat/node_modules lerna info clean packages/dog/node_modules ? Proceed? Yes ...Copy the code
lerna bootstrap
What it does: Establish soft connections between interdependent packages and install other dependencies
This command performs the following steps behind it:
- Install external dependencies for all packages
- Establish dependencies between packages that have dependencies
- NPM run prepublish in BootStrapped packages (with –ignore-prepublish not specified)
- Execute NPM run Prepare in bootStrapped packages
$ lerna bootstrap
info cli using localVersion of LERna Lerna Notice CLI V4.0.0 LERna info Versioning independent LERna info Bootstrapping 3 Packages Lerna info Symlinking packages and binaries lerna success Bootstrapped 3 packagesCopy the code
lerna diff
Effect: Lists the changes made to the package since the last release.
Usage:
List all package changes
$ lerna diff
List changes for a specific package
$ lerna diff package-name
Copy the code
lerna exec
Function: Use lerna exec to execute arbitrary commands in all package directories.
Usage:
$ lerna exec -- <command> [..args] # runs the command in all packages
$ lerna exec -- rm -rf ./node_modules # Remove node_modules from all packages
Copy the code
lerna run
Use lerna run to execute scripts in all package directories.
Usage:
$ lerna run test Execute test for all packages
Copy the code
lerna import
What it does: Import an existing project as a package into an existing Lerna project, along with the project’s COMMIT history.
Usage:
$ lerna import <path-to-external-repository>
Copy the code
lerna link
Role: To establish soft connections between packages that have dependencies
Usage:
$ lerna link
Copy the code
conclusion
This article has made some simple explanations on lerna’s practice process of managing multi-package projects. It should not be a problem to get the basic introduction, and the rest is free to play 😊.