Read the statement by Michael Teeuw here.
Use .JSON as a module config value?
-
Hey there!
I am trying to automate a super small part of my MagicMirror which involves the MMM-PlexNowPlaying module.
Basically, I’m trying to take a JSON file from the filesystem, read it to a JS Object, then use that Object as the value for the
userNameFilter
key within theconfig
Object.
Code
This is the code I’ve made and added to my
config.js
to avoid writing an entire module lol:if (typeof module !== "undefined") { var plexUsers = require('/home/pi/mmConfig/user_list.json'); let obj = config.modules.find((o, i) => { if (o.module === "MMM-PlexNowPlaying") { config.modules[i].config.userNameFilter = plexUsers; return true; // stop searching } }); let test = config.modules.find(o => o.module === "MMM-PlexNowPlaying"); console.log(test); }
It’s located between
var config = {};
and
if (typeof module !== "undefined") {module.exports = config;}
Confusion
The two scenarios I have compared against each other are
- running the code above without
- commenting out
let obj = {};
and hardcoding the Object (the normal way)
And in both cases the output of
test
is the exact same. Yet the behavior of the Mirror doesn’t seem to reflect that. The standard usage of2
works as expected, yet1
doesAny and all help would be greatly appreciated!
-
@sdetweil Thanks for the info!
Ok, I see. That could definitely work.
I went ahead and scrapped the code from above and converted it to a bash script that is executed before the
cd ./MagicMirror
(pretty sure I’ve had this setup running since beforeinstallers/mm.sh
was actually provided lol). I admit the bash script is suuuper rough and was written with my symlinked config files in mind but it gets the job done!- makes an intermediate copy of
config.js
- grabs the content of the of the json
- declares variable containing config variable to be replaced
- prepends the variable string to the json content
- uses perl/regex to search for the variable in the intermediate file and replace it
- makes a backup of
config.js
just in case - moves intermediate copy to working version
#!/bin/bash cp /home/pi/mmConfig/config.js /home/pi/mmConfig/config.inter mapfile < /home/pi/mmConfig/user_list.json variableName="userNameFilter" replace="${variableName}: ${MAPFILE[@]}" perl -0777 -pi -e "s/${variableName}: {((.|\n)*?)}/${replace}/g" /home/pi/mmConfig/config.inter mv --backup=numbered /home/pi/mmConfig/config.js /home/pi/mmConfig/config_backups mv /home/pi/mmConfig/config.inter /home/pi/mmConfig/config.js
And that successfully allows for the data to be read in! Thank you!
Sadly, it did break
pm2
watching config (which kinda makes sense to avoid a loop) and if I’m modifying the config I’m already in the command line and apm2 restart mm
isn’t too bad I suppose ¯\_(ツ)_/¯Edit: I’m realizing I should add a check to make sure it doesn’t perform either
mv
if the file is the same. New to-do lol! - makes an intermediate copy of
-
@drippyer I don’t know about putting the code IN config.js,but you can add another step to the script that starts mm and create the updated config.js
installers/mm.sh is the normal start script
it does
#!/bin/bash # This file is still here to keep PM2 working on older installations. cd ~/MagicMirror # add your script here node xxx.js DISPLAY=:0 npm start
make your original config.js config.model
and merge your user info to get config.js -
@sdetweil Thanks for the info!
Ok, I see. That could definitely work.
I went ahead and scrapped the code from above and converted it to a bash script that is executed before the
cd ./MagicMirror
(pretty sure I’ve had this setup running since beforeinstallers/mm.sh
was actually provided lol). I admit the bash script is suuuper rough and was written with my symlinked config files in mind but it gets the job done!- makes an intermediate copy of
config.js
- grabs the content of the of the json
- declares variable containing config variable to be replaced
- prepends the variable string to the json content
- uses perl/regex to search for the variable in the intermediate file and replace it
- makes a backup of
config.js
just in case - moves intermediate copy to working version
#!/bin/bash cp /home/pi/mmConfig/config.js /home/pi/mmConfig/config.inter mapfile < /home/pi/mmConfig/user_list.json variableName="userNameFilter" replace="${variableName}: ${MAPFILE[@]}" perl -0777 -pi -e "s/${variableName}: {((.|\n)*?)}/${replace}/g" /home/pi/mmConfig/config.inter mv --backup=numbered /home/pi/mmConfig/config.js /home/pi/mmConfig/config_backups mv /home/pi/mmConfig/config.inter /home/pi/mmConfig/config.js
And that successfully allows for the data to be read in! Thank you!
Sadly, it did break
pm2
watching config (which kinda makes sense to avoid a loop) and if I’m modifying the config I’m already in the command line and apm2 restart mm
isn’t too bad I suppose ¯\_(ツ)_/¯Edit: I’m realizing I should add a check to make sure it doesn’t perform either
mv
if the file is the same. New to-do lol! - makes an intermediate copy of
-
@drippyer you could change the pm2 setup to not watch config.js
… -
@sdetweil I might have been a bit unclear haha. The break was pm2 no longer watching config.js, not a loop. My bad about that misunderstanding.
-
@drippyer got it…