Read the statement by Michael Teeuw here.
Getting module's URL from node_helper before socket connected
-
Is there any convenient way to get the related/launched MM module’s basic URL from node_helper?
In the module itself, it has.file()
can be used. But in node_helper, there is no equivalent..path
would return the local path for the module, not the URL.Of course, I might be able to calculate or assume the URL from the path and name, but that way looks not so elegant. Default core modules will have
.../modules/default/moduleName
but 3rd party would have.../modules/moduleName
, I need to distinguish the cases.Or the module could send that info to node_helper through socketNotification. But I need that URL in the early initializing stage even when the socket is not connected yet.
.file()
or.url()
method of node_helper would be helpful for me. -
@MMRIZE why do u need the url before socket connected.
nothing ‘needs’ to be done before then. the helpers JOB is to service the module. if it needs config info, then it can’t start is job til notification w that info.
as u wait for the calendar broadcast, your module has eons of time waiting.
-
@MMRIZE also, u know that mm’s web server is routed at ~/MagicMirror
so __dirname with give u path to this file, strip of everything before /modules
back up one folder from this modules location and u know the root of where modules are located (mroot)
and mroot/default/defaultmodules.js
will give u the list of modules in defaults(that don’t use explicit name ‘default/modulename’) -
Is there any convenient way to get the related/launched MM module’s basic URL from node_helper?
AFAIS not really …
You could iterate over the modules in the
config.js
doing something like this in theinit()
of your node_helper:const configFilename = path.resolve( global.configuration_file || `${global.root_path}/config/config.js` ); let config; try { fs.accessSync(configFilename, fs.F_OK); const c = require(configFilename); config = Object.assign(c); } catch (e) { Log.error(name + ": Couldn't read config.js"); return; } let modules = []; for (const module of config.modules) { if ( !modules.includes(module.module) && !module.disabled ) { console.dir(module.module); } }
So you have the names and for knowing what are default modules you could use
modules/default/defaultmodules.js
-
@karsten13 wow!!!.. i didn’t think config was available to node_helper…
-
@sdetweil Yes I send my config to node_helper in my weather module :)
-
@cowboysdude said in Getting module's URL from node_helper before socket connected:
Yes I send my config to node_helper in my weather module
but that is not the topic
without doing ANYTHING
in the node_helper
config points to the TOTAL config/config.js
ALL modules and ALL (config.js) parms to ALL modules…this is NOT the merged with defaults view that you would send…
-
Assuming URL with PATH might be an almost general solution, but Sometimes, People install modules in weird ways like using Symlink. (That way could have some benefit, anyway, this is out of issues) In that case, assuming is somehow picky.
My purpose is to rewrite the
Translator
class to use in Module and node_helper together with one common class. (So that is the reason why pre-connection is needed.)To access translations in both layers, there could be many ways to achieve. However, if the exact URL could be obtained in the background layer, it would be easier and simpler and with the least dependency. (To work on browser and on nodejs layer together)Of course, there could be many alternative approaches. I’m just curious if there could be a better way.