lucasvieirasilva / nx-plugins

NX Plugins
MIT License
99 stars 15 forks source link

Add a `serve` target #210

Open jafaircl opened 4 months ago

jafaircl commented 4 months ago

(I would definitely be willing to help implement this feature. But, there are a couple of things I'm not sure how to accomplish)

Description

For local development, it would be very helpful to have a "watch" mode. Other plugins generally name this target serve.

Motivation

Being able to run a script, such as a REST API, with a single command from the workspace root and have it re-run when file contents change would speed up development in workspaces using this plugin.

Suggested Implementation

I'm able to manually add a serve target that uses nodemon to the generated project.json like so:

"serve": {
    "executor": "@nxlv/python:run-commands",
    "options": {
        "command": "npx nodemon -e py --exec \"poetry run python my_project/__init__.py\"",
        "cwd": "my-project"
    }
}

I also modified __init__.py to look like this:

"""Automatically generated by Nx."""

from hello import hello

if __name__ == "__main__":
    print(hello())

But, I'm not sure that would also watch library code. I'm also not sure if nodemon is the right tool for this job or not. Since it's able to run with npx, it would prevent users from having to install additional tooling.

Alternate Implementations

A "more correct" approach would probably be to use watchmedo or something similar. nodemon has worked in my testing. But, that may not be true for everyone.

lucasvieirasilva commented 4 months ago

Hey @jafaircl, thanks for raising this request, I believe if you are using FastAPI you can use the fastapi dev CLI or this tool https://www.uvicorn.org/,

By using the @nxlv/python:run-commands you can achieve that, however, it would be nice to have an executor dedicated to this, the same as Nx does for many platforms, like next.js, react, angular and etc.

I think it's definitely worth the investigation, in the meantime could you explore a little bit on your side if uvicorn or fastapi CLI works for your needs, please let me know the findings so we can implement something based on a real scenario.

lucasvieirasilva commented 4 months ago

@jafaircl I've created an example repo that uses the FastAPI CLI to serve the Rest API with watch mode, it worked pretty well, I don't think we need a dedicated Nx executor for that, the serve target is pretty simple and straightforward

https://github.com/lucasvieirasilva/nx-python-fastapi