Skip to content

Commit

Permalink
Optimize config resolution (#2452)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
foxriver76 authored Sep 25, 2023
1 parent ad519c8 commit c590b2a
Showing 1 changed file with 12 additions and 42 deletions.
54 changes: 12 additions & 42 deletions packages/common/src/lib/common/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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>)[]): 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<void> {
for (const key of keys) {
try {
Expand Down

0 comments on commit c590b2a

Please sign in to comment.