合并具有相似属性的两个bower

合并具有相似属性的两个bower

本文介绍了合并具有相似属性的两个bower.json文件(合并具有相似属性的两个JSON文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想合并两个bower.json文件。一个是我的项目的本地,另一个是不同项目之间共享的。我需要在运行bower install之前合并这两个文件。



请检查我的要求示例:

本地bower.json

  {
name:myLocalProject,
version:0.0.1,
devDependencies:{
angular:1.1.0
},
依赖关系:{
components-font-awesome:*
},
resolution:{
angular:1.3.15
}
}

和共享bower.json

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

},
决议:{
angular:1.3.15
}
}

和结果bower.json将取代本地bower.json

  {
name:myLocalProject,//不变
version:0.0.1,//不变
devDependencies:{
angular:1.3.15,//版本已更新
angular-resource:1.3.15//添加到本地
},
dependencies:{
components-font-awesome:*//在本地
},
resolution中保持不变:{
angular:1.3.15
}
}

我将为合并创建一个Grunt任务,所以我需要在JavaScript中进行合并。

解决方案

好的,我自己写了这个函数:

  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){
//用于将依赖关系的版本从共享版升级到本地
grunt .log.writeln(更新本地+ j +从+ local [i] [j] +到>+ shared [i] [j]);
local [i] [j] = shared [i] [j];
} else if(cmp(shared [i] [j],local [i] [j]) grunt.log.writeln(j +in shared bower.json +is+ shared [i] [j] +且比本地版本旧+ local [i] [j]);



$ b if(check){
//如果在本地
local [i ] [j] =共享[i] [j];
grunt.log.writeln(增加的依赖关系:+ j +:+ local [i] [j]);
}
}
}
}
返回JSON.stringify(local,null,4);
};

和我的Grunt任务:

  grunt.task.registerTask('merge','合并本地和共享bower.json文件的任务',function(){

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

运行:

  $ grunt merge 


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.

解决方案

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

这篇关于合并具有相似属性的两个bower.json文件(合并具有相似属性的两个JSON文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 11:23