Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Share Build Scripts In Lerna Packages

I have a lerna repo that contains multiple packages organised in the usual structure:

package.json
/packages
  - alpha
    package.json
  - bravo
    package.json
  - charlie
    package.json

I need to transpile all packages, and I currently have the following scripts in each package's package.json:

"build": "npm run build:noWatch -- --watch --verbose",
"build:noWatch": "babel src --out-dir lib --root-mode upward --ignore '**/*.test.js','**/__tests__'",
"prebuild": "rimraf lib/*"

I currently run a build using:

lerna run build --stream --parallel

However I don't want to duplicate these scripts for every package. I would like to define the scripts in a single place, but use them from all packages. I currently have linting scripts and testing scripts in my root package which make sense there as they are effectively traversing the whole monorepo looking for tests, or files to lint. It doesn't seeem to make sense to move the build scripts up there as well as they are scoped to the individual packages, and I like the fact that I get different colour output for each package when I use lerna run.

An unsatisfying solution is to create some shell scripts in the root of the monorepo and call them from the packages' package.json files:

In root/packages/example/package.json:

"scripts": {
  "build": "../../scripts/build.sh",
  "build:noWatch": "../../scripts/build.sh",
  "prebuild": "../../scripts/prebuild.sh"
},

Then in root/scripts/build.sh:

#!/bin/sh 

babel src --out-dir lib --root-mode upward --ignore '**/*.test.js','**/__tests__' --watch --verbose 

Whilst this works, it doesn't feel right: it still involves duplication between the packages and requires setting permissions on the shell scripts (which complicates CI).

Is there a better way to share these commands amongst all my packages?

like image 385
Undistraction Avatar asked Dec 03 '18 19:12

Undistraction


People also ask

What does lerna run build do?

The Original Tool forJavaScript Monorepos Lerna is a fast modern build system for managing and publishing multiple JavaScript/TypeScript packages from the same repository.

Does lerna work with npm?

Lerna is a popular and widely used tool written in JavaScript for setting and managing multi-package repositories for Node. js projects with npm and Git. Lerna has two modes: fixed and independent.

How do I add packages to Lerna?

lerna create is a command to add a new package to be managed in Monorepo. To install an npm package, we can use the yarn workspace command in the root directory or the yarn add package-name command in the package directory where you'd like to install the package. Yarn updates package. json for the package and yarn.


1 Answers

Package all the build scripts into their own module and then use lerna --hoist to host the common module so it is installed once but available to all the other packages.

like image 149
Old Pro Avatar answered Oct 05 '22 23:10

Old Pro