UNPKG

4.36 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.commonTools = exports.resolveNamedModule = exports.controllerToolsInternal = exports.controllerCommonModulesInternal = void 0;
4const path = require("path");
5const helpers_1 = require("./helpers");
6const utils = require("./utils");
7function resolveControllerTools() {
8 // Attempt 1: Resolve @iobroker/js-controller-common from here - JS-Controller 4.1+
9 let importPath = (0, helpers_1.tryResolvePackage)(["@iobroker/js-controller-common"]);
10 if (importPath) {
11 try {
12 exports.controllerCommonModulesInternal = require(importPath);
13 const { tools } = exports.controllerCommonModulesInternal;
14 if (tools)
15 return tools;
16 }
17 catch (_a) {
18 // did not work, continue
19 }
20 }
21 // Attempt 2: Resolve @iobroker/js-controller-common in JS-Controller dir - JS-Controller 4.1+
22 importPath = (0, helpers_1.tryResolvePackage)(["@iobroker/js-controller-common"], [path.join(utils.controllerDir, "node_modules")]);
23 if (importPath) {
24 try {
25 exports.controllerCommonModulesInternal = require(importPath);
26 const { tools } = exports.controllerCommonModulesInternal;
27 if (tools)
28 return tools;
29 }
30 catch (_b) {
31 // did not work, continue
32 }
33 }
34 // Attempt 3: Legacy resolve - until JS-Controller 4.0
35 importPath = path.join(utils.controllerDir, "lib");
36 try {
37 // This was a default export prior to the TS migration
38 const tools = require(path.join(importPath, "tools"));
39 if (tools)
40 return tools;
41 }
42 catch (_c) {
43 // did not work, continue
44 }
45 throw new Error("Cannot resolve tools module");
46 return process.exit(10);
47}
48/** The collection of utility functions in JS-Controller, formerly `lib/tools.js` */
49exports.controllerToolsInternal = resolveControllerTools();
50// Export a subset of the utilties in controllerTools
51/**
52 * Resolve a module that is either exported by @iobroker/js-controller-common (new controllers) or located in in the controller's `lib` directory (old controllers).
53 * @param name - The filename of the module to resolve
54 * @param exportName - The name under which the module may be exported. Defaults to `name`.
55 */
56function resolveNamedModule(name, exportName = name) {
57 // The requested module might be moved to @iobroker/js-controller-common and exported from there
58 if (exports.controllerCommonModulesInternal === null || exports.controllerCommonModulesInternal === void 0 ? void 0 : exports.controllerCommonModulesInternal[exportName])
59 return exports.controllerCommonModulesInternal[exportName];
60 // Otherwise it was not moved yet, or we're dealing with JS-Controller <= 4.0
61 // Attempt 1: JS-Controller 4.1+
62 let importPath = path.join(utils.controllerDir, "build/lib", name);
63 try {
64 // This was a default export prior to the TS migration
65 const module = require(importPath);
66 if (module)
67 return module;
68 }
69 catch (_a) {
70 // did not work, continue
71 }
72 // Attempt 2: JS-Controller <= 4.0
73 importPath = path.join(utils.controllerDir, "lib", name);
74 try {
75 // This was a default export prior to the TS migration
76 const module = require(importPath);
77 if (module)
78 return module;
79 }
80 catch (_b) {
81 // did not work, continue
82 }
83 throw new Error(`Cannot resolve JS-Controller module ${name}.js`);
84 return process.exit(10);
85}
86exports.resolveNamedModule = resolveNamedModule;
87// TODO: Import types from @iobroker/js-controller-common and iobroker.js-controller
88/**
89 * Converts a pattern to match object IDs into a RegEx string that can be used in `new RegExp(...)`
90 * @param pattern The pattern to convert
91 */
92function pattern2RegEx(pattern) {
93 return exports.controllerToolsInternal.pattern2RegEx(pattern);
94}
95exports.commonTools = {
96 pattern2RegEx,
97 // TODO: Add more methods from lib/tools.js as needed
98 password: resolveNamedModule("password"),
99 letsEncrypt: resolveNamedModule("letsencrypt"),
100 session: resolveNamedModule("session"),
101 zipFiles: resolveNamedModule("zipFiles"),
102 // TODO: expose more (internal) controller modules as needed
103};