UNPKG

3.47 kBJavaScriptView Raw
1const Common = require('./common.js');
2
3
4let reg_exp = /([0-9A-Fa-f]*)\s+\((.*)\s+(\d+)\)/
5let name_exp = /(.*)\s+([0-9\:\s\-]{19})\s((\+|\-)\d{4})/
6
7module.exports = {
8 /*
9 HASHSTR := newType('HASHSTR', str)
10 description : function to get vcs hash
11 requires : None
12 returns : Optional[HASHSTR]
13 */
14 getHash: function(root_path) {
15 try{
16 var git_hash = Common.systemSync('git rev-parse HEAD', root_path);
17 return git_hash;
18 }
19 catch(error){
20 console.error(error)
21 return null
22 }
23 },
24
25
26 /*
27 description : function to get git url
28 requires : None
29 returns : Optional[str]
30 */
31 getURL: function(root_path) {
32 try{
33 var git_url = Common.systemSync('git config --get remote.origin.url', root_path);
34 return git_url;
35 }
36 catch(error){
37 console.error(error)
38 return null
39 }
40 },
41
42
43
44 /*
45 description : function to get project name
46 requires : None
47 returns : Optional[str]
48 */
49 getProject: function(root_path) {
50 try{
51 var project_name = Common.systemSync('basename \`git rev-parse --show-toplevel\`', root_path);
52 return project_name;
53 }
54 catch(error){
55 console.error(error)
56 return null
57 }
58 },
59
60
61 /*
62 description : function to get blm infouse regex in javascrip
63 requires : root_path -> str
64 returns : Optional[str]
65 */
66 getBlame: function(root_path, root_json) {
67 try{
68 var blame_info = {}
69 blame_info['package_json'] = Common.systemSync('git blame -l ' + root_path + '/package.json', root_path);
70 Object.keys(root_json).forEach(path =>{
71 root_json[path].forEach( file => {
72 blame_info[path + file] = Common.systemSync('git blame -l ' + path + file, root_path);
73 });
74 });
75 return blame_info;
76 }
77 catch(error){
78 console.error(error)
79 return null
80 }
81 },
82
83
84 genBlame: function(line_info){
85 var blame = {}
86 var line_list = reg_exp.exec(line_info);
87 if(!line_list){ return null; }
88 blame['hash'] = line_list[1];
89 blame['line'] = line_list[3];
90 var name_list = name_exp.exec(line_list[2]);
91 if(!name_list){ return null; }
92 blame['name'] = name_list[1].trim();
93 blame['time'] = name_list[2] + ' ' + name_list[3]
94 return blame;
95 },
96
97
98 /*
99 DEPNAME := newType('DEPNAME', str)
100 BLMINFO := newType('BLMINFO', str)
101 description : function to get blm info
102 requires : raw_blame -> Optional[str],
103 dep_list -> Optional[Sequence[str]]
104 returns : Optional[Dict[DEPNAME, BLMINFO]]
105 */
106 parseBlm: function(raw_blame, dep_list){
107
108 if ((raw_blame == null) || (dep_list == null)) return null;
109
110 var dep_list = Object.keys(dep_list);
111 var blm_list = raw_blame['package_json'].split('\n');
112 var result = {};
113 var i = 0;
114
115 for(var line in blm_list){
116 if (blm_list[line].includes(dep_list[i])){
117 result[dep_list[i]] = this.genBlame(blm_list[line]);
118 i++;
119 }
120 }
121
122 return result
123 }
124};
125
126
127