jstransformers / jstransformer

Normalize the API of any JSTransformer.
http://npm.im/jstransformer
MIT License
153 stars 12 forks source link

Detect whether there are any jstransformers installed #196

Closed ismay closed 7 years ago

ismay commented 7 years ago

I was wondering if there is a way to detect whether a user has any jstransformers installed. I'd like to check that for this library: https://github.com/superwolff/metalsmith-engine-jstransformer, because it would be userfriendly to throw an error when there aren't any jstransformers installed.

calebeby commented 7 years ago

Could you use try/catch with require?

ismay commented 7 years ago

I could, but in that case I would have to do that for every transformer that exists, which is a bit much :). For a file that you want to transform something like that would work, but in my case I want to check whether a user has any jstransformers installed at all.

tunnckoCore commented 7 years ago

You should use jstransformer-jstransformer in such libraries. Then if user try to .render for example it won't work. And will throw if there isn't options.engine passed.

ismay commented 7 years ago

@tunnckoCore I might be misunderstanding you, but I'm already using jstransformer/jstransformer. And metalsmith-engine-jstransformer isn't directly used by the end user. It's consumed by metalsmith-in-place, which renders a whole batch of files by passing them individually to metalsmith-engine-jstransformer.

So your suggestion would solve detecting whether a user has any jstransformers installed for a single file. Which I'm already doing here: https://github.com/superwolff/metalsmith-engine-jstransformer/blob/master/lib/get-transformer.js#L20. But what I'm trying to do is detect whether there are any jstransformers installed so I can remind the user to do so (as some users forget it).

ismay commented 7 years ago

But anyway, if it's not possible or just a very expensive operation I understand. Just wondering if this org. already had a solution for such a problem, as it might be something you've run into as well.

tunnckoCore commented 7 years ago

With jstransformer-jstransformer i meant really jstransformer-jstransformer not jstransformers/jstransformer. ref: https://github.com/jstransformers/jstransformer-jstransformer.

It is the best thing for such things. And no more abstraction or things are needed. No matter what where is used.

And @RobLoach already did such thing with https://github.com/RobLoach/metalsmith-jstransformer. Actually it uses https://github.com/jstransformers/inputformat-to-jstransformer to give the ability of that "based on extension" thing.

tunnckoCore commented 7 years ago

I can't get what the idea behind metalsmith-engine-jstransformer and metalsmith-in-place is. Both seems like the "official" ones.

ismay commented 7 years ago

And @RobLoach already did such thing with https://github.com/RobLoach/metalsmith-jstransformer. Actually it uses https://github.com/jstransformers/inputformat-to-jstransformer to give the ability of that "based on extension" thing.

Like this you mean? To be clear, I'm not trying to see whether the user has a jstransformer installed, but whether the user has any jstransformers installed.

But thinking about it I guess my only option would be to loop through a dictionary of all existing jstransformers and try/catch require them. So I guess that's not really an error I can efficiently catch in my particular usecase.

RyanZim commented 7 years ago

But thinking about it I guess my only option would be to loop through a dictionary of all existing jstransformers and try/catch require them.

That's the only way I can think of, other than using https://github.com/substack/node-resolve, which wouldn't be much more efficient.

tunnckoCore commented 7 years ago

Still don't see a difference. What I understand from you, you want to do a thing that already is done at least by input-to-jstransformer using require-one.

Like this you mean?

I seen that you using it. But can you explain me what is the difference between the two packages - the "official" by Rob metalsmith-jstransformer and yours metalsmith-engine-jstransformer? And why in-place exists? It just uses promises, instead of callbacks. All i see is duplication of logic, code and idea - nothing more, sorry.

I'm not aggressive, maybe I'm just the next idiot in that world.

edit: btw, on readme of metalsmith-engine-jstransformer there's no example using it. In its example is used metalsmith-in-place.

edit2:

But thinking about it I guess my only option would be to loop through a dictionary of all existing jstransformers and try/catch require them.

That's done in input-to-*.

ismay commented 7 years ago

@tunnckoCore

I seen that you using it. But can you explain me what is the difference between the two packages - the "official" by Rob metalsmith-jstransformer and yours metalsmith-engine-jstransformer? And why in-place exists? It just uses promises, instead of callbacks. All i see is duplication of logic, code and idea - nothing more, sorry.

Metalsmith-in-place's purpose is to render templates to static html (even though it can be used for more). It was forked from metalsmith-templates, together with metalsmith-layouts, because metalsmith-templates was doing too much for one lib. Metalsmith-layouts mainly exists as a polyfill for languages that don't support extending, like handlebars, and is better off as a separate lib.

So the base library is in-place, and it is purposefully separate from layouts. We've recently started moving in-place to jstransformers because the approach is more modular than consolidate, which we used before. Together with that we also made the rendering engine replaceable, which is why engine-jstransformer is a separate lib (so users who prefer consolidate can still swap it back in).

Metalsmith-engine-jstransformer was based on metalsmith-jstransformer (which is why I credit Rob in the readme), however the approach is different, as metalsmith-jstransformer allows for similar functionality as metalsmith-layouts, with specifying a layout to extend from, recursive extending, etc. In-place only renders a single file at a time, with no recursion (unless the transformer itself allows for recursion)

So the underlying design decisions are different. It doesn't 'just use promises' or 'duplicate logic, code and ideas'. By that rationale, metalsmith-jstransformer would just be a duplication of logic, code and ideas of metalsmith-layouts. See the code or the readmes if you want to know more about what the differences are.

Still don't see a difference. What I understand from you, you want to do a thing that already is done at least by input-to-jstransformer using require-one.

That's ok, @RyanZim answered my question, feel free to close this issue.

edit: btw, on readme of metalsmith-engine-jstransformer there's no example using it. In its example is used metalsmith-in-place.

Don't understand what you mean. Feel free to open an issue if there's something missing from the readme.

tunnckoCore commented 7 years ago

Okey, I'm idiot, but you still not answer what is the diff.

As about the readme of metalsmith-engine-jstransformer, i'm talking about that

2017-01-27-19 50 59_1280x1024_scrot

RobLoach commented 7 years ago

@ismay For http://github.com/robloach/metalsmith-jstransformer , I use two packages:

Translates the file extension to an input format, loads the list of jstransformers from them, and then uses require-one to load one of the packages.

ismay commented 7 years ago

@tunnckoCore Ah ok, I see what you mean. Yeah the readme is still a bit confusing. There's an issue open for that, that I will hopefully get around to soon. Thanks for the heads up 👍

@RobLoach Yeah I'm using inputformat-to-jstransformer and it works great, I'll just need to make some changes to my implementation in order to remind the user to install jstransformers. Thanks! 👍