About

Lerna is a tool that optimizes the workflow for managing multiple package repositories using Git and NPM.

Vue, Babel, React, etc. Our company also uses it. The document is in English, I will briefly summarize a, I hope to help you.

Two modes of work

Fixed/Locked mode (default)

Json file “version”: “0.1.5”, which will be added according to lerna.json file. Select this number only once, and update the version of other packages automatically.

Independent mode

Lerna init — Independent initializes the project. Json file “version”: “independent”,

Each time you publish, you will get a prompt for each package that has changed to specify whether it is a patch, minor, major, or custom change.

Start init

    $ npm install lerna -g
    $ mkdir lerna-gp && cd The $_
    $ lerna init The default fixed mode is used by vue Babel, etc
    
     # Add packages
    $ cd packages
    $ mkdir daybyday gpnode gpwebpack
    ...
    Enter three directories respectively to initialize the package
    $ cd daybyday
    $ npm init -y 
    $ cd ../gpnode
    $ npm init -y
    $ cd ../gpwebpack
    $ npm init -y
Copy the code

The project structure

➜ rana-go git:(master) go onto university and go onto university. ├── download.├ ── download.├ ── download.├ ── download.├ ── go onto university and go onto university │ ├ ─ └─ garbage └─ exclude.txt (3 filesCopy the code

Set up

Set up git + npm

✗ git remote add origin [email protected]: gaopo/lerna - gp. Git# check whether to log in
✗ npm whoami
gp0320

If no, log in
npm login 
# Enter username password
Logged in as gp0320 on https://registry.npmjs.org/. # succeed
Copy the code

Set up YARN workspaces mode

The default is NPM, and each subpackage has its own node_modules, so that only the top layer has one node_modules

  • Modify the toppackage.json and lerna.json
# package.json file added
 "private": true."workspaces": [
    "packages/*"].# lerna.json file added
"useWorkspaces": true."npmClient": "yarn".Copy the code

Lerna Script

lerna create < name > [loc]

Create a package, name package name, loC location optional

Examples

# package.json in the root directory
 "workspaces": [
    "packages/*"."packages/@gp0320/*"].Gpnote is placed by default in workspaces[0]
lerna create gpnote 

Gpnote specifies packages/@gp0320 folder. Note that packages/@gp0320 must be written in workspaces
lerna create gpnote packages/@gp0320
Copy the code

lerna add [@version] [–dev] [–exact]

Add local or remote packages as dependencies within the current project Packages

  • --devDevDependencies alternativedependencies
  • --exactInstall the correct version, that is, the installed package version is not preceded by^, Eg: "^" 2.20.0 ➜ 2.20.0 ""

Examples

# 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
lerna add module-1 --scope=module-2

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

# Install module-1 in all modules except module-1
lerna add module-1

# Install babel-core in all modules
lerna add babel-core
Copy the code

lerna bootstrap

The default is NPM I, because we specified YARN, so,run YARN install, which installs all package dependencies to the root node_modules.

lerna list

List all packages. If they don’t match the one in your folder, go to that package and run YARN init -y to fix it

➜ lerna-gp git:(master) qualify lerna list lerna notice cli v3.14.1 daybyday gpnode gpnote gpwebpack lerna success found 4 packagesCopy the code

lerna import

Import a package that already exists locally

lerna run

lerna run < script > -- [..args] Run all commands with this script in the package
$ lerna run --scope my-component test
Copy the code

lerna exec

Run arbitrary commands in each package

$ lerna exec -- < command > [..args] # runs the command in all packages
$ lerna exec -- rm -rf ./node_modules
$ lerna exec -- protractor conf.js
lerna exec --scope my-component -- ls -la
Copy the code

lerna link

The project package establishes a soft link, similar to NPM Link

lerna clean

Delete the node_modules directory for all packages

lerna changed

List the packages that lerna publishes to update next release.

Git add and git commit Git diff –name-only v Not all packages are the same version as the Internet says.

➜ lerna-repo git:(master) qualify lerna changed info cli usinglocalVersion of Lerna Lerna Notice CLI v3.14.1 Lerna info LookingforChanged Packages since V0.1.4 daybyDayNext publish will upload only this one
lerna success found 1 package ready to publish

Copy the code

lerna publish

Can tag, upload Git, upload NPM. If your package name has scope e.g. “name”: “@gp0320/ gpWebpack “, that needs to be added in packes.json

 "publishConfig": {
    "access": "public"
  },
Copy the code
Lerna Publish LERna info Current version 0.1.4Find packages that have changed since V0.1.4
lerna info Looking forChanged packages since V0.1.4? Select a new version (currently 0.1.4) Patch (0.1.5) Changes: -daybyDay: 0.1.3 => 0.1.5# Only one change was made. Successfully published: - [email protected] lerna success published 1 packageCopy the code