Lerna: Use lerna exec to run a command in the root scope

Created on 19 Aug 2019  ยท  3Comments  ยท  Source: lerna/lerna

(question)
Is there a way to use lerna exec/run to run a specific script/command in the root scope?

Expected Behaviour

We already use something like lerna run --scope=a <script>, but we'd like to be able to use lerna to execute a root script from a package script. Given a structure like:

.
โ”œโ”€โ”€ lerna.json
โ”œโ”€โ”€ package-lock.json
โ”œโ”€โ”€ package.json
โ”œโ”€โ”€ packages
โ”‚ย ย  โ”œโ”€โ”€ a
โ”‚ย ย  โ””โ”€โ”€ b
โ””โ”€โ”€ ...

Is there a way that a script "prestart" in packages/a/package.json can use lerna to run a script "xyz" which is in root package.json.

Something like:

// package.json
"xyz": "do-something-in-root-scope"
// packages/a/package.json
"prestart": "npx lerna run --scope <root> xyz"

Current Behaviour

According to this comment, it's a bad idea to add the root as a managed package.

Unsure how to proceed. Any help would be appreciated! Thank you :)

lerna.json


{
  "packages": [
    "packages/*"
  ],
  "version": "0.0.0"
}

Context


Your Environment

| Executable | Version |
| ---: | :--- |
| lerna --version | 3.16.4 |
| npm --version | 6.10.2 |
| yarn --version | N/A |
| node --version | 12.8.1 |

| OS | Version |
| --- | --- |
| macOS Mojave | 10.14.5 |

Most helpful comment

it relies on assuming a directory structure

I mean, yes? Like, your packages don't have a habit of changing their location in the directory tree often, do they?

if lerna kept track of the root path

$LERNA_ROOT_PATH is available to lerna exec.

All 3 comments

This seems more than sufficient for your use case:

    "prestart": "cd ../.. && npm run xyz"

@evocateur I strongly disagree that this is sufficient, as it relies on assuming a directory structure and the point is to keep the scope. if lerna kept track of the root path, that would be more stable and useful. Please reopen this.

it relies on assuming a directory structure

I mean, yes? Like, your packages don't have a habit of changing their location in the directory tree often, do they?

if lerna kept track of the root path

$LERNA_ROOT_PATH is available to lerna exec.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jcousins-ynap picture jcousins-ynap  ยท  3Comments

kirill-konshin picture kirill-konshin  ยท  3Comments

pcowgill picture pcowgill  ยท  3Comments

dylang picture dylang  ยท  3Comments

Donato-EF picture Donato-EF  ยท  3Comments