2

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()?

gregthegeek
  • 1,353
  • 3
  • 14
  • 24

1 Answers1

0

I'm not sure of the specifics but ember-cli-new-version does this. During the build stage they create a VERSION.txt file, might even do what you need already without needing to write it yourself.

jrjohnson
  • 2,401
  • 17
  • 23
  • I'll have to play with that again. Its been a couple years now and when I tried that addon before it either didn't work or didn't do what I wanted exactly. But at this point, I have my numbers working, and I like the way it works, except it doesn't update ENV on serve. It works great on build and I think I'm just going to move on with what I have. Thanks for the help though! – gregthegeek Jun 05 '19 at 20:27