mattpocock / xstate-codegen

A codegen tool for 100% TS type-safety in XState
MIT License
245 stars 12 forks source link

Package manager removes @xstate/compiled on run #51

Closed danielkcz closed 3 years ago

danielkcz commented 3 years ago

I mean it's kinda expected, but annoying what it happens. I wanted to install a new dependency and suddenly app started complaining it cannot find that module.

I guess codegen should also watch on its own output and when it disappears, rerun the generator.

mattpocock commented 3 years ago

Ah yes, this is annoying. My fix for this is to add xstate-codegen "src/**/**.whatever.ts" --once in a postinstall script, so that you're covered after these reinstalls. We should add this to documentation as the 'official' way.

danielkcz commented 3 years ago

I guess for now it would do, but duplicating that pattern in package.json could be a nightmare if something more elaborate is needed.

mattpocock commented 3 years ago

What do you mean by more elaborate?

danielkcz commented 3 years ago

Well, you need to duplicate that command with --once including the pattern to run it as postinstall script. I am not sure what is there to say to it. Perhaps it's my concern only :)

mattpocock commented 3 years ago

OK - I think there are two options here:

  1. Document this approach carefully to make sure people adopt it as part of installation. I use it for my work repo and it does address 100% of the issues above.

  2. Add an extra watch to the CLI to check the node_modules directory to check for deletions. This is the more complex approach, and doesn't cover all bases. For instance, what if someone runs yarn install but the CLI watch isn't running?

danielkcz commented 3 years ago

There are surely more pressing matters than this, so don't worry too much about it right now. Maybe next year when everything is running smoothly :)

danielkcz commented 3 years ago

About the duplication, I actually realized it's possible to do this, so that's totally fine. Strangely enough, it doesn't run always for some reason. I am not really sure why yet. Might be related to monorepo situation and having this in root means it will run only when root packages changes 🤷

    "codegen": "xstate-codegen \"modules/machinery/src/**/*Machine.ts\"",
    "postinstall": "yarn codegen --once"

There is one gotcha when running in CI. Since my machines depend on other generated stuff (GraphQL), the build was failing because it attempted to run codegen too soon. My solution is to use --ignore-scripts for the CI run. It's a bit fragile, but it will do for now.

mattpocock commented 3 years ago

@FredyC Sometimes it won't run when you add or remove a package in a workspace, but it should always run when you run yarn install in root.