mattpocock / xstate-codegen

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

Getting match for single machine only #49

Closed danielkcz closed 3 years ago

danielkcz commented 3 years ago

It's most likely an issue in upstream gaze, but just in case someone else stumbles upon it

I have my machines structured like

machinery
  src
    firstMachine
      firstMachine.ts
      index.ts
    secondMachine
      secondMachine.ts
      index.ts

Upon running xstate-codegen machinery/src/**/**Machine.ts I get match for the firstMachine, nothing else. And that machine isn't even using @xstate/compiled yet (maybe it should be skipped? 🤷‍♂️).

I actually tried logging filesAsRecord and it indeed gives a single result for some reason.

I am running this in WSL2, so it's not a Windows issue. But if you see some problem with my pattern, let me know. Otherwise, I will probably open the issue with gaze.

mattpocock commented 3 years ago

Wow, that is super odd. I am also open to other alternatives to gaze, since it feels a bit hacky and doesn't always work as intended. I will keep this issue open to add a test case to the repo to see if I can repeat it.

danielkcz commented 3 years ago

Well, this has officially become a huge problem when I wanted to use codegen for a second machine. Because of this, it seems I cannot. I will dive deeper into a debugger and try to figure out where it goes wrong.

Perhaps instead of gaze the https://www.npmjs.com/package/chokidar could be an option? Never used it personally, but seen it with a couple of projects.

danielkcz commented 3 years ago

Whoa...turns out it's some sort of "feature" of bash (?). When I inspect process.argv directly in codegen index.js I get this...

yarn xstate-codegen modules/machinery/src/*Machine.ts
[
  '/home/fredyc/.nvm/versions/node/v12.16.3/bin/node',
  '/home/fredyc/workspace/lmv/node_modules/.bin/xstate-codegen',
  'modules/machinery/src/audioControlMachine.ts',
  'modules/machinery/src/languageMachine.ts',
  'modules/machinery/src/meetPairMachine.ts'
]

Something is translating that glob before it even gets into Node process 😱. Um, I guess it's super cool, but since codegen expects single arg it's problem. I honestly never heard about this. Tried the same on Windows and no such magic happens there. I suppose MacOS is also dumb on this.

I wonder what would be the best approach to handle this. I mean the minimist should be probably able to extract multiple patterns from args and gaze seems to support that as well.

The alternative would be to read this from some config file which wouldn't be so clever.

What do you think @mattpocock?

danielkcz commented 3 years ago

Oh wait, is the reason why all your examples are using quotes around the pattern? 😆 Yes, I can confirm it works with that but wasn't aware this is the reason so I did not bother...

I suppose quick & dirty "fix" would be to check if multiple "patterns" were passed and throw an error with an explanation. But generally, it would be better to support multiple patterns anyway. Strangely enough, only the first glob is auto "de-globbed", so it's not possible to make any assumptions about this.

mattpocock commented 3 years ago

Ahaaaa yes indeed - this is also why Prettier and other CLI's accept only quoted globs, not the raw glob itself. Bash just does this transpilation itself.

Agree that we can do better by checking multiple patterns.