Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass args into lerna exec command

Tags:

npm

yarnpkg

lerna

Goal

I have yarn test, which is actually composed of two subcommands yarn test:root && yarn test:packages. Both run jest (but packages does it indirectly using lerna exec). I want to be able to type yarn test -t=Pattern from the terminal and have both sub-commands append -t=Pattern to the end. lerna exec -- "yarn test" doesnt seem to have a way to do this.

Background

I have a monorepo, that uses lerna exec to run yarn test on each lerna package.

Given:

"test": "yarn run test:packages $@ && yarn run test:root $@",
"test:packages": "lerna exec -- yarn test $@", // No args passed
"test:root": "jest ./tests/Storyshots.jest.js $@", // Args passed

I want to be able to do something like

yarn test --updateSnapshot and for --updateSnapshot to be appended to yarn test run through lerna exec

With a regular npm script (see test:root) using $@ works fine. The lerna docs don't mention any way to do this.

Update

I think the easiest way will be to write a script which composes the args and the commands. This will need to be used in all lerna packages.

like image 836
Ashley Coolman Avatar asked Jan 29 '23 21:01

Ashley Coolman


2 Answers

You are able to pass command line args to inner commands by using --. Using -- will signify the end of the options for the current command and allow options to get passed to the inner commands.

So for this situations we need to escape three times:

test:packages

  1. yarn
  2. yarn run test:packages
  3. lerna exec -- yarn test

test:root

  1. yarn
  2. yarn run test:root
  3. jest ./tests/Storyshots.jest.js

yarn test -- -- -- -t=Pattern

like image 52
jjbskir Avatar answered Feb 27 '23 16:02

jjbskir


To append args, you just use something like lerna exec 'yarn test --updateSnapshot'

like image 28
xuanyue202 Avatar answered Feb 27 '23 15:02

xuanyue202