Merge "script to import multiple dgs"
[ccsdk/distribution.git] / dgbuilder / tools / multiple_dg_load.js
1
2 var fs = require('fs');
3 var obj =[];
4 try{
5  obj =  JSON.parse(fs.readFileSync(process.argv[2], 'utf8'));
6 }catch(err){
7 }
8 var dirPath=process.argv[3];
9 console.log(dirPath);
10 function getID() {
11         return (1+Math.random()*4294967295).toString(16);
12 }
13
14 var filteredArray = obj.filter(function(itm){
15   return itm.type == 'tab' ;
16 });
17
18 var tabsArr=filteredArray;
19  var files = fs.readdirSync(dirPath);
20     for (var i in files) {
21        var currentFile = dirPath + '/' + files[i];
22         //console.log(currentFile);
23        var stats = fs.statSync(currentFile);
24 try{
25         if (stats.isFile()) {
26                 var moduleName="";
27                 var methodName="";
28                 console.log("processing... " + currentFile);
29                 var o = JSON.parse(fs.readFileSync(currentFile, 'utf8'));
30                 for(var idx in o){
31                         if(o[idx].type == "service-logic"){
32                                 moduleName=o[idx].name;
33                         } 
34                         if(o[idx].type == "method"){
35                                 methodName=o[idx].name;
36                                 methodName = methodName.replace("method ","");
37                         } 
38                         if(moduleName != "" && methodName != ""){
39                                 break;
40                         }
41                 }
42                 var id  = o[0].z;
43                 //console.log("tab id:" + id);
44                 var newId = id;
45                 var number_of_tabs = 0;
46                 if(filteredArray != null ){
47                  number_of_tabs = filteredArray.length;
48                 }
49                 //console.log("number_of_tabs:" + number_of_tabs);
50                 //console.log("id:" + id);
51                 if(filteredArray != null  && filteredArray.length > 0){
52                         for(var i in filteredArray){
53                                 if(filteredArray[i].id == id){
54                                         newId = getID();        
55                                         console.log("Using new id:" + newId);
56                                 }
57                         }
58                         for(var idx in o){
59                                 o[idx].z= newId;
60                                 var oldNodeId = o[idx].id;
61                                 var newNodeId = getID();
62                                 o[idx].id= newNodeId;
63                                 for(var k=0;k<o.length;k++){
64                                         if(o[k] != null && o[k].wires != undefined && o[k].wires != null){      
65                                                 for(var j=0;o[k].wires[0] != null && j< o[k].wires[0].length;j++){
66                                                         if(o[k].wires[0][j] == oldNodeId){
67                                                                 o[k].wires[0][j]=newNodeId;
68                                                         }
69                                                         
70                                                 }       
71                                         }
72                                 }
73                         }
74                 }
75                         //"label": "Sheet " + (number_of_tabs + 1),
76                 var tabObj= {
77                         "id": newId,
78                         "label": methodName,
79                         "type": "tab"
80                 };
81         
82                 filteredArray.push(tabObj);
83                 obj.unshift(tabObj);
84                 for(var idx in o){
85                         obj.push(o[idx]);
86                 }
87                 //console.dir(obj);
88         }
89 }catch(err){
90         console.log(err);
91 }
92 };
93 function getCurrentDate(){
94                 var d = new Date();
95                 var mm = d.getMonth() + 1;
96                 var dd =   d.getDate();
97                 var yyyy = d.getYear() + 1900;
98                 var hr = d.getHours();
99                 var min = d.getMinutes();
100                 var sec = d.getSeconds();
101                 if(mm<10) mm = "0" + mm;
102                 if(dd<10) dd = "0" + dd;
103                 if(hr<10) hr = "0" + hr;
104                 if(min<10) min = "0" + min;
105                 if(sec<10) sec = "0" + sec;
106                 var formatedValue = mm + "-" + dd + "-" + yyyy + "_" + hr + "" + min + "" + sec;
107                 return formatedValue;
108         }
109 if (fs.existsSync(process.argv[2])) {
110         fs.renameSync(process.argv[2],process.argv[2]+ "_" + getCurrentDate());
111 }
112 fs.writeFileSync( process.argv[2] + ".new", JSON.stringify(obj));
113 if (fs.existsSync(process.argv[2] + ".new")) {
114         fs.renameSync(process.argv[2] + ".new",process.argv[2]);
115 }

© 2017 ONAP. Copyright © The Linux Foundation ®. All Rights Reserved.
The Linux Foundation has registered trademarks and uses trademarks.
For a list of trademarks of The Linux Foundation, please see our Trademark Usage page.
Linux is a registered trademark of Linus Torvalds.
Privacy Policy and Terms of Use