jondot / hygen

The simple, fast, and scalable code generator that lives in your project.
http://www.hygen.io
MIT License
5.65k stars 253 forks source link

fix(config-resolver): it uses templates from config when explicitly set #398

Closed alexlobera closed 2 years ago

alexlobera commented 2 years ago

When using hygen as a standalone script in conjunction with yarn create something, npm create something, etc hygen fails to read custom templates passed in the config param.

The reason is because config.templates is applied only if the first condition is falsy, which is not the case using global yarn create, etc. In this case path.join(cwd, '_templates')] is always resolved to the default hygen _templates and actions (generator help, generator new, generator with-prompt).

const resolvedTemplates =
    [process.env.HYGEN_TMPLS, path.join(cwd, '_templates')].find(
      (_) => _ && fs.existsSync(_),
    ) || templates // 👈

I think it would make more sense to first apply config.templates if they are explicitly passed:

 const resolvedTemplates =
    templates || // 👈
    [process.env.HYGEN_TMPLS, path.join(cwd, '_templates')].find(
      (_) => _ && fs.existsSync(_),
    )