• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
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.

Storing variables to be ignored by git in a js file?

Scheduled Pinned Locked Moved Solved Troubleshooting
14 Posts 4 Posters 2.8k Views 5 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.
  • T Offline
    TimOgden
    last edited by Jan 26, 2022, 8:15 PM

    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:check returned Parsing 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!

    S C 2 Replies Last reply Jan 26, 2022, 9:56 PM Reply Quote 0
    • K Offline
      karsten13 @TimOgden
      last edited by Jan 26, 2022, 10:19 PM

      @timogden

      you could use envsubst < config/config.js.template > config/config.js.

      The config.js.template is 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 run envsubst for config1 and the same for user2/config2 afterwards.

      S 1 Reply Last reply Jan 26, 2022, 10:26 PM Reply Quote 1
      • S Away
        sdetweil @TimOgden
        last edited by sdetweil Jan 26, 2022, 9:57 PM Jan 26, 2022, 9:56 PM

        @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

        Sam

        How to add modules

        learning how to use browser developers window for css changes

        T 2 Replies Last reply Jan 26, 2022, 9:57 PM Reply Quote 1
        • T Offline
          TimOgden @sdetweil
          last edited by Jan 26, 2022, 9:57 PM

          @sdetweil Could I import a json file into the config.js file instead? Would that be more straightforward?

          S K 3 Replies Last reply Jan 26, 2022, 9:59 PM Reply Quote 0
          • S Away
            sdetweil @TimOgden
            last edited by Jan 26, 2022, 9:59 PM

            @timogden no…

            Sam

            How to add modules

            learning how to use browser developers window for css changes

            1 Reply Last reply Reply Quote 0
            • S Away
              sdetweil @TimOgden
              last edited by sdetweil Jan 26, 2022, 10:15 PM Jan 26, 2022, 10:13 PM

              @timogden

              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 it

              and exit
              then launch mm

              Sam

              How to add modules

              learning how to use browser developers window for css changes

              T 1 Reply Last reply Jan 26, 2022, 10:15 PM Reply Quote 1
              • T Offline
                TimOgden @sdetweil
                last edited by Jan 26, 2022, 10:15 PM

                @sdetweil okay, thanks a lot for the reference material. I’ll work on that and post my solution when I finish. Thanks!

                S 1 Reply Last reply Jan 26, 2022, 10:18 PM Reply Quote 0
                • S Away
                  sdetweil @TimOgden
                  last edited by Jan 26, 2022, 10:18 PM

                  @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

                  https://github.com/sdetweil/MagicMirror-backup-restore

                  Sam

                  How to add modules

                  learning how to use browser developers window for css changes

                  1 Reply Last reply Reply Quote 1
                  • K Offline
                    karsten13 @TimOgden
                    last edited by Jan 26, 2022, 10:19 PM

                    @timogden

                    you could use envsubst < config/config.js.template > config/config.js.

                    The config.js.template is 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 run envsubst for config1 and the same for user2/config2 afterwards.

                    S 1 Reply Last reply Jan 26, 2022, 10:26 PM Reply Quote 1
                    • S Away
                      sdetweil @karsten13
                      last edited by Jan 26, 2022, 10:26 PM

                      @karsten13 same as I suggested different tool

                      but envsubst needs it’s values in the environment variables… so another hidden thing (to me)

                      Sam

                      How to add modules

                      learning how to use browser developers window for css changes

                      K 1 Reply Last reply Jan 26, 2022, 10:35 PM Reply Quote 0
                      • K Offline
                        karsten13 @sdetweil
                        last edited by Jan 26, 2022, 10:35 PM

                        @sdetweil

                        only wanted to post this variant …

                        As background: This is a feature of my docker container where you can use a config.js.template and the variables are defined in the docker-compose.yml which starts the container and before starting mm I run envsubst to make the real config.js.

                        1 Reply Last reply Reply Quote 1
                        • 1
                        • 2
                        • 1 / 2
                        1 / 2
                        • First post
                          1/14
                          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