nx
nx copied to clipboard
Allow nx affected to run with a whitelist --only
- [x] I'd be willing to implement this feature (contributing guide)
Description
Allow nx affected to only execute targets on a subset of projects.
Motivation
Let's say you have an app that requires special setup to run its tests. Like maybe a database. Or one app requires it be run inside a certain container. Or many other things.
In your CI, you don't want to perform this setup for all projects. So you create one job for your pure JS projects that runs:
nx affected --target test --exclude my-server-app
And another job that runs:
bash do-special-setup.sh
npx nx affected:apps --plain | grep -q 'my-server-app' && npx nx test my-server-app || echo 'apps/my-server-app is unaffected'
Which works but sucks because:
- The
grepis brittle, and would match onmy-server-app-e2e. You could improve that with some regex but this gets ugly fast. - grep will exit with a non zero code if it fails to match, which would fail CI when everything is fine. So you have to remember to add
|| somethingto catch that.
or
bash do-special-setup.sh
npx nx affected test --exclude=app-a,app-b,lib-c,lib-d
Which works but sucks because:
- When adding new apps or libs you'd have to add them here as well. And it's not a place you would think to look.
Suggested Implementation
This could be vastly simplified by adding an --only option to nx affected which sets up a whitelist of projects to run the target on.
npx nx affected --target test --only my-server-app
This compliments the --exclude option (a blacklist) nicely, IMHO.
Alternate Implementations
Maybe add a --if-affected to the command runner?
nx test my-server-app --if-affected
I don't like this as much because it executing targets on only affected projects is the domain and responsibility of nx affected.