[jsscripting] Fix node_modules handling (#14398)

Signed-off-by: Jan N. Klug <github@klug.nrw>
This commit is contained in:
J-N-K 2023-02-18 19:53:33 +01:00 committed by GitHub
parent f3d48681b0
commit 223deffcfd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 10 deletions

View File

@ -89,8 +89,8 @@ public final class GraalJSScriptEngineFactory implements ScriptEngineFactory {
if (!SCRIPT_TYPES.contains(scriptType)) {
return null;
}
return new DebuggingGraalScriptEngine<>(
new OpenhabGraalJSScriptEngine(injectionEnabled, useIncludedLibrary, jsScriptServiceUtil));
return new DebuggingGraalScriptEngine<>(new OpenhabGraalJSScriptEngine(injectionEnabled, useIncludedLibrary,
jsScriptServiceUtil, jsDependencyTracker));
}
@Override

View File

@ -139,7 +139,7 @@ public class OpenhabGraalJSScriptEngine
* lifecycle and provides hooks for scripts to do so too.
*/
public OpenhabGraalJSScriptEngine(boolean injectionEnabled, boolean useIncludedLibrary,
JSScriptServiceUtil jsScriptServiceUtil) {
JSScriptServiceUtil jsScriptServiceUtil, JSDependencyTracker jsDependencyTracker) {
super(null); // delegate depends on fields not yet initialised, so we cannot set it immediately
this.injectionEnabled = injectionEnabled;
this.useIncludedLibrary = useIncludedLibrary;
@ -149,7 +149,8 @@ public class OpenhabGraalJSScriptEngine
delegate = GraalJSScriptEngine.create(ENGINE,
Context.newBuilder("js").allowExperimentalOptions(true).allowAllAccess(true)
.allowHostAccess(HOST_ACCESS).option("js.commonjs-require-cwd", JSDependencyTracker.LIB_PATH)
.allowHostAccess(HOST_ACCESS)
.option("js.commonjs-require-cwd", jsDependencyTracker.getLibraryPath().toString())
.option("js.nashorn-compat", "true") // Enable Nashorn compat mode as openhab-js relies on
// accessors, see
// https://github.com/oracle/graaljs/blob/master/docs/user/NashornMigrationGuide.md#accessors

View File

@ -13,7 +13,6 @@
package org.openhab.automation.jsscripting.internal.fs.watch;
import java.io.File;
import java.nio.file.Files;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.automation.module.script.ScriptDependencyTracker;
@ -40,15 +39,11 @@ public class JSDependencyTracker extends AbstractScriptDependencyTracker {
private final Logger logger = LoggerFactory.getLogger(JSDependencyTracker.class);
public static final String LIB_PATH = String.join(File.separator, "automation", "js", "node_modules");
private static final String LIB_PATH = String.join(File.separator, "automation", "js", "node_modules");
@Activate
public JSDependencyTracker(@Reference(target = WatchService.CONFIG_WATCHER_FILTER) WatchService watchService) {
super(watchService, LIB_PATH);
if (Files.isRegularFile(this.libraryPath)) {
logger.warn("Trying to watch directory '{}', however it is a file", this.libraryPath);
}
}
@Deactivate