Read the statement by Michael Teeuw here.
Storing variables to be ignored by git in a js file?
-
Hey all. I have my own smart mirror and I recently created one for my girlfriend.
I’ve set it up so both mine and hers are clones of my forked repo of the MitchMitch MagicMirror repo. I want to be able to make changes on mine and then let her pull those changes so she doesn’t have to mess with the files herself.
The only issue is there are some things specific to our own mirrors, like the latitude and longitude for weather, or the link to our google calendars. The solution I came up with is to create another config file which I’ll add to .gitignore,
// config/variables_config.js const variables = { lat: 40.0000, lon: -70.0000 }; export { variables };So I should be able to use these variables in my usual config.js file, right?
// config/config.js import { variables } from 'variables_config.js' ... { module: "weather", config: { lat: variables.lat, lon: variables.lon } }Adding the import statement breaks the config file though. Running
npm run config:checkreturnedParsing error: 'import' and 'export' may appear only with 'sourceType: module'I need config.js to be a module I guess? Sorry, I’m a programmer but have almost zero javascript experience, so I need some guidance as to how to fix this or what the better solution is here. Thank you!
-
you could use
envsubst < config/config.js.template > config/config.js.The
config.js.templateis the config for both which contains variables for the different values. So you could write a script which sets the variables for user1 and then runenvsubstfor config1 and the same for user2/config2 afterwards. -
@timogden no straight forward way to do that.
you can write a script to merge your two property files into THE config.js and then launch mm
put the script in front of the npm start in installers/mm sh
which is used by pm2 to launch mm
as u noted, import doesn’t work. config.js is loaded into the browser which doesn’t support require or import
-
@sdetweil Could I import a json file into the config.js file instead? Would that be more straightforward?
-
@timogden no…
-
but u can write a quick bash script that can read the variables and replacements from a file and use sed (stream editor) to apply them
https://unix.stackexchange.com/questions/268640/make-multiple-edits-with-a-single-call-to-sed
use a config.model.js which is the one with the funky names to change,
cp that to config.js
loop thru the var list to make the command line, then run itand exit
then launch mm -
@sdetweil okay, thanks a lot for the reference material. I’ll work on that and post my solution when I finish. Thanks!
-
@timogden u can look at some of my scripts which get a list from a command then loop
see… restore which reads a file of module urls
-
you could use
envsubst < config/config.js.template > config/config.js.The
config.js.templateis the config for both which contains variables for the different values. So you could write a script which sets the variables for user1 and then runenvsubstfor config1 and the same for user2/config2 afterwards. -
@karsten13 same as I suggested different tool
but envsubst needs it’s values in the environment variables… so another hidden thing (to me)
-
only wanted to post this variant …
As background: This is a feature of my docker container where you can use a
config.js.templateand the variables are defined in thedocker-compose.ymlwhich starts the container and before starting mm I runenvsubstto make the realconfig.js. -
@timogden I’ve created a module to extract the config so you don’t need to maintain your own whole MM fork: https://forum.magicmirror.builders/topic/16224/mmm-personalconfig-persist-personal-config-to-github/1
A way to achieve what you want could be to create your own copy of personal config with your mirror config. Then make a clone of if for your friend where you do the specific changes. Changes to your repo then can be pulled to the other one via Pull requests.
In combination with MMM-UpdateNotification with can pull updates automatically, your friend won’t need to do anything. -
@sdetweil Ok, implementing @sdetweil 's suggestions, I have created a working version. This is my workflow.
// config/dictionary.txt // This is a list of key-value or string-replacement pairs "{latitude}" : "40" "{longitude}" : "-70"// installers/copy-values.sh // sed command found here: // https://unix.stackexchange.com/questions/269368/string-replacement-using-a-dictionary cd ~/MagicMirror cp config/config.model.js config/config.js sed ' s|"\(.*\)"[[:blank:]]*:[[:blank:]]*"\(.*\)"|\1\ \2| h s|.*\n|| s|[\&/]|\\&|g x s|\n.*|| s|[[\.*^$/]|\\&|g G s|\(.*\)\n\(.*\)|s/\1/\2/g| ' config/dictionary.txt | sed -i -f - config/config.js DISPLAY=:0 npm startNow I can have separate dictionary.txt files on both machines for any local important information, which I will add to .gitignore. I will continue to modify config.model.js, and config.model.js will be copied and cleaned up with all the right values before every start of the mirror.
-
@timogden Also, I should add that there is nothing fancy about the strings on the left-hand side of my dictionary.txt. That’s just how I decided to distinigush them. This is how I would write my config.model.js file now:
// config/config.model.js ... config: { lat: {latitude}, lon: {longitude}, apiKey: {API_KEY}, } ... -
@timogden cool… I would just use them where u need them in any module in the template.
and then replace all…
also I would use the quoted on the left (and in the config.js template)
and the replace with or without as appropriate (so u can replace numbers without and text with quotes)using the quotes means u can run npm run config:check and not get errors
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login