monist-tools
v2.0.0
Published
A lightweight tool for managing monorepos.
Readme
Monist-tools is a lightweight tool for managing monorepos.
Monist-tools is derived from monist. Monist itself is being retired in favor of npm workspaces. Since this code base was forked from monist it shares history with it and the first version of monist-tools is not 1.0.0 but version 2.0.0.
Motivation
The npm developers did not think through the addition of workspaces. In
particular doing a version update on workspaced packages does not change the
version number in depedencies or devDependencies, etc.
See:
- https://github.com/npm/cli/issues/3403
- https://github.com/npm/cli/issues/3885
Terminology
This documentation uses a terminology that distinguishes types of x as "local x" and "monorepo x". The basic distinction is:
local package: a package which is a part of the monorepo. These packages are in the
workspacessetting of the top-level package. (The code of monist refers to these as "monorepo members".)monorepo package (aka "top-level package"): the package modeled by a
package.jsonappearing at the top of the monorepo. This package is never published to an npm repository.
Then we derive other terms from this basic distinction:
- a "local
package.json" is apackage.jsonwhich belongs to a local package, whereas a "monorepopackage.json" is the top-level one.
Requirements
Monist requires that your monorepo conforms to some constraints:
You must have a correctly defined
workspacesin yourpackage.jsonfile.You must have a monorepo
package.jsonfile which is not meant for publication. You should setpublic: falsein this file.All package versions are in lockstep. If you have packages A and B in your monorepo, then when when A reaches version 2.3.1 then B also reaches the same version.
Publishing one package entails publishing all publishable packages, even if some packages did not change. (Note that monist itself does not publish packages so you could write a publication script that publishes only a subset of packages but your published packages could then refer to those packages you did not publish and would not be installable.)
Usage
You invoke monist with monist and then as first argument pass a monist
command. Here is a brief descriptions of the command monist offers. Please use
monist-tools [cmd] --help to get a more comprehensive description of what
the commands can do.
monist-tools update-versionsupdates version numbers in thepackage.jsonfiles for all packages, including the monorepo package. Note that this command is not meant to replacenpm version. It is a command you'd use in yourpreversionscript to update version numbers. This command verifies versions prior to running likemonist verify-depsdoes.monist-tools set-scriptis utility allowing you to quickly add a script to all local packages'package.json. It does NOT touch the monorepopackage.json.monist-tools verify-depsis a utility that checks whether the dependencies in your monorepopackage.jsonand the local packages are in a sane state.
Usage Examples
Here are examples of scripts in a monorepo package.json:
"scripts": {
"postversion": "monist-tools update-versions $npm_package_version && git add package.json package-lock.json packages/*/package.json && git commit -m'build: version bump' && git tag -a v$npm_package_version && npm run build-and-test && npm run self:publish"
}(This monorepo has a .nprmc which turns off automatic git manipulation when
issuing npm version. This is why there are git commands in the
postversion script.)
Dependency Verification Rules
Dependencies can exist both for the monorepo package and the local
packages. However, not all dependency usages make sense when using a
monorepo. monist verify-deps and monist update-versions perform the
following checks:
The monorepo
package.jsonmay contain onlydevDependencies. This package is never published. Consequently, the other types of dependencies supported bypackage.jsondo not make sense there.The
devDependenciesin a localpackage.jsonmay only contain local packages. Development dependencies for everything else belong to the monorepopackage.json.All dependencies other than
devDependenciesin a localpackage.jsonmust have a corresponding entry in the monorepopackage.json, and the entry there must have the same version number as the entry in the localpackage.json.
