0

I want to merge two bower.json files. One is local to my project and the other one is shared between different projects. I need to merge these two files before running bower install.

Please check the example for my requirements:

Local bower.json

{
    "name": "myLocalProject",
    "version": "0.0.1",
    "devDependencies": {
        "angular": "1.1.0"
    },
    "dependencies": {
       "components-font-awesome": "*"
    },
    "resolutions": {
        "angular": "1.3.15"
    }
}

and shared bower.json

{
    "name": "sharedBowerFile",
    "version": "0.0.3",
    "devDependencies": {
        "angular": "1.3.15",
        "angular-resource": "1.3.15"
    },
    "dependencies": {

    },
    "resolutions": {
        "angular": "1.3.15"
    }
}

and the result bower.json which will replace local bower.json

{
        "name": "myLocalProject",           // unchanged
        "version": "0.0.1",                 // unchanged
        "devDependencies": {
            "angular": "1.3.15",             //version updated
            "angular-resource": "1.3.15"     // added to local
        },
        "dependencies": {
            "components-font-awesome": "*"   //stays the same in local
        },
        "resolutions": {
            "angular": "1.3.15"                 
        }
    }

I will create a Grunt task for the merge, so I would need the merge in JavaScript.

mohi
  • 1,093
  • 2
  • 16
  • 21
  • http://stackoverflow.com/a/383245/2506594 Is that what you're looking to do? You could just load the two json files, merge, and stringify again? – Devin H. May 22 '15 at 14:59
  • No, that does not do the work. Check my example – mohi May 22 '15 at 15:21

1 Answers1

0

OK, I wrote the function myself:

var mergeJSON = function (local, shared) {
    var check = true;
    for(var i in shared ) {
        if(typeof(shared[i]) == 'object') {
            for(var j in shared[i]) {
                check =true;
                for (var z in local[i]) {
                    if(j == z){
                        check = false;
                            if( cmp(shared[i][j], local[i][j])>0 ) {
                            //For upgrading the version of dependencies from shared to local
                            grunt.log.writeln("Updating local " + j + " from " + local[i][j] + " to >" + shared[i][j]);
                            local[i][j] = shared[i][j];
                            }else if(cmp(shared[i][j], local[i][j])<0){
                                grunt.log.writeln(j + " in shared bower.json " + " is " + shared[i][j] + " and older than local version " + local[i][j]);

                            }
                    }
                }
                if(check){
                    //add the dependency if does not exist in local
                    local[i][j] = shared[i][j];
                    grunt.log.writeln("Added dependency: " + j + ": " + local[i][j] );
                }
            }
        }
    }
    return JSON.stringify(local,null,4);
};

and my Grunt task:

 grunt.task.registerTask('merge','A task to merge local and shared bower.json file', function(){

            var shared = grunt.file.readJSON("/path/to/sharedJSON");
            var local = grunt.file.readJSON("bower.json");
            grunt.file.write('bower.json',mergeJSON(local,shared));
    });

Run:

$ grunt merge
mohi
  • 1,093
  • 2
  • 16
  • 21