Skip to content

Instantly share code, notes, and snippets.

@fedecarg
Last active February 4, 2020 12:49
Show Gist options
  • Save fedecarg/f10fc9c017de650a22b1e8d4042158aa to your computer and use it in GitHub Desktop.
Save fedecarg/f10fc9c017de650a22b1e8d4042158aa to your computer and use it in GitHub Desktop.

Using Lerna to Lint, Test and Copy NPM packages

Lerna allows you to list local packages that have changed since the last release:

$ cd /path/to/node-app
$ npx lerna changed -p

lerna notice cli v3.20.2
lerna info versioning independent
lerna info Looking for changed packages since @fedecarg/template@4.0.17
/path/to/node-app/packages/analytics
/path/to/node-app/packages/breaking-news
/path/to/node-app/packages/footer
/path/to/node-app/packages/header
/path/to/node-app/packages/template
/path/to/node-app/packages/user-account
lerna success found 6 packages ready to publish

You can use a combination of lerna changed and lerna exec to lint and test only the packages that changed.

For example:

Lint:

$ npx lerna exec $(for package_dir in `npx lerna changed -p`; do echo -n " --scope @fedecarg/$(basename ${package_dir})"; done) -- yarn lint

Test:

$ npx lerna exec $(for package_dir in `npx lerna changed -p`; do echo -n " --scope @fedecarg/$(basename ${package_dir})"; done) -- npm test -- --no-watch --passWithNoTests

You can also use Lerna to build packages that changed in node-app and copy them to any consuming app:

Copy:

  1. Change directory to node-app:
$ cd /path/to/node-app
  1. Commit all the changed files and push to remote:
git add .
git commit -m "Commit message"
git push origin <branch>
  1. Set the destination path to the consuming app:
$ DESTINATION_PATH=/path/to/consuming_app/node_modules/@fedecarg
  1. Build and copy packages:
$ for package_dir in `npx lerna changed -p`; do \
  yarn --cwd ${package_dir} build && \
  rm -Rf "${DESTINATION_PATH}/$(basename ${package_dir})" && \
  cp -R ${package_dir} ${DESTINATION_PATH} && \
  echo "Copied ${package_dir}..."; \
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment