Lerna is used to manage projects with multiple packages that may refer to each other.

Lerna manages the version numbers of subprojects in two ways

  • Fixed/Locked mode (default) : This mode is executed every timelerna publishWill upgrade the packages involved to the latest version, and the developer just needs to make sure to release the next oneversion.
  • Independent mode: managed by the developerversion, each timelerna publishYou need to determine the next version number for each package.

The basic use

The following command is based on YARN.

Install the installation

yarn global add lerna

Init Initializes the project

After the lerna init command is executed, the corresponding directory structure is generated.

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

Lerna. Json configuration

{
  "version": 1.1.3 "".// Project version
  "npmClient": "npm".// The default value is NPM. You can change it to yarn
  "command": { // Lerna built-in command configuration
    "publish": {
      "ignoreChanges": ["*.md"."**/test/**",].// Ignore changes to some files when publishing. This configuration can reduce unnecessary Publish.
      "message": "chore(release): publish" // git commit message}},"packages": ["packages/*"]}Copy the code

Create creates a subproject

Lerna create

creates a subproject and generates the corresponding package.json as prompted by the interaction

Add Adds dependencies

lerna add <package>[@version] [--dev] [--exact]

  • lerna add eslint: All bags will be loadedeslint.
  • lerna add eslint --scope=package1: onlypackage1Will be installed.
  • lerna add eslint packages/prefix-*: in accordance withprefixThe bag will be loaded.

options:

  • -devAdded:devDependencies
  • --exact: Installs only a specific version

If the subproject is added, it will be soft-linked to the corresponding project via link. lerna add package1 –scope=package2

Run Run the NPM script command

lerna run <script> -- [..args]

  • lerna run test: will execute all subprojectstest.
  • lerna run --scope package1 test: only performpackage1In thetest.
  • lerna run --ignore package-* test: Only matches are executedpackage-*Outside of the projecttest

Exec Executes any command

lerna exec -- <command> [..args]

Similar to lerna run, except it can execute any command. eg: lerna exec — rm -rf ./node_modules

Other commands

  • lerna bootstrap: Installs dependencies of sub-projects, soft links projects that reference each other, and executes them in sub-projectsnpm run prepublishandnpm run prepare
    • --hoist [glob]: will match dependencies of subitems (eg:eslint.jestEtc.), unified in the root directorynode_modulesTo reduce installation time, but onlynpmClient=npm
    • - nohoist [glob]: Match dependencies (eg:babel) is installed in a subprojectnode_modulesIn the
  • lerna clean: to delete a subitemnode_modules
  • lerna linkWith:bootstrapThe second step.

Use the advanced

Lerna-changelog

Lerna-changelog generates Changelog for a project based on PR

Refer to repo

Using the step
  • frommasterBranch outfeature/bugfixEqual branches, referencegit-flow.
  • After completion of developmentcommit, recommended to usecommitizenTo regulatecommit msg, while helping to generate subsequent subprojectschangelog.
  • The new branchpushtoremoteEnd.
  • createprAnd play onlabelBe sure to type it herelabel.learn-changelogIs based onlabelTo determine theprBelong tofeature/bugfix/documentAnd so on.
  • Remember to inmergeBefore play onlabel.
  • formerge prOperation.
  • Local switch tomasterBranch and proceedpullOperation.
  • performlerna-changelog, can get a sharechangeling.
Pay attention to

The label of pr cannot be set arbitrarily, it must be declared in the project to take effect.

Official default support product/enhancement/bug/documentation/internal, need in the package. If you want to use other json accordingly in the configuration.

{
  "changelog": {
    "labels": {
      "feat": ":rocket: New Feature"."bug": ":bug: Bug Fix"."doc": ":memo: Documentation"."internal": ":house: Internal"."breaking": ":boom: Breaking Change"}}}Copy the code

Subproject changelog

This has not yet been done, so refer to README for details

conclusion

The use of LERNA has been introduced, the above content can meet the daily development needs, more details need to refer to the official documents.