Skip to content

Commit

Permalink
compatibility with new dev controller with new esm/cjs exports (#577)
Browse files Browse the repository at this point in the history
* compatibility with new dev controller with new esm/cjs exports

* readme change and code reorder
  • Loading branch information
foxriver76 committed Mar 24, 2024
1 parent 5e6ae96 commit 33505dd
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 114 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down
39 changes: 18 additions & 21 deletions build/controllerTools.js
Expand Up @@ -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);
Expand Down
54 changes: 16 additions & 38 deletions build/utils.js
Expand Up @@ -34,52 +34,30 @@ 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;
}
catch (_a) {
// 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);
}
Expand Down
35 changes: 17 additions & 18 deletions src/controllerTools.ts
Expand Up @@ -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`);
Expand Down
56 changes: 19 additions & 37 deletions src/utils.ts
Expand Up @@ -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);
}
Expand Down

0 comments on commit 33505dd

Please sign in to comment.