lundi 20 mai 2019

Updating Ember.js environment variables do not take effect using in-repo addon config() method on ember serve

My goal here is to create an auto-increment build number that updates both on ember build and ember serve. In the end, if I can only use this on build, that's totally ok.

I originally asked this question: In-repo addon writing public files on build causes endless build loop on serve In that I was attempting to solve this problem by writing out JSON files. The problem was mostly solved, but not using ember serve.

Instead of doing that, I'm now trying to update the local environment. But this is having a similar problem with ember serve. I've got the build number incrementing fine. I can use the config() method to set custom/dynamic variables in the environment. The problem I'm having is that the even though I can log the change in terminal when config() is called, and I can see it run on serve when files change, I don't see the changes in browser when I output Ember's ENV using ember serve. Here's my addon's methods so far.

Note: the appNumberSetup() function is just reading a local json file in the project root and updating the build number. That's working fine. Anything about pubSettingsFile can be ignored, I won't be using that moving forward.

init(parent, project) {
    this._super.init && this._super.init.apply(this, arguments);
    // we need to setup env in init() so config() and prebuild()
    // will see update immediately
    this.settingsFile = path.resolve(this.appDir,  this.settingsFileName);
    this.addonPubDataPath = path.resolve(this.appDir, 'lib', this.name, 'inc', 'public', 'build-data-output');
    this.pubSettingsFile = path.resolve(this.addonPubDataPath,  this.pubSettingsFileName);
    // this only checks for .env variables and sets defaults
    this.dotEnvSetup();
    // must set this so prebuild skips processing a build number on build
    // else we get build number incremented twice on first run
    // then appNumberSetup() disables so subsequent serve preBuild() will run.
    this.skipPreBuild = true;
    this.appNumberSetup();

},
// this sends our created settings data to ENV.localBuildSettings in app
config(environment, appConfig){
    // this 'buildme' is just an experiment
    let x = `buildme${this.buildNumber}`;
    let r = {
        localBuildSettings: this.settings
    };
    r[`buildme${this.buildNumber}`] = this.buildNumber;
    this.dlog("Config ran...");
    this.dlog(JSON.stringify(r, null, 4));
    return r;
},
preBuild: function(result){
    // init() disables preBuild() here, but subsequent builds with serve still
    // run appNumberSetup() to update this.settings for env and JSON
    if(this.skipPreBuild === true){
        this.skipPreBuild = false;
    }
    else {
        // only run here after init runs
        this.appNumberSetup();
    }
    // don't do this... write file makes endless loop on serve
    // this.saveSettingsFile(this.pubSettingsFile, this.settings);

},

this.settings is a local variable in addon and it updated on build/serve, the JSON looks like this:

{
"appVersion": 911,
"appBuildNumber": 7117
}

Is there a way to update Ember's ENV with dynamic data? (like a new build number)

The addon config() appears to run on each change in ember serve, and it shows the build number in terminal output. But it looks like that runs after postBuild(). Maybe that's why I don't see the changes. Is there a way to update that environment during preBuild()?




Aucun commentaire:

Enregistrer un commentaire