|
| 1 | +"use strict"; |
| 2 | +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { |
| 3 | + if (k2 === undefined) k2 = k; |
| 4 | + var desc = Object.getOwnPropertyDescriptor(m, k); |
| 5 | + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { |
| 6 | + desc = { enumerable: true, get: function() { return m[k]; } }; |
| 7 | + } |
| 8 | + Object.defineProperty(o, k2, desc); |
| 9 | +}) : (function(o, m, k, k2) { |
| 10 | + if (k2 === undefined) k2 = k; |
| 11 | + o[k2] = m[k]; |
| 12 | +})); |
| 13 | +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { |
| 14 | + Object.defineProperty(o, "default", { enumerable: true, value: v }); |
| 15 | +}) : function(o, v) { |
| 16 | + o["default"] = v; |
| 17 | +}); |
| 18 | +var __importStar = (this && this.__importStar) || (function () { |
| 19 | + var ownKeys = function(o) { |
| 20 | + ownKeys = Object.getOwnPropertyNames || function (o) { |
| 21 | + var ar = []; |
| 22 | + for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; |
| 23 | + return ar; |
| 24 | + }; |
| 25 | + return ownKeys(o); |
| 26 | + }; |
| 27 | + return function (mod) { |
| 28 | + if (mod && mod.__esModule) return mod; |
| 29 | + var result = {}; |
| 30 | + if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); |
| 31 | + __setModuleDefault(result, mod); |
| 32 | + return result; |
| 33 | + }; |
| 34 | +})(); |
| 35 | +var __importDefault = (this && this.__importDefault) || function (mod) { |
| 36 | + return (mod && mod.__esModule) ? mod : { "default": mod }; |
| 37 | +}; |
| 38 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 39 | +exports.loadNpmPackage = loadNpmPackage; |
| 40 | +exports.loadIoPackage = loadIoPackage; |
| 41 | +exports.getAdapterExecutionMode = getAdapterExecutionMode; |
| 42 | +exports.locateAdapterMainFile = locateAdapterMainFile; |
| 43 | +exports.loadAdapterConfig = loadAdapterConfig; |
| 44 | +exports.loadAdapterCommon = loadAdapterCommon; |
| 45 | +exports.loadInstanceObjects = loadInstanceObjects; |
| 46 | +exports.getAppName = getAppName; |
| 47 | +exports.getAdapterName = getAdapterName; |
| 48 | +exports.getAdapterFullName = getAdapterFullName; |
| 49 | +exports.getAdapterDependencies = getAdapterDependencies; |
| 50 | +// Add debug logging for tests |
| 51 | +const typeguards_1 = require("alcalzone-shared/typeguards"); |
| 52 | +const debug_1 = __importDefault(require("debug")); |
| 53 | +const fs_extra_1 = require("fs-extra"); |
| 54 | +const path = __importStar(require("path")); |
| 55 | +const debug = (0, debug_1.default)('testing:unit:adapterTools'); |
| 56 | +/** |
| 57 | + * Loads an adapter's package.json |
| 58 | + * |
| 59 | + * @param adapterDir The directory the adapter resides in |
| 60 | + */ |
| 61 | +function loadNpmPackage(adapterDir) { |
| 62 | + return require(path.join(adapterDir, 'package.json')); |
| 63 | +} |
| 64 | +/** |
| 65 | + * Loads an adapter's io-package.json |
| 66 | + * |
| 67 | + * @param adapterDir The directory the adapter resides in |
| 68 | + */ |
| 69 | +function loadIoPackage(adapterDir) { |
| 70 | + return require(path.join(adapterDir, 'io-package.json')); |
| 71 | +} |
| 72 | +function getAdapterExecutionMode(adapterDir) { |
| 73 | + const ioPackage = loadIoPackage(adapterDir); |
| 74 | + return ioPackage.common.mode; |
| 75 | +} |
| 76 | +/** |
| 77 | + * Locates an adapter's main file |
| 78 | + * |
| 79 | + * @param adapterDir The directory the adapter resides in |
| 80 | + */ |
| 81 | +async function locateAdapterMainFile(adapterDir) { |
| 82 | + debug(`locating adapter main file in ${adapterDir}...`); |
| 83 | + const ioPackage = loadIoPackage(adapterDir); |
| 84 | + const npmPackage = loadNpmPackage(adapterDir); |
| 85 | + // First look for the file defined in io-package.json or package.json or use "main.js" as a fallback |
| 86 | + const mainFile = typeof ioPackage.common.main === 'string' |
| 87 | + ? ioPackage.common.main |
| 88 | + : typeof npmPackage.main === 'string' |
| 89 | + ? npmPackage.main |
| 90 | + : 'main.js'; |
| 91 | + let ret = path.join(adapterDir, mainFile); |
| 92 | + debug(` => trying ${ret}`); |
| 93 | + if (await (0, fs_extra_1.pathExists)(ret)) { |
| 94 | + debug(` => found ${mainFile}`); |
| 95 | + return ret; |
| 96 | + } |
| 97 | + // If the specified file doesn't exist and ends with .js, try the .ts equivalent |
| 98 | + if (mainFile.endsWith('.js')) { |
| 99 | + const tsFile = mainFile.replace(/\.js$/, '.ts'); |
| 100 | + ret = path.join(adapterDir, tsFile); |
| 101 | + debug(` => trying ${ret}`); |
| 102 | + if (await (0, fs_extra_1.pathExists)(ret)) { |
| 103 | + debug(` => found ${tsFile}`); |
| 104 | + return ret; |
| 105 | + } |
| 106 | + } |
| 107 | + // If both don't exist, JS-Controller uses <adapter name>.js as another fallback |
| 108 | + ret = path.join(adapterDir, `${ioPackage.common.name}.js`); |
| 109 | + debug(` => trying ${ret}`); |
| 110 | + if (await (0, fs_extra_1.pathExists)(ret)) { |
| 111 | + debug(` => found ${ioPackage.common.name}.js`); |
| 112 | + return ret; |
| 113 | + } |
| 114 | + // Also try <adapter name>.ts as a fallback |
| 115 | + ret = path.join(adapterDir, `${ioPackage.common.name}.ts`); |
| 116 | + debug(` => trying ${ret}`); |
| 117 | + if (await (0, fs_extra_1.pathExists)(ret)) { |
| 118 | + debug(` => found ${ioPackage.common.name}.ts`); |
| 119 | + return ret; |
| 120 | + } |
| 121 | + throw new Error(`The adapter main file was not found in ${adapterDir}`); |
| 122 | +} |
| 123 | +/** |
| 124 | + * Locates an adapter's config to populate the `adapter.config` object with |
| 125 | + * |
| 126 | + * @param adapterDir The directory the adapter resides in |
| 127 | + */ |
| 128 | +function loadAdapterConfig(adapterDir) { |
| 129 | + const ioPackage = loadIoPackage(adapterDir); |
| 130 | + return ioPackage.native || {}; |
| 131 | +} |
| 132 | +/** |
| 133 | + * Loads the adapter's common configuration from `io-package.json` |
| 134 | + * |
| 135 | + * @param adapterDir The directory the adapter resides in |
| 136 | + */ |
| 137 | +function loadAdapterCommon(adapterDir) { |
| 138 | + const ioPackage = loadIoPackage(adapterDir); |
| 139 | + return ioPackage.common || {}; |
| 140 | +} |
| 141 | +/** |
| 142 | + * Loads the instanceObjects for an adapter from its `io-package.json` |
| 143 | + * |
| 144 | + * @param adapterDir The directory the adapter resides in |
| 145 | + */ |
| 146 | +function loadInstanceObjects(adapterDir) { |
| 147 | + const ioPackage = loadIoPackage(adapterDir); |
| 148 | + return ioPackage.instanceObjects || []; |
| 149 | +} |
| 150 | +/** Returns the branded name of "iobroker" */ |
| 151 | +function getAppName(adapterDir) { |
| 152 | + const npmPackage = loadNpmPackage(adapterDir); |
| 153 | + return npmPackage.name.split('.')[0] || 'iobroker'; |
| 154 | +} |
| 155 | +/** Returns the name of an adapter without the prefix */ |
| 156 | +function getAdapterName(adapterDir) { |
| 157 | + const ioPackage = loadIoPackage(adapterDir); |
| 158 | + return ioPackage.common.name; |
| 159 | +} |
| 160 | +/** Returns the full name of an adapter, including the prefix */ |
| 161 | +function getAdapterFullName(adapterDir) { |
| 162 | + const npmPackage = loadNpmPackage(adapterDir); |
| 163 | + return npmPackage.name; |
| 164 | +} |
| 165 | +/** Reads other ioBroker modules this adapter depends on from io-package.json */ |
| 166 | +function getAdapterDependencies(adapterDir) { |
| 167 | + const ioPackage = loadIoPackage(adapterDir); |
| 168 | + const ret = {}; |
| 169 | + if ((0, typeguards_1.isArray)(ioPackage.common.dependencies)) { |
| 170 | + for (const dep of ioPackage.common.dependencies) { |
| 171 | + if (typeof dep === 'string') { |
| 172 | + ret[dep] = 'latest'; |
| 173 | + } |
| 174 | + else if ((0, typeguards_1.isObject)(dep)) { |
| 175 | + const key = Object.keys(dep)[0]; |
| 176 | + if (key) { |
| 177 | + ret[key] = dep[key] || 'latest'; |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + return ret; |
| 183 | +} |
0 commit comments