AngularJs: Show version number from git or gazebo

I have created an Angular app where I want to show the current version number of my app on the screen. Currently I have implemented it as a constant:

application
    .constant('constants', {
        VERSION: '1.1.2'
    });

      

But this will require me to update the constant in every new version. I am using bower and git and I was wondering if there is a way to get the version number as a variable from one of these packages dynamically?

+3


source to share


1 answer


The solution is to use the Gulp and gulp-ng-constant plugin. It automates the release workflow, writes the version of your app to Angular support.

  • Install Gulp
  • Install gulp -ng-constant
  • Write your gulp task like this and run it gulp constants

    :
var gulp = require('gulp');
var ngConstant = require('gulp-ng-constant');

gulp.task('constants', function() {
  // get version from bower file
  var bower = require('./bower.json');
  // set version to ng contants
  var constants = { version: bower.version };

  return ngConstant({
      constants: constants,
      stream: true,
      name: 'app.constants'
    })
    // save ngConstant.js to src/app/
    .pipe(gulp.dest('./src/app'));
});

      

It will generate angular.module for you like this:

angular.module("app.constants", [])
  .constant("version", "0.0.1")

      



And the last step you need is to inject your created persistent service into the main application:

var app = angular.module('app', ['app.constants']);

      

You may also be interested in using a gulp-bump task to automatically bump up your version of the application before it is added to the ng-constant service.

See the following tutorial: Versions of your Angular App with Gulp

+4


source







All Articles