From c590b2a5a6d0f54b4d78ce5c72deb759986dfc2c Mon Sep 17 00:00:00 2001 From: Max Hauser Date: Mon, 25 Sep 2023 21:03:46 +0200 Subject: [PATCH] Optimize config resolution (#2452) * log the id of the binary state in the deprecation message * optimize config file name resolution * also improve readability of dev installatiob config determination * restore last fallback logic --- packages/common/src/lib/common/tools.ts | 54 ++++++------------------- 1 file changed, 12 insertions(+), 42 deletions(-) diff --git a/packages/common/src/lib/common/tools.ts b/packages/common/src/lib/common/tools.ts index 4450b1359c..703d979796 100644 --- a/packages/common/src/lib/common/tools.ts +++ b/packages/common/src/lib/common/tools.ts @@ -2257,45 +2257,26 @@ export function getConfigFileName(): string { return path.join(envDataDir, `${appNameLowerCase}.json`); } - let devConfigDir; + const controllerDir = getControllerDir(); + const fallbackConfigFile = path.join(controllerDir, 'data', `${appNameLowerCase}.json`); if (_isDevInstallation()) { - devConfigDir = __dirname.replace(/\\/g, '/'); - const devConfigParts = devConfigDir.split('/'); + const devConfigFile = path.join(controllerDir, 'conf', `${appNameLowerCase}.json`); - // dev install -> Remove /lib - devConfigParts.splice(devConfigParts.length - 4, 4); - devConfigDir = devConfigParts.join('/'); - devConfigDir += '/controller'; // go inside controller dir - if (fs.existsSync(`${devConfigDir}/conf/${appNameLowerCase}.json`)) { - return `${devConfigDir}/conf/${appNameLowerCase}.json`; - } else if (fs.existsSync(`${devConfigDir}/data/${appNameLowerCase}.json`)) { - return `${devConfigDir}/data/${appNameLowerCase}.json`; + if (fs.existsSync(devConfigFile)) { + return devConfigFile; + } else if (fs.existsSync(fallbackConfigFile)) { + return fallbackConfigFile; } } - let configDir = __dirname.replace(/\\/g, '/'); - const configParts = configDir.split('/'); + const prodConfigFile = path.join(getRootDir(), `${appNameLowerCase}-data`, `${appNameLowerCase}.json`); - // if debugging with npm5 -> node_modules on e.g., /opt/node_modules - if ( - fs.existsSync(`${__dirname}/../../../../../../../../node_modules/${appNameLowerCase}.js-controller`) || - fs.existsSync(`${__dirname}/../../../../../../../../node_modules/${appName}.js-controller`) - ) { - // remove /node_modules/' + appName + '.js-controller/lib - configParts.splice(configParts.length - 8, 8); - configDir = configParts.join('/'); - } else { - // If installed with npm -> remove node_modules/@iobroker/js-controller-common/src/lib/common - configParts.splice(configParts.length - 6, 6); - configDir = configParts.join('/'); - } - - if (!fs.existsSync(`${configDir}/${appNameLowerCase}-data/${appNameLowerCase}.json`) && devConfigDir) { - return `${devConfigDir}/data/${appNameLowerCase}.json`; + if (!fs.existsSync(prodConfigFile)) { + return fallbackConfigFile; } - return `${configDir}/${appNameLowerCase}-data/${appNameLowerCase}.json`; + return prodConfigFile; } /** @@ -2378,7 +2359,7 @@ export function promisify( * @param context (optional) The context (value of `this` to bind the function to) * @param returnArgNames (optional) If the callback contains multiple arguments, * you can combine them into one object by passing the names as an array. - * Otherwise the Promise will resolve with an array + * Otherwise, the Promise will resolve with an array */ export function promisifyNoError( fn: (...args: any[]) => void, @@ -2426,17 +2407,6 @@ export function promisifyNoError( }; } -/** - * Creates and executes an array of promises in sequence - * @param promiseFactories An array of promise-returning functions - */ -export function promiseSequence(promiseFactories: ((...args: any[]) => Promise)[]): any[] { - /** @ts-expect-error don't want to touch now */ - return promiseFactories.reduce((promise, factory) => { - return promise.then(result => factory().then(Array.prototype.concat.bind(result))); - }, Promise.resolve([])); -} - async function _setQualityForStates(states: any, keys: string[], quality: number): Promise { for (const key of keys) { try {