MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.

    Use .JSON as a module config value?

    Scheduled Pinned Locked Moved Solved Troubleshooting
    6 Posts 2 Posters 493 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S Offline
      sdetweil @drippyer
      last edited by

      @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

      Sam

      How to add modules

      learning how to use browser developers window for css changes

      drippyerD 1 Reply Last reply Reply Quote 1
      • drippyerD Offline
        drippyer @sdetweil
        last edited by drippyer

        @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 before installers/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!

        1. makes an intermediate copy of config.js
        2. grabs the content of the of the json
        3. declares variable containing config variable to be replaced
        4. prepends the variable string to the json content
        5. uses perl/regex to search for the variable in the intermediate file and replace it
        6. makes a backup of config.js just in case
        7. 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 a pm2 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!

        S 1 Reply Last reply Reply Quote 0
        • S Offline
          sdetweil @drippyer
          last edited by

          @drippyer you could change the pm2 setup to not watch config.js
          …

          Sam

          How to add modules

          learning how to use browser developers window for css changes

          drippyerD 1 Reply Last reply Reply Quote 0
          • drippyerD Offline
            drippyer @sdetweil
            last edited by

            @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.

            S 1 Reply Last reply Reply Quote 0
            • S Offline
              sdetweil @drippyer
              last edited by

              @drippyer got it…

              Sam

              How to add modules

              learning how to use browser developers window for css changes

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              Enjoying MagicMirror? Please consider a donation!
              MagicMirror created by Michael Teeuw.
              Forum managed by Sam, technical setup by Karsten.
              This forum is using NodeBB as its core | Contributors
              Contact | Privacy Policy