stephenh / ts-poet

A code generator DSL for typescript
Apache License 2.0
103 stars 13 forks source link

filename pattern is too restrictive #58

Closed jollytoad closed 6 months ago

jollytoad commented 6 months ago

I'm trying to use ts-poet to generate a http router module after discovery of modules on the filesystem, it builds up imports of the routes registering them with the router.

I'm supporting module naming conventions similar to next.js/fresh, and also URLPattern style, so filenames may well contain characters such as: []():.*?+.

For example: ./routes/user/[name]/index.ts, ts-poet truncates the module filename to: ./routes/user.

It appears that ts-poet has a restrictive set of characters: const fileNamePattern = "(?:[a-zA-Z0-9._-]+)";

Is there any chance this could become less restrictive, or an option to override? AFAIK there is no restriction on the name of a module in TS/JS beyond that of the underlying filesystem, could it just be something like: "(?:[^/]+)", ie. anything other than forward or back slash?

jollytoad commented 6 months ago

It's also possible, if generation code for browser or Deno, that the module spec is a URL (with a <scheme>:, and possibly include chars such as ^@ for version denotation). I've not tested if these work, but I'd suspect not looking at the current modulePattern).

Would it make sense to just match the entire remainder of the string as the module specifier, after the importType?

stephenh commented 6 months ago

generate a http router module after discovery of modules on the filesystem

Neat!

const fileNamePattern = "(?:[a-zA-Z0-9._-]+)";

Ah yeah, happy to loosen that up. In general I don't love those regexes--iirc I copy/pasted them in from some older poet-ish library, and have always wondered if just dumber indexOf-based parsing would be simpler :shrug: .

If you'd like to submit a PR that adjusts the filename to what works for your purposes (both the file names and the scheme), that'd be great! Thanks!

jollytoad commented 6 months ago

Great, i'll take a look next week.

jollytoad commented 6 months ago

I've just discovered I can work around by using the Import.imports* functions directly.