Friends I m working on…

2 minute read

Friends - I’m working on some Angular schematics and I’d like them to work in both Angular and Nx projects. Does anyone have a good example of this? I’m looking at some decent ways to get angular.json / workspace.json and for example figure out what the default app is. Any pointers are very welcome :)

Responses:

I don’t believe you need to do anything special to make them work in both since nx uses ng under the hood.

Well, not in all cases. If you generate a project that has workspace.json, the ng cli won’t work

in regards to finding the default project, you can read the file in the use JSON.parse(<fileData>) which gives you the file then just traverse it to the defaultProject prop

I know, I am saying that when you use nx to run schematics it will run angular schematics fine.

so if you build an angular schematics it will be usable by people using either nx or ng

*there might gotchas with this, I do not have a huge amount of experience with it, but that is my understanding.

There are gotcha’s, that’s why I’m curious to see some prior art

For example, nx does not work with ng-add (as far as I know)

I’ve built a bunch of schematics and keep repeating myself with this stuff, so I wondered if there are some shortcuts I could use

For instance, I found addDepsToPackageJson the other day, which is a super sweet shortcut for something I kept on doing manually. I figured there might be similar stuff for common things like figuring out the default app, or not having to wrap around workspace.json / angular.json myself

Just looked in the repo and in some migration scripts they are doing this, if there was a shortcut I would think they would be using it.

Right

So one ends up with a check for angular.json and one for workspace.json, then we need to handle if any, none or both exists. Which is kinda clumsy to keep repeating

Thanks for checking btw, I appreciate the input :slightly_smiling_face:

I think it would be safe to do

if angular.json

else if workspace

ele throw error

No problem! I remember when i was figuring out how to write the nrwl/nest:lib schematic and it was a lot to learn all at once hahah

:grin:

For me the benefit of nest is that I’m fine with running it on nx-only, so in that case I stopped caring about angular.json

It’s different for an Angular UI lib, it’s cool if it also works in plain Angular CLI apps

Guess I found at least one helper :slightly_smiling_face: https://github.com/nrwl/nx/blob/b55a1a96dd74fc1ecc209eb4f42959933102bd75/packages/workspace/src/utils/cli-config-utils.ts#L5-L8

there you go!

exactly what you were looking for

Yep! Thanks again!

I’m super happy that I found this slack btw! So much more useful than the Nx Gitter

Updated: