本文介绍了在ember伺服器上使用in-repo addon config()方法更新Ember.js环境变量不会生效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里的目标是创建一个自动增量版本号,该版本号会在ember版本和ember投放时同时进行更新.最后,如果我只能在构建中使用它,那完全可以.

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.

我最初问这个问题:>回购插件在构建中写入公共文件会导致服务上无限的构建循环在这种情况下,我试图通过写出JSON文件来解决此问题.该问题已基本解决,但未使用余烬服务.

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

现在,我正在尝试更新本地环境,而不是这样做.但这在余烬发运中也有类似的问题.我的内部版本号逐渐增加.我可以使用config()方法在环境中设置自定义/动态变量.我遇到的问题是,即使我可以在调用config()时在终端中记录更改,并且可以在文件更改时看到它在服务上运行,但是当我输出Ember时却看不到浏览器中的更改ENV使用余烬服务.到目前为止,这是我插件的方法.

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.

注意:appNumberSetup()函数只是读取项目根目录中的本地json文件并更新内部版本号.很好关于pubSettingsFile的任何内容都可以忽略,我不会继续使用.

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是插件中的局部变量,它在build/serv上更新,JSON如下所示:

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

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

是否可以使用动态数据更新Ember的ENV? (如新的内部版本号)

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

addon config()似乎在emberserv的每次更改上运行,并且在终端输出中显示内部版本号.但是看起来好像在postBuild()之后运行.也许这就是为什么我看不到变化.有没有办法在preBuild()期间更新该环境?

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

推荐答案

我不确定具体细节,但 ember-cli-new-version 完成此操作.在构建阶段,他们将创建VERSION.txt文件,甚至可以执行您所需的操作,而无需自己编写.

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.

这篇关于在ember伺服器上使用in-repo addon config()方法更新Ember.js环境变量不会生效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 07:48