I m writing a Nx builder…

2 minute read

I’m writing a Nx builder for my project, and I’d essentially like to just wrap @nrwl/node:build with some extra functionality. @angular-devkit/schematics has a handy externalSchematic() function for running other schematics.

Does anyone know if there is a similar thing for executing one builder from another?


curious about this, what are you trying to wrap?

We do some swagger codegen at build time. Generating interfaces from our Open api spec. I basically just want to be able to run that before every time build is run

cool, thx

I’m thinking it’s possible to use the run commands. But I think I’d rather have it packaged up as it’s own builder if it’s easy enough

context_.scheduleBuilder_() should do the job if I remember correctly

btw which lib are you using to generate interfaces from the Swagger api?

once you get this worked out, it would be an awesome nx-plugin

I’m currently using the swagger-codegen project

It kinda sucks since it requires java on the users system. Not an issue at my workplace since we have java in our stack anyway

I’ll check out context.scheduleBuilder() , thank you

are there any tutorials on writing custom builders?

this guide will get you started: https://nx.dev/angular/guides/nx-plugin

nx plugins can consist of builders and/or schematics

using create-nx-plugin will scaffold out a new nx workspace with some of the default files needed to get started creating a builder, you can also alternatively create nx plugins inside an existing nx workspace

it has some helpful defaults like baking in an e2e architect

perfect, thanks a lot

I’m checking out context.scheduleBuilder but getting cannot read property 'sourceRoot' of undefined Does this look about right to you?

sorry if that’s getting a little too “in the weeds”

:thinking_face: hard to tell from this. But yeah. I remember having it used a while back. BAsically there is scheduleBuilder and scheduleTarget on the context object

but I’d have to take a look at the entire source to see why it doesn’t work in ur case :slightly_smiling_face:

ok, thanks for taking a peek

what might help as well: go search through the Angular CLI repo whether they use it as well somewhere: https://github.com/angular/angular-cli/search?q=scheduleBuilder&unscoped_q=scheduleBuilder

I have to say, I’ve only been hanging around this community for a couple weeks maybe. and everyone here is super responsive and helpful. I really appreciate it, as do others I’m sure

In case anyone else is interested. When calling context.scheduleBuild it appears you need to pass context.target as an option. It is not passed to the builder by default const res = await context.scheduleBuilder('@nrwl/node:build', schema, { target: context.target });