Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow apidocs helper to accept an array #1

Open
tunnckoCore opened this issue Aug 23, 2017 · 5 comments
Open

Allow apidocs helper to accept an array #1

tunnckoCore opened this issue Aug 23, 2017 · 5 comments

Comments

@tunnckoCore
Copy link

tunnckoCore commented Aug 23, 2017

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
Copy link
Member

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

@doowb
Copy link
Member

doowb commented Aug 23, 2017

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
Copy link
Author

tunnckoCore commented Aug 27, 2017

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
Copy link
Member

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
Copy link
Author

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? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants