verbose / verb-repo-helpers

Commonly used helpers for adding repository-related data to templates in verb projects.
MIT License
5 stars 3 forks source link

Allow `apidocs` helper to accept an array #1

Open tunnckoCore opened 7 years ago

tunnckoCore commented 7 years ago

Hi! I'm just trying to create a condition. If src/index.js exists call apidocs for it and then if not exists check if index.js on root exists and call apidocs.

I tried ifExists helper, but it not worked as expected. Even always returns (somehow) truthy value. I expect (as read the code too) to return empty string if file not exist.

So the snippet in that if shouldn't be rendered, but it is.

{% if (ifExists('./src/inxxxxxxxxxdex.js')) { %}
## API
Review carefully the provided examples and the working [tests](./test.js).

{%= apidocs('src/index.js') %}

{% } %}

Even with .length (which is surely falsey value, negative) it still renders the block that is inside the if.

But even if it worked, it still be better if apidocs support such feature by default. What i imagine

## API
Review carefully the provided examples and the working [tests](./test.js).

{%= apidocs(['src/index.js', 'index.js']) %}

So when src/index.js not exist, it will continue to the next one until it find existing file.


Also tried with require helper to require the fs and call existsSync but it throws that it's not a function

jonschlinkert commented 7 years ago

I think it's because of async helpers. might need @doowb to input on this

doowb commented 7 years ago

I thought I responded to this last night...

Using the native javascript if statement doesn't work because of how async helpers work. There is a temporary results coming back from ifExists that is always a string and will always have a .length property.

You can pass values from a helper into another helper and the results will be resolved correctly, so you can do something like:

register a custom _if helper

// template-helpers has this, but I think it's implemented differently and may need to be updated
// or a new helper added that does this
app.helper('_if', function(condition, a, b) {
  return condition ? a : b;
});

use it with the apidocs and exists helpers

## API
Review carefully the provided examples and the working [tests](./test.js).

{%= apidocs(_if(exists('src/index.js'), 'src/index.js', 'index.js')) %}
tunnckoCore commented 7 years ago

Right.

Thanks for the suggestion! :) But still.. i don't want one more new file (verbfile.js) for such easy thing - first; second, it still will output the section header (## API etc), and third it still will call the apidocs helper (i know it is pretty optimized and won't do so much job, but..). :)

jonschlinkert commented 7 years ago

i don't want one more new file (verbfile.js) for such easy thing

yeah I agree, I think we can figure out a fix for this.

tunnckoCore commented 7 years ago

Yea, one probably cool thing that I thought for, is that to allow access Ramda or Lodash (or why not both) through the templates, hm? :)