Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manage AngularJS modules version and releases using Grunt

Currently, I have multiple angular modules. A custom Grunt task concats, minifies and packages each module so it is ready to deploy. The only thing I haven't done yet is to manage the version of these modules.

Each project (one per module) contains a package.json file, in which I declare the name and version of a component :

{
    "name": "my-module",
    "version" : "1.0.0",
    // etc.
}

So each module is built in a directory */dist/my-module/1.0.0/

But, in the module itself, I need to access its version. For example, in a controller, I declare a variable $scope.version = '1.0.0'. But currently, it is hardcoded in the controller script.


First question, is there a way the module could get the version from the package.json file ? Or that the grunt task building the application replaces a given flag in the scripts by the current version of the module ? (for example, I could declare my variable $scope.version = 'FLAG_VERSION' knowing that during the build grunt will replace the flag by the right value)


Second question, is there some grunt component which allows to tag the current version of a module in my VCS (SVN for example) and then increment the current version ? In short, perform a release of the module.

Edit: new question asked on that matter, see Bump a specific version number on SVN using Grunt


Any help or lead will be appreciated !

like image 342
Eria Avatar asked Jan 05 '16 13:01

Eria


1 Answers

First question

I added a grunt-ng-constant task (thank you @Joe) to my grunt build to generate angular constants from the package.json file. It works great, I can now inject them into my directives and services. I had to add the module name to my package.json though, because the ng-constant task need it to identify the module for the generated constants. And the name declared in package.json was more a project name than the name of the module.

{
    "name": "my-project",
    "version" : "1.0.0",
    "moduleName": "myModuleName"
    // etc.
}

Below the result of the ng-constant task :

angular.module('myModuleName')
.constant('appInfo', {name:'my-project',version:'1.0.0'})   
;

I changed my grunt build task to concat this generated script at the end of my application script, before minification. So now I can use appInfo in a service for example :

angular.module('myModuleName').service('myService', [ ..., 'appInfo', function( ..., appInfo ){
    // I can use appInfo.name and appInfo.version here !
}

Be careful! I had a surprise applying this to several modules on the same page : if a constant with the same name is declared on two different modules, the value of the former will be overriden by the latter. AngularJS module.constant() : how to define a constant inside a module only?


Second question

I asked another question, more precise, on that matter : Bump a specific version number on SVN using Grunt.

like image 143
Eria Avatar answered Nov 18 '22 22:11

Eria