'Scoping Lerna Exec to a specific folder

I'm using Lerna whilst building a component library using a mono-repo structure. I'd like to create a few new files in a large number of packages, but crucially, not EVERY package.

Ordinary I would run:

lerna exec -- touch docs/readme.md

or something similar. However I'd like to scope this command to just packages in a sub-directory e.g - packages/molecules.

Any ideas?



Solution 1:[1]

You can use filter options --scope which accept all filter flags

--scope Include only packages with names matching the given glob.

E.g.

Create readme.md file only in packages/pkg-a/docs directory:

?  npx lerna exec --scope pkg-a -- touch docs/readme.md
lerna notice cli v3.22.1
lerna notice filter including "pkg-a"
lerna info filter [ 'pkg-a' ]
lerna info Executing command in 1 package: "touch docs/readme.md"
lerna success exec Executed command in 1 package: "touch docs/readme.md"

lerna version:

?  npx lerna -v                                        
3.22.1

Solution 2:[2]

In a shell:

# This returns all packages with directory and package name
lerna list --all --parseable --long

# We then grep for your directory name
lerna list --all --parseable --long | grep packages/molecules

# We get just the package name
lerna list --all --parseable --long | grep packages/molecules | cut -d ':' -f 2

# We add the --scope flag to each package name and put it all in one line
lerna list --all --parseable --long | grep packages/molecules | cut -d ':' -f 2 | sed 's/^/--scope=/' | xargs

# We now pass all of above into `lerna exec`
lerna exec -- touch docs/readme.md $(yarn lerna list --all --parseable --long | grep packages/molecules | cut -d ':' -f 2 | sed 's/^/--scope=/' | xargs)

References:

  1. list
  2. exec
  3. filters

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 slideshowp2
Solution 2 Pants