Skip to content

Commit

Permalink
specify the adapter-core version as override in the root pack json (#…
Browse files Browse the repository at this point in the history
…2806)

* specify the adapter-core version as override in the root pack json

* added a bit of docs

* only do file write and install if necessary

* also skip on dev installations as these do not have a root pack json
  • Loading branch information
foxriver76 authored Jun 15, 2024
1 parent 8ba1447 commit 34d893b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 13 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ The ioBroker.js-controller is the heart of any ioBroker installation. The contro
* js-controller 1.x works with Node.js 4.x, 6.x, 8.x and probably 10.x (untested)

Please try to stay current with your Node.js version because the support is limited in time. As of now (May 2024) all Node.js versions below 18.x are no longer supported by Node.js and considered EOL (End Of Life).
To upgrade your Node.js version and ioBroker, please follow https://forum.iobroker.net/topic/44566/how-to-node-js-f%C3%BCr-iobroker-richtig-updaten-2021-edition !
To upgrade your Node.js version and ioBroker, please follow https://forum.iobroker.net/topic/44566/how-to-node-js-f%C3%BCr-iobroker-richtig-updaten-2021-edition!

As it is hard to keep a lot of the decentralized adapters up-to-date, up from controller version 6.0.4 the js-controller will override the `@iobroker/adapter-core` dependency of single adapters to ensure compatibility with the current js-controller version.

* js-controller 6.x (Kiera) specifies this version as `^3.1.6`.

## Links
* [Changelog](CHANGELOG.md)
Expand Down
64 changes: 58 additions & 6 deletions packages/cli/src/lib/setup/setupSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ export interface SetupCommandOptions {
}

export class Setup {
/** Object IDs which are not allowed to exist but could be generated due to errors in the past */
private readonly KNOWN_GARBAGE_OBJECT_IDS = ['null', 'undefined'];
/** Adapter core version supported by this js-controller */
private readonly SUPPORTED_ADAPTER_CORE_VERSION = '^3.1.6';
/** Default name for redis sentinels */
private readonly DEFAULT_SENTINEL_NAME = 'mymaster';
private readonly processExit: ProcessExitCallback;
private states: StatesRedisClient | undefined;
private objects: ObjectsRedisClient | undefined;
Expand Down Expand Up @@ -197,6 +202,12 @@ export class Setup {
await this._fixWindowsControllerJs();
}

try {
await this.addAdapterCoreRequirement();
} catch (e) {
console.error(`Could not add "@iobroker/adapter-core" requirement: ${e.message}`);
}

await this._cleanupInstallation();

// special methods which are only there on objects server
Expand Down Expand Up @@ -703,7 +714,9 @@ Please DO NOT copy files manually into ioBroker storage directories!`
if (Array.isArray(originalConfig.objects.host)) {
console.log(
` - Sentinel-Master-Name: ${
originalConfig.objects.sentinelName ? originalConfig.objects.sentinelName : 'mymaster'
originalConfig.objects.sentinelName
? originalConfig.objects.sentinelName
: this.DEFAULT_SENTINEL_NAME
}`
);
}
Expand All @@ -714,7 +727,7 @@ Please DO NOT copy files manually into ioBroker storage directories!`
if (Array.isArray(originalConfig.states.host)) {
console.log(
` - Sentinel-Master-Name: ${
originalConfig.states.sentinelName ? originalConfig.states.sentinelName : 'mymaster'
originalConfig.states.sentinelName ? originalConfig.states.sentinelName : this.DEFAULT_SENTINEL_NAME
}`
);
}
Expand Down Expand Up @@ -836,7 +849,7 @@ Please DO NOT copy files manually into ioBroker storage directories!`
if (oSentinel) {
const defaultSentinelName = originalConfig.objects.sentinelName
? originalConfig.objects.sentinelName
: 'mymaster';
: this.DEFAULT_SENTINEL_NAME;
oSentinelName = rl.question(`Objects Redis Sentinel Master Name [${defaultSentinelName}]: `, {
defaultInput: defaultSentinelName
});
Expand Down Expand Up @@ -963,7 +976,7 @@ Please DO NOT copy files manually into ioBroker storage directories!`
? originalConfig.states.sentinelName
: oSentinelName && oPort === sPort
? oSentinelName
: 'mymaster';
: this.DEFAULT_SENTINEL_NAME;
sSentinelName = rl.question(`States Redis Sentinel Master Name [${defaultSentinelName}]: `, {
defaultInput: defaultSentinelName
});
Expand Down Expand Up @@ -1032,10 +1045,20 @@ Please DO NOT copy files manually into ioBroker storage directories!`
if (dir) {
config.states.dataDir = dir;
}
if (config.objects.type === 'redis' && oSentinel && oSentinelName && oSentinelName !== 'mymaster') {
if (
config.objects.type === 'redis' &&
oSentinel &&
oSentinelName &&
oSentinelName !== this.DEFAULT_SENTINEL_NAME
) {
config.objects.sentinelName = oSentinelName;
}
if (config.states.type === 'redis' && sSentinel && sSentinelName && sSentinelName !== 'mymaster') {
if (
config.states.type === 'redis' &&
sSentinel &&
sSentinelName &&
sSentinelName !== this.DEFAULT_SENTINEL_NAME
) {
config.states.sentinelName = sSentinelName;
}

Expand Down Expand Up @@ -1085,6 +1108,35 @@ Please DO NOT copy files manually into ioBroker storage directories!`
}
}

/**
* Add adapter-core in supported version in the overrides field of the root package.json and call install there to apply it
*/
private async addAdapterCoreRequirement(): Promise<void> {
if (tools.isDevInstallation()) {
return;
}

const rootDir = tools.getRootDir();
const packPath = path.join(rootDir, 'package.json');
const packJson = await fs.readJson(packPath);

if (packJson.overrides?.['@iobroker/adapter-core'] === this.SUPPORTED_ADAPTER_CORE_VERSION) {
console.log(
`The supported version of "@iobroker/adapter-core" is already specified as "${this.SUPPORTED_ADAPTER_CORE_VERSION}"`
);
return;
}

packJson.overrides = { '@iobroker/adapter-core': this.SUPPORTED_ADAPTER_CORE_VERSION };

await fs.writeFile(packPath, JSON.stringify(packJson));
await tools.execAsync('npm i --omit=dev', { cwd: rootDir });

console.log(
`Successfully specified supported "@iobroker/adapter-core" version as "${this.SUPPORTED_ADAPTER_CORE_VERSION}"`
);
}

/**
* Create the adapters object per host if not yet existing
*/
Expand Down
12 changes: 6 additions & 6 deletions packages/common-db/src/lib/common/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ export async function isHostRunning(objects: any, states: any): Promise<boolean>
/**
* Checks if ioBroker is installed in a dev environment
*/
function _isDevInstallation(): boolean {
export function isDevInstallation(): boolean {
return fs.pathExistsSync(`${getControllerDir()}/../../packages/controller`);
}

Expand All @@ -345,7 +345,7 @@ type AppName = 'iobroker' | 'ioBroker';
* Get the app name either for prod or for dev installation
*/
function getAppName(): AppName {
if (_isDevInstallation()) {
if (isDevInstallation()) {
// dev install - GitHub folder is uppercase
return 'ioBroker';
}
Expand Down Expand Up @@ -2220,7 +2220,7 @@ export function getDefaultDataDir(): string {
return envDataDir;
}

if (_isDevInstallation()) {
if (isDevInstallation()) {
// dev install
return './data/';
}
Expand All @@ -2243,9 +2243,9 @@ export function getConfigFileName(): string {

const controllerDir = getControllerDir();
const fallbackConfigFile = path.join(controllerDir, 'data', `${appNameLowerCase}.json`);
const isDevInstallation = _isDevInstallation();
const isDevInstall = isDevInstallation();

if (isDevInstallation) {
if (isDevInstall) {
const devConfigFile = path.join(controllerDir, 'conf', `${appNameLowerCase}.json`);

if (fs.existsSync(devConfigFile)) {
Expand All @@ -2257,7 +2257,7 @@ export function getConfigFileName(): string {

const prodConfigFile = path.join(getRootDir(), `${appNameLowerCase}-data`, `${appNameLowerCase}.json`);

if (!fs.existsSync(prodConfigFile) && isDevInstallation) {
if (!fs.existsSync(prodConfigFile) && isDevInstall) {
return fallbackConfigFile;
}

Expand Down

0 comments on commit 34d893b

Please sign in to comment.