csowada
(csowada)
January 13, 2022, 8:37pm
6
I’ve worked an a solution for the node_modules
directory issue. At the same time I’ve changed the behaviour to allow loading of relative files from the root folder.
opened 09:11PM - 10 Jan 22 UTC
enhancement
I miss the option to load relative files within the ``automation/js folder``. So… I can't separate my small helper functions from my rules without own modules in node_modules folder.
I've used a custom version of the automation in the past. But I've changed the path for ``js.commonjs-require-cwd`` to the root folder instead of the node_modules folder. This allows us to load files from the folder ``automation/js`` **and** ``node_modules``. So it is not required to create own node modules in the ``node_modules`` folder.
I've already build custom snapshot and it works as expected but I'm not sure if there is more to check. I only changed two lines to change the root directory for CommonJS loading.
But we have still one disadvantage, the FileWatcher will load all files in the folder except the ``node_modules`` folder. So the config.js in my example would also be loaded on startup. But in that case I just want to load the file via a ``require`` call in my rule.
So we still need a solution not only for the ``node_module`` folder. My suggestion is to provide an optional file similar to a ``.gitignore`` file to skip all folder and files defined in that file.
I think it's more a minor bug in the current implementation instead of a feature, but this is a question for @jpg0 and @digitaldan . If you agree with me I could create a PR.
In ``OpenhabGraalJSScriptEngine.java``
```java
.allowHostAccess(hostAccess).option("js.commonjs-require-cwd", JSDependencyTracker.LIB_PATH2)
```
and in ``JSDependencyTracker.java``
```java
public static final String LIB_PATH2 = String.join(File.separator, OpenHAB.getConfigFolder(), "automation", "js");
```
```
<folder> automation
-<folder> js
- myrule.js
- <folder>config
- myconfig.js
- <folder> node_modules
- <folder> mylib
- ...
```
```javascript
const mylib= require("mylib");
const config = require("./config/myconfig");
...
```
## Your Environment
* Version used: openHAB 3.2 release
You can download my build jar from github and create an .ohignore
file in the automation/js
directory
Use it like a .gitignore file and add following content as example.
node_modules/**
.git/**
do-not-load-me.js
This should skip all files within the node_modules directory and .git directory (if you use git). And the file do-not-load-me.js
in the automation/js directory.