Skip to content

Commit

Permalink
Add getAdapterDir, getInstalledInfo and getHostName to the exposed me…
Browse files Browse the repository at this point in the history
…thods from tools (#513)

* * Add getAdapterDir, getInstalledInfo and getHostName to the exposed commonTools methods
* exclude .idea directory

* * add build

* * incorporate review feedback
  • Loading branch information
Apollon77 committed Oct 5, 2022
1 parent 9bc4137 commit c20a6d1
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/
node_modules/
iobroker.*.tgz
Thumbs.db
Expand Down
49 changes: 49 additions & 0 deletions build/controllerTools.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,59 @@ export declare function resolveNamedModule(name: string, exportName?: string): a
/**
* Converts a pattern to match object IDs into a RegEx string that can be used in `new RegExp(...)`
* @param pattern The pattern to convert
* @returns The RegEx string
*/
declare function pattern2RegEx(pattern: string): string;
/**
* Finds the adapter directory of a given adapter
*
* @param adapter name of the adapter, e.g. hm-rpc
* @returns path to adapter directory or null if no directory found
*/
declare function getAdapterDir(adapter: string): string | null;
interface Multilingual {
en: string;
de?: string;
ru?: string;
pt?: string;
nl?: string;
fr?: string;
it?: string;
es?: string;
pl?: string;
uk?: string;
"zh-cn"?: string;
}
export interface GetInstalledInfoReponse {
controller?: boolean;
version?: string;
icon?: string;
title?: string;
titleLang?: Multilingual;
desc?: Multilingual;
platform?: string;
keywords?: string[];
readme?: string;
runningVersion?: string;
license?: string;
licenseUrl?: string;
}
/**
* Get list of all installed adapters and controller version on this host
* @param hostJsControllerVersion Version of the running js-controller, will be included in the returned information if provided
* @returns object containing information about installed host
*/
declare function getInstalledInfo(hostJsControllerVersion?: string): GetInstalledInfoReponse;
/**
* Returns the hostname of this host
* @returns hostname
*/
declare function getHostName(): string;
export declare const commonTools: {
pattern2RegEx: typeof pattern2RegEx;
getAdapterDir: typeof getAdapterDir;
getInstalledInfo: typeof getInstalledInfo;
getHostName: typeof getHostName;
password: any;
letsEncrypt: any;
session: any;
Expand Down
32 changes: 30 additions & 2 deletions build/controllerTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function resolveControllerTools() {
// did not work, continue
}
throw new Error("Cannot resolve tools module");
return process.exit(10);
//return process.exit(10);
}
/** The collection of utility functions in JS-Controller, formerly `lib/tools.js` */
exports.controllerToolsInternal = resolveControllerTools();
Expand Down Expand Up @@ -81,19 +81,47 @@ function resolveNamedModule(name, exportName = name) {
// did not work, continue
}
throw new Error(`Cannot resolve JS-Controller module ${name}.js`);
return process.exit(10);
//return process.exit(10);
}
exports.resolveNamedModule = resolveNamedModule;
// TODO: Import types from @iobroker/js-controller-common and iobroker.js-controller
/**
* Converts a pattern to match object IDs into a RegEx string that can be used in `new RegExp(...)`
* @param pattern The pattern to convert
* @returns The RegEx string
*/
function pattern2RegEx(pattern) {
return exports.controllerToolsInternal.pattern2RegEx(pattern);
}
/**
* Finds the adapter directory of a given adapter
*
* @param adapter name of the adapter, e.g. hm-rpc
* @returns path to adapter directory or null if no directory found
*/
function getAdapterDir(adapter) {
return exports.controllerToolsInternal.getAdapterDir(adapter);
}
/**
* Get list of all installed adapters and controller version on this host
* @param hostJsControllerVersion Version of the running js-controller, will be included in the returned information if provided
* @returns object containing information about installed host
*/
function getInstalledInfo(hostJsControllerVersion) {
return exports.controllerToolsInternal.getInstalledInfo(hostJsControllerVersion);
}
/**
* Returns the hostname of this host
* @returns hostname
*/
function getHostName() {
return exports.controllerToolsInternal.getHostName();
}
exports.commonTools = {
pattern2RegEx,
getAdapterDir,
getInstalledInfo,
getHostName,
// TODO: Add more methods from lib/tools.js as needed
password: resolveNamedModule("password"),
letsEncrypt: resolveNamedModule("letsencrypt"),
Expand Down
67 changes: 65 additions & 2 deletions src/controllerTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function resolveControllerTools(): any | never {
}

throw new Error("Cannot resolve tools module");
return process.exit(10);
//return process.exit(10);
}

/** The collection of utility functions in JS-Controller, formerly `lib/tools.js` */
Expand Down Expand Up @@ -88,21 +88,84 @@ export function resolveNamedModule(
}

throw new Error(`Cannot resolve JS-Controller module ${name}.js`);
return process.exit(10);
//return process.exit(10);
}

// TODO: Import types from @iobroker/js-controller-common and iobroker.js-controller

/**
* Converts a pattern to match object IDs into a RegEx string that can be used in `new RegExp(...)`
* @param pattern The pattern to convert
* @returns The RegEx string
*/
function pattern2RegEx(pattern: string): string {
return controllerToolsInternal.pattern2RegEx(pattern);
}

/**
* Finds the adapter directory of a given adapter
*
* @param adapter name of the adapter, e.g. hm-rpc
* @returns path to adapter directory or null if no directory found
*/
function getAdapterDir(adapter: string): string | null {
return controllerToolsInternal.getAdapterDir(adapter);
}

// Types copied from https://github.com/ioBroker/ioBroker.js-controller/blob/master/packages/common/src/lib/common/tools.ts#L898-L924
// TODO: Import types from @iobroker/js-controller-common
interface Multilingual {
en: string;
de?: string;
ru?: string;
pt?: string;
nl?: string;
fr?: string;
it?: string;
es?: string;
pl?: string;
uk?: string;
"zh-cn"?: string;
}

export interface GetInstalledInfoReponse {
controller?: boolean;
version?: string;
icon?: string;
title?: string;
titleLang?: Multilingual;
desc?: Multilingual;
platform?: string;
keywords?: string[];
readme?: string;
runningVersion?: string;
license?: string;
licenseUrl?: string;
}
/**
* Get list of all installed adapters and controller version on this host
* @param hostJsControllerVersion Version of the running js-controller, will be included in the returned information if provided
* @returns object containing information about installed host
*/
function getInstalledInfo(
hostJsControllerVersion?: string,
): GetInstalledInfoReponse {
return controllerToolsInternal.getInstalledInfo(hostJsControllerVersion);
}

/**
* Returns the hostname of this host
* @returns hostname
*/
function getHostName(): string {
return controllerToolsInternal.getHostName();
}

export const commonTools = {
pattern2RegEx,
getAdapterDir,
getInstalledInfo,
getHostName,
// TODO: Add more methods from lib/tools.js as needed

password: resolveNamedModule("password"),
Expand Down

0 comments on commit c20a6d1

Please sign in to comment.