8

Is it possible to generate environment object asynchronously?

Reason: I would like to have available commit hash in my application. Idea is to use environment.ts file. However to obtain current commit hash I need asynchronous call (eg. using git-rev).

I tried to inject variable from environment variables using process.env: $ VERSION=123 ng serve

declare var process: any;

export const environment = {
  production: false,
  version: process.env.VERSION
};

But this results in undefined version.

Martin Nuc
  • 5,604
  • 2
  • 42
  • 48
  • You can make async calls in the environment file but I don't think that will help you get a correct commit hash as it will be evaluated at runtime in the users browser. Why do you want to have the commit hash in the environment file? – shusson Feb 28 '17 at 04:14
  • Oh I didnt know its evaluated at runtime. Thought its evaluated during build. Is there any other way to inject commit message to my project? Lets say I want to display commit message in some component. – Martin Nuc Feb 28 '17 at 23:36
  • Are you coupling your dev environment to your components or are you arbitrarily displaying commit messages? If it's the prior then I would be thinking some sort of commit hook that writes the commit to the environment file. – shusson Feb 28 '17 at 23:42
  • I use SentryIO as Error Handler and it allows you to specify version currently running. I would like to include commit hash in that version. – Martin Nuc Feb 28 '17 at 23:46

1 Answers1

8

Take a look at this question : How to include git revision into angular-cli application?

There is also a discussion on github on this related subject, which it will be easier to add possibility to pass environment variables to environment.ts : https://github.com/angular/angular-cli/issues/4318

Community
  • 1
  • 1
Alexandre Duros
  • 826
  • 1
  • 7
  • 9