Merging two bower.json files with similar attributes (merging two JSON files with similar attributes)

故事扮演 提交于 2019-12-13 04:50:58

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/30399841/merging-two-bower-json-files-with-similar-attributes-merging-two-json-files-wit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!