From 33505dd2a51121233cc38d2b40487b26ba23daa9 Mon Sep 17 00:00:00 2001 From: Max Hauser Date: Sun, 24 Mar 2024 13:15:44 +0100 Subject: [PATCH] compatibility with new dev controller with new esm/cjs exports (#577) * compatibility with new dev controller with new esm/cjs exports * readme change and code reorder --- README.md | 4 +++ build/controllerTools.js | 39 +++++++++++++--------------- build/utils.js | 54 ++++++++++++-------------------------- src/controllerTools.ts | 35 ++++++++++++------------- src/utils.ts | 56 ++++++++++++++-------------------------- 5 files changed, 74 insertions(+), 114 deletions(-) diff --git a/README.md b/README.md index 78b070d..24e241f 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,10 @@ If you find errors in the definitions, e.g. function calls that should be allowe Placeholder for the next version (at the beginning of the line): ### **WORK IN PROGRESS** --> +### **WORK IN PROGRESS** + +- (foxriver76) compatibility with next controller with new esm/cjs exports + ### 3.0.4 (2023-10-12) - (foxriver76) ensure that utility methods work with the returned adapter instance on type level diff --git a/build/controllerTools.js b/build/controllerTools.js index e82bc25..24213e1 100644 --- a/build/controllerTools.js +++ b/build/controllerTools.js @@ -58,27 +58,24 @@ function resolveNamedModule(name, exportName = name) { if (exports.controllerCommonModulesInternal === null || exports.controllerCommonModulesInternal === void 0 ? void 0 : exports.controllerCommonModulesInternal[exportName]) return exports.controllerCommonModulesInternal[exportName]; // Otherwise it was not moved yet, or we're dealing with JS-Controller <= 4.0 - // Attempt 1: JS-Controller 4.1+ - let importPath = path.join(utils.controllerDir, "build/lib", name); - try { - // This was a default export prior to the TS migration - const module = require(importPath); - if (module) - return module; - } - catch (_a) { - // did not work, continue - } - // Attempt 2: JS-Controller <= 4.0 - importPath = path.join(utils.controllerDir, "lib", name); - try { - // This was a default export prior to the TS migration - const module = require(importPath); - if (module) - return module; - } - catch (_b) { - // did not work, continue + const importPaths = [ + // Attempt 1: JS-Controller 5.1+ + path.join(utils.controllerDir, "build/cjs/lib", name), + // Attempt 2: JS-Controller 4.1+ + path.join(utils.controllerDir, "build/lib", name), + // Attempt 3: JS-Controller <= 4.0 + path.join(utils.controllerDir, "lib", name), + ]; + for (const importPath of importPaths) { + try { + // This was a default export prior to the TS migration + const module = require(importPath); + if (module) + return module; + } + catch (_a) { + // did not work, continue + } } throw new Error(`Cannot resolve JS-Controller module ${name}.js`); //return process.exit(10); diff --git a/build/utils.js b/build/utils.js index 490f6e0..4d7d706 100644 --- a/build/utils.js +++ b/build/utils.js @@ -34,11 +34,23 @@ function getControllerDir(isInstall) { /** The root directory of JS-Controller */ exports.controllerDir = getControllerDir(!!((_a = process === null || process === void 0 ? void 0 : process.argv) === null || _a === void 0 ? void 0 : _a.includes("--install"))); function resolveAdapterConstructor() { - // Attempt 1: Resolve @iobroker/js-controller-adapter from here - JS-Controller 4.1+ - let adapterPath = (0, helpers_1.tryResolvePackage)(["@iobroker/js-controller-adapter"]); - if (adapterPath) { + const adapterPaths = [ + // Attempt 1: Resolve @iobroker/js-controller-adapter from here - JS-Controller 4.1+ + (0, helpers_1.tryResolvePackage)(["@iobroker/js-controller-adapter"]), + // Attempt 2: Resolve @iobroker/js-controller-adapter in JS-Controller dir - JS-Controller 4.1+ + (0, helpers_1.tryResolvePackage)(["@iobroker/js-controller-adapter"], [path.join(exports.controllerDir, "node_modules")]), + // Attempt 3: JS-Controller 4.1+ with adapter stub + path.join(exports.controllerDir, "build/lib/adapter.js"), + // Attempt 4: JS-Controller 5.1+ with adapter stub + path.join(exports.controllerDir, "build/cjs/lib/adapter.js"), + // Attempt 5: Legacy resolve - until JS-Controller 4.0 + path.join(exports.controllerDir, "lib/adapter.js"), + ]; + for (const adapterPath of adapterPaths) { + if (!adapterPath) + continue; try { - const { Adapter } = require(adapterPath); + const Adapter = require(adapterPath); if (Adapter) return Adapter; } @@ -46,40 +58,6 @@ function resolveAdapterConstructor() { // did not work, continue } } - // Attempt 2: Resolve @iobroker/js-controller-adapter in JS-Controller dir - JS-Controller 4.1+ - adapterPath = (0, helpers_1.tryResolvePackage)(["@iobroker/js-controller-adapter"], [path.join(exports.controllerDir, "node_modules")]); - if (adapterPath) { - try { - const { Adapter } = require(adapterPath); - if (Adapter) - return Adapter; - } - catch (_b) { - // did not work, continue - } - } - // Attempt 3: Legacy resolve - until JS-Controller 4.0 - adapterPath = path.join(exports.controllerDir, "lib/adapter.js"); - try { - // This was a default export prior to the TS migration - const Adapter = require(adapterPath); - if (Adapter) - return Adapter; - } - catch (_c) { - // did not work, continue - } - // Attempt 4: JS-Controller 4.1+ with adapter stub - adapterPath = path.join(exports.controllerDir, "build/lib/adapter.js"); - try { - // This was a default export prior to the TS migration - const Adapter = require(adapterPath); - if (Adapter) - return Adapter; - } - catch (_d) { - // did not work, continue - } throw new Error("Cannot resolve adapter class"); return process.exit(10); } diff --git a/src/controllerTools.ts b/src/controllerTools.ts index 3b3996f..8ae39c4 100644 --- a/src/controllerTools.ts +++ b/src/controllerTools.ts @@ -67,24 +67,23 @@ export function resolveNamedModule( // Otherwise it was not moved yet, or we're dealing with JS-Controller <= 4.0 - // Attempt 1: JS-Controller 4.1+ - let importPath = path.join(utils.controllerDir, "build/lib", name); - try { - // This was a default export prior to the TS migration - const module = require(importPath); - if (module) return module; - } catch { - // did not work, continue - } - - // Attempt 2: JS-Controller <= 4.0 - importPath = path.join(utils.controllerDir, "lib", name); - try { - // This was a default export prior to the TS migration - const module = require(importPath); - if (module) return module; - } catch { - // did not work, continue + const importPaths = [ + // Attempt 1: JS-Controller 5.1+ + path.join(utils.controllerDir, "build/cjs/lib", name), + // Attempt 2: JS-Controller 4.1+ + path.join(utils.controllerDir, "build/lib", name), + // Attempt 3: JS-Controller <= 4.0 + path.join(utils.controllerDir, "lib", name), + ]; + + for (const importPath of importPaths) { + try { + // This was a default export prior to the TS migration + const module = require(importPath); + if (module) return module; + } catch { + // did not work, continue + } } throw new Error(`Cannot resolve JS-Controller module ${name}.js`); diff --git a/src/utils.ts b/src/utils.ts index ccaf8ef..454699d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -35,51 +35,33 @@ export const controllerDir = getControllerDir( ); function resolveAdapterConstructor(): any | never { - // Attempt 1: Resolve @iobroker/js-controller-adapter from here - JS-Controller 4.1+ - let adapterPath = tryResolvePackage(["@iobroker/js-controller-adapter"]); - if (adapterPath) { - try { - const { Adapter } = require(adapterPath); - if (Adapter) return Adapter; - } catch { - // did not work, continue - } - } + const adapterPaths = [ + // Attempt 1: Resolve @iobroker/js-controller-adapter from here - JS-Controller 4.1+ + tryResolvePackage(["@iobroker/js-controller-adapter"]), + // Attempt 2: Resolve @iobroker/js-controller-adapter in JS-Controller dir - JS-Controller 4.1+ + tryResolvePackage( + ["@iobroker/js-controller-adapter"], + [path.join(controllerDir, "node_modules")], + ), + // Attempt 3: JS-Controller 4.1+ with adapter stub + path.join(controllerDir, "build/lib/adapter.js"), + // Attempt 4: JS-Controller 5.1+ with adapter stub + path.join(controllerDir, "build/cjs/lib/adapter.js"), + // Attempt 5: Legacy resolve - until JS-Controller 4.0 + path.join(controllerDir, "lib/adapter.js"), + ]; + + for (const adapterPath of adapterPaths) { + if (!adapterPath) continue; - // Attempt 2: Resolve @iobroker/js-controller-adapter in JS-Controller dir - JS-Controller 4.1+ - adapterPath = tryResolvePackage( - ["@iobroker/js-controller-adapter"], - [path.join(controllerDir, "node_modules")], - ); - if (adapterPath) { try { - const { Adapter } = require(adapterPath); + const Adapter = require(adapterPath); if (Adapter) return Adapter; } catch { // did not work, continue } } - // Attempt 3: Legacy resolve - until JS-Controller 4.0 - adapterPath = path.join(controllerDir, "lib/adapter.js"); - try { - // This was a default export prior to the TS migration - const Adapter = require(adapterPath); - if (Adapter) return Adapter; - } catch { - // did not work, continue - } - - // Attempt 4: JS-Controller 4.1+ with adapter stub - adapterPath = path.join(controllerDir, "build/lib/adapter.js"); - try { - // This was a default export prior to the TS migration - const Adapter = require(adapterPath); - if (Adapter) return Adapter; - } catch { - // did not work, continue - } - throw new Error("Cannot resolve adapter class"); return process.exit(10); }