Skip to content

Commit

Permalink
hotfix previous release: compatibility with next controller with new … (
Browse files Browse the repository at this point in the history
#578)

* hotfix previous release: compatibility with next controller with new esm/cjs exports

* build
  • Loading branch information
foxriver76 committed Mar 24, 2024
1 parent 0b60242 commit 7e8d91d
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 37 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) hotfix previous release: compatibility with next controller with new esm/cjs exports

### 3.0.5 (2024-03-24)

- (foxriver76) compatibility with next controller with new esm/cjs exports
Expand Down
2 changes: 1 addition & 1 deletion build/controllerTools.js
Expand Up @@ -59,7 +59,7 @@ function resolveNamedModule(name, exportName = name) {
return exports.controllerCommonModulesInternal[exportName];
// Otherwise it was not moved yet, or we're dealing with JS-Controller <= 4.0
const importPaths = [
// Attempt 1: JS-Controller 5.1+
// Attempt 1: JS-Controller 6+
path.join(utils.controllerDir, "build/cjs/lib", name),
// Attempt 2: JS-Controller 4.1+
path.join(utils.controllerDir, "build/lib", name),
Expand Down
1 change: 1 addition & 0 deletions build/index.d.ts
@@ -1,3 +1,4 @@
/// <reference types="iobroker" />
import { ExitCodes } from "./exitCodes";
import "@iobroker/types";
export { commonTools } from "./controllerTools";
Expand Down
1 change: 1 addition & 0 deletions build/utils.d.ts
@@ -1,3 +1,4 @@
/// <reference types="iobroker" />
/** The root directory of JS-Controller */
export declare const controllerDir: string;
/** Reads the configuration file of JS-Controller */
Expand Down
65 changes: 49 additions & 16 deletions build/utils.js
Expand Up @@ -34,30 +34,63 @@ 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() {
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;
// 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) {
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: JS-Controller 6+ with adapter stub
adapterPath = path.join(exports.controllerDir, "build/cjs/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
}
// Attempt 5: 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 (_e) {
// did not work, continue
}
throw new Error("Cannot resolve adapter class");
return process.exit(10);
}
Expand Down
2 changes: 1 addition & 1 deletion src/controllerTools.ts
Expand Up @@ -68,7 +68,7 @@ export function resolveNamedModule(
// Otherwise it was not moved yet, or we're dealing with JS-Controller <= 4.0

const importPaths = [
// Attempt 1: JS-Controller 5.1+
// Attempt 1: JS-Controller 6+
path.join(utils.controllerDir, "build/cjs/lib", name),
// Attempt 2: JS-Controller 4.1+
path.join(utils.controllerDir, "build/lib", name),
Expand Down
66 changes: 47 additions & 19 deletions src/utils.ts
Expand Up @@ -35,33 +35,61 @@ export const controllerDir = getControllerDir(
);

function resolveAdapterConstructor(): any | never {
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 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
}
}

// 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: JS-Controller 6+ with adapter stub
adapterPath = path.join(controllerDir, "build/cjs/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
}

// Attempt 5: 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
}

throw new Error("Cannot resolve adapter class");
return process.exit(10);
}
Expand Down

0 comments on commit 7e8d91d

Please sign in to comment.