keithamus / hbs-cli

A CLI for handlebars
43 stars 26 forks source link

--partial and --helper glob issues #49

Open mdmoreau opened 5 years ago

mdmoreau commented 5 years ago

I ran into what seems like a strange problem on Ubuntu 18.04 when trying to add partials and helpers as globs. I typically double quote and escape any paths/globs in npm scripts to ensure Windows support, but when used with --helper I get an error saying the first helper file in the glob can't be found. The opposite is true for partials, where a glob only works if double quoted.

The following is what I used to get everything working correctly: "hbs": "hbs \"src/hbs/*.hbs\" --output \"dist\" --helper ./src/hbs/util/*.js --partial \"./src/hbs/components/*.hbs\""

Do you know why helper might only work without quotes and partial only with quotes? If I pass each helper/partial individually, it didn't seem to matter if it is quoted or not.

Thanks for the great tool!

keithamus commented 5 years ago

Hey @mdmoreau thanks for the issue.

Can we get a bit more insight as to what your directory structure looks like? Maybe you could provide us with a repreduction? It is not clear to me what is happening here.

mdmoreau commented 5 years ago

@keithamus Absolutely! Here's a quick rundown of the project structure:

dist/
src/
  hbs/
    components/
      footer.hbs
      header.hbs
    layouts/
      default.hbs
    util/
      inline-svg.js
      layouts.js
    index.hbs
package.json

Calling helpers individually works fine: --helper ./src/hbs/util/inline-svg.js --helper ./src/hbs/util/layouts.js

Breaks when calling as a glob: --helper ./src/hbs/util/*.js Error: Missing helper: "extend"

Quote/escape the helper glob: --helper \"./src/hbs/util/*.js\" Error: Cannot find module './src/hbs/util/inline-svg.js'

Happy to provide a demo repo if you think that would help. Sorry for the delayed response as well - had wrapped up the project using hbs-cli, and created the issue afterwards. Definitely plan on using it again in the future though.

Thanks again for the help, and let me know if there's anything else I can do on my end.

keithamus commented 5 years ago

Calling with --helper ./src/hbs.util/*.js will use shell expansion to expand to --helper ./src/hbs/util/inline-svg.js ./src/hbs/util/layouts.js - so ./src/hbs/util/layouts.js becomes a positional argument rather than an argument to --helper, which is probably why that fails.

The quoted glob should "just work". Maybe could you run the command with DEBUG='*' environment variable? e.g. env DEBUG='*' npm run hbs. Hopefully this will give us some idea of what is happening which causes the module to not load.

mdmoreau commented 5 years ago

Here's what I'm getting when running with debug:

  hbs Parsed argv { _: [ 'src/hbs/*.hbs' ],
  version: false,
  v: false,
  help: false,
  h: false,
  stdout: false,
  s: false,
  output: 'dist',
  o: 'dist',
  helper: [ 'handlebars-layouts', './src/hbs/util/*.js' ],
  H: [ 'handlebars-layouts', './src/hbs/util/*.js' ],
  partial: [ './src/hbs/components/*.hbs', './src/hbs/layouts/*.hbs' ],
  P: [ './src/hbs/components/*.hbs', './src/hbs/layouts/*.hbs' ] } +0ms
  hbs Setting up helpers [ 'handlebars-layouts', './src/hbs/util/*.js' ] +6ms
  hbs Trying to require handlebars-layouts as a node_module +1ms
  hbs Trying to require ./src/hbs/util/*.js as a node_module +3ms
  hbs Setting up partials [ './src/hbs/components/*.hbs', './src/hbs/layouts/*.hbs' ] +0ms
  hbs Trying to require ./src/hbs/components/*.hbs as a node_module +0ms
  hbs Trying to require ./src/hbs/layouts/*.hbs as a node_module +1ms
  hbs ./src/hbs/util/*.js is glob or actual file, expanding... +4ms
  hbs ./src/hbs/components/*.hbs is glob or actual file, expanding... +3ms
  hbs Requiring /home/mdmoreau/Desktop/parachute/node_modules/handlebars-layouts/index.js +2ms
  hbs /home/mdmoreau/Desktop/parachute/node_modules/handlebars-layouts/index.js has a register function, registering with handlebars +1ms
  hbs Requiring ./src/hbs/util/inline-svg.js +0ms
Error: Cannot find module './src/hbs/util/inline-svg.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:594:15)
    at Function.Module._load (internal/modules/cjs/loader.js:520:25)
    at Module.require (internal/modules/cjs/loader.js:650:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at /home/mdmoreau/Desktop/parachute/node_modules/hbs-cli/lib/index.js:46:28
    at Array.forEach (<anonymous>)
    at addHandlebarsHelpers (/home/mdmoreau/Desktop/parachute/node_modules/hbs-cli/lib/index.js:44:9)

This is using the following npm script:

"hbs": "hbs \"src/hbs/*.hbs\" --output \"dist\" --helper handlebars-layouts --helper \"./src/hbs/util/*.js\" --partial \"./src/hbs/components/*.hbs\" --partial \"./src/hbs/layouts/*.hbs\""

If I remove the escaped quotes around the helper arg, it will work as long as there is only one helper in the glob. Anytime there is a second helper included that's actually used in a .hbs file, I get the following error:

  hbs Parsed argv { _: [ 'src/hbs/*.hbs', './src/hbs/util/test.js' ],
  version: false,
  v: false,
  help: false,
  h: false,
  stdout: false,
  s: false,
  output: 'dist',
  o: 'dist',
  helper: [ 'handlebars-layouts', './src/hbs/util/inline-svg.js' ],
  H: [ 'handlebars-layouts', './src/hbs/util/inline-svg.js' ],
  partial: [ './src/hbs/components/*.hbs', './src/hbs/layouts/*.hbs' ],
  P: [ './src/hbs/components/*.hbs', './src/hbs/layouts/*.hbs' ] } +0ms
  hbs Setting up helpers [ 'handlebars-layouts', './src/hbs/util/inline-svg.js' ] +4ms
  hbs Trying to require handlebars-layouts as a node_module +2ms
  hbs Trying to require ./src/hbs/util/inline-svg.js as a node_module +2ms
  hbs Setting up partials [ './src/hbs/components/*.hbs', './src/hbs/layouts/*.hbs' ] +0ms
  hbs Trying to require ./src/hbs/components/*.hbs as a node_module +0ms
  hbs Trying to require ./src/hbs/layouts/*.hbs as a node_module +0ms
  hbs Requiring /home/mdmoreau/Desktop/parachute/node_modules/handlebars-layouts/index.js +5ms
  hbs /home/mdmoreau/Desktop/parachute/node_modules/handlebars-layouts/index.js has a register function, registering with handlebars +1ms
  hbs Requiring /home/mdmoreau/Desktop/parachute/src/hbs/util/inline-svg.js +0ms
  hbs /home/mdmoreau/Desktop/parachute/src/hbs/util/inline-svg.js has a register function, registering with handlebars +0ms
  hbs ./src/hbs/layouts/*.hbs is glob or actual file, expanding... +2ms
  hbs ./src/hbs/components/*.hbs is glob or actual file, expanding... +3ms
  hbs Registering partial ./src/hbs/components/footer.hbs +5ms
  hbs Registering partial ./src/hbs/components/header.hbs +1ms
  hbs Registering partial ./src/hbs/layouts/default.hbs +0ms
  hbs Trying to require src/hbs/*.hbs as a node_module +5ms
  hbs Trying to require ./src/hbs/util/test.js as a node_module +0ms
  hbs src/hbs/*.hbs is glob or actual file, expanding... +4ms
  hbs Rendering template src/hbs/faq.hbs with data {} +1ms
  hbs Rendering template src/hbs/index.hbs with data {} +0ms
  hbs Rendering template /home/mdmoreau/Desktop/parachute/src/hbs/util/test.js with data {} +0ms
Error: Missing helper: "test"
    at Object.<anonymous> (/home/mdmoreau/Desktop/parachute/node_modules/handlebars/dist/cjs/handlebars/helpers/helper-missing.js:19:13)
    at eval (eval at createFunctionContext (/home/mdmoreau/Desktop/parachute/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:254:23), <anonymous>:8:70)
    at Object.prog [as fn] (/home/mdmoreau/Desktop/parachute/node_modules/handlebars/dist/cjs/handlebars/runtime.js:221:12)
    at fn (/home/mdmoreau/Desktop/parachute/node_modules/handlebars-layouts/index.js:41:17)
    at Object.applyAction (/home/mdmoreau/Desktop/parachute/node_modules/handlebars-layouts/index.js:54:11)
    at Array.reduce (<anonymous>)
    at Object.block (/home/mdmoreau/Desktop/parachute/node_modules/handlebars-layouts/index.js:171:43)
    at Object.eval [as main] (eval at createFunctionContext (/home/mdmoreau/Desktop/parachute/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:254:23), <anonymous>:6:87)
    at main (/home/mdmoreau/Desktop/parachute/node_modules/handlebars/dist/cjs/handlebars/runtime.js:175:32)
    at ret (/home/mdmoreau/Desktop/parachute/node_modules/handlebars/dist/cjs/handlebars/runtime.js:178:12)
    at ret (/home/mdmoreau/Desktop/parachute/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:526:21)
    at Object.extend (/home/mdmoreau/Desktop/parachute/node_modules/handlebars-layouts/index.js:132:11)
    at Object.eval [as main] (eval at createFunctionContext (/home/mdmoreau/Desktop/parachute/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:254:23), <anonymous>:5:92)
    at main (/home/mdmoreau/Desktop/parachute/node_modules/handlebars/dist/cjs/handlebars/runtime.js:175:32)
    at ret (/home/mdmoreau/Desktop/parachute/node_modules/handlebars/dist/cjs/handlebars/runtime.js:178:12)
    at ret (/home/mdmoreau/Desktop/parachute/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:526:21)

The line right before the error hbs Rendering template /home/mdmoreau/Desktop/parachute/src/hbs/util/test.js with data {} +0ms looks like it's reading the second file in helper glob as one of the input templates for some reason.

mdmoreau commented 5 years ago

@keithamus I setup a demo with the problem at https://github.com/mdmoreau/hbs-cli-demo. The quoted glob still isn't working in a helper for me, but when I call them individually it compiles correctly.

"hbs \"src/hbs/*.hbs\" --output \"dist\" --helper handlebars-layouts --helper \"./src/hbs/util/inline-svg.js\" --helper \"./src/hbs/util/svg-inline.js\" --partial \"./src/hbs/layouts/*.hbs\""
keithamus commented 5 years ago

@mdmoreau I dont know if I'll be able to find the time to look into this soon, but I've put it on my todo list and will get round to it. I'll do my best though 😄

mdmoreau commented 5 years ago

@keithamus Thanks! I don't mind helping out if you have any idea where the issue might be.

keithamus commented 5 years ago

Here's where the helpers get added:

https://github.com/keithamus/hbs-cli/blob/feacd1c0d6a0ee5c7ea0465c8356ca1789af71c5/src/index.js#L163

each --helper call gets fed into resolveModuleOrGlob via here:

https://github.com/keithamus/hbs-cli/blob/feacd1c0d6a0ee5c7ea0465c8356ca1789af71c5/src/index.js#L38-L40

and here's the logic for that resolveModuleOrGlob function:

https://github.com/keithamus/hbs-cli/blob/feacd1c0d6a0ee5c7ea0465c8356ca1789af71c5/src/index.js#L22-L28

Finally once all files are resolved they get given to addHandlebarsPartials:

https://github.com/keithamus/hbs-cli/blob/feacd1c0d6a0ee5c7ea0465c8356ca1789af71c5/src/index.js#L57-L60

It is probably this part that is going wrong, looking at the debug output. I'd probably just scatter console logs in this function to see where things are going wrong, then investigate from there. I don't have an immediate hunch as to what is going wrong though.