openhab / openhab-js

openHAB JavaScript Library for JavaScript Scripting Automation
https://www.openhab.org/addons/automation/jsscripting/
Eclipse Public License 2.0
38 stars 31 forks source link

Add function that returns version of library #238

Closed rkoshak closed 1 year ago

rkoshak commented 1 year ago

As I've been playing with and incorporating the changes in the 4.0 release it occurs to me that there are a few places where it would be handy to know which version of the openhab-js library is installed. I'm mostly thinking about rule template situations where it might have completely different approaches based on the base library.

Obviously I can infer the version based on whether certain functions and properties exist or not but that feels brittle.

My idea was to have a function, probably in utils that returns the version of openhab.js. Initially I thought it could just load the version from package.json but I'm not certain that file would be available to the library when using the version that comes with the add-on. If there isn't another way short of needing to remember to change some constant in the code every time a release is made this may not be worth doing.

It's probably a half baked idea but surely this is something that Node developers have had to solve, right?

florian-h05 commented 1 year ago

Loading the version from the package json would be the easiest way, but unfortunately the information of the package.json is not available for the version included in the addon, because the included version is compiled/bundled using webpack.

AFAIK there is no existing solution for this in the Node universe (and some googling didn‘t bring any solutions).

So the only way would be to add a constant to the code, but now the question is: where? I would say the package root export, but he have to keep in mind that everything that is there will become a global (if enabled) and may pollute the namespace.

Regarding the updating: I would propose to use a npm hook that run‘s pre release/webpacking and inserts the current version into the constant.

rkoshak commented 1 year ago

To prevent pollution, I'd put it in utils where other stuff that most end users don't care about like dumpObject and javaListToJavascriptArray (of whatever those collection of functions are called).

That tucks it out of the way so it doesn't get in anyone's way and, since we don't document utils it can be one of those hidden features those who bother to look at the code can discover. Like I said, this is probably going to be something that only Rule Template developers will probably care about so they can maintain one template that works with multiple versions of the library.

florian-h05 commented 1 year ago

I had a look into this, it indeed was much easier as expected. I can import the version string from the package.json inside the utils.js and export it as const, webpack handles this!!

PR is now open, see #244. I chose to name it OPENHAB_JS_VERSION because VERSION IMO might be not concise enough for some users (they could think that is is the openHAB or addon version or whatever).