Newer
Older
CVSS_3.0_GUI / node_modules / nwjs-builder-phoenix / node_modules / dir-compare / print.js
root on 7 May 2019 6 KB Initial commit
  1. var colors = require('colors');
  2. var util = require('util');
  3.  
  4. var tab = function (tabs) {
  5. var res = '';
  6. while (tabs>=0) {
  7. res += ' ';
  8. tabs--;
  9. }
  10. return res;
  11. };
  12.  
  13. // Prints dir compare results.
  14. // 'program' represents display options and correspond to dircompare command line parameters.
  15. // Example: 'dircompare --show-all --exclude *.js dir1 dir2' translates into
  16. // program: {showAll: true, exclude: '*.js'}
  17. //
  18. var print = function(res, writer, program){
  19. var nocolor = function(str){return str};
  20. var cequal = program.nocolors?nocolor:colors.green;
  21. var cdistinct = program.nocolors?nocolor:colors.red;
  22. var cleft = nocolor;
  23. var cright = nocolor;
  24. var cdir = nocolor;
  25. var cmissing = program.nocolors?nocolor:colors.yellow;
  26.  
  27. // calculate relative path length for pretty print
  28. var relativePathMaxLength = 0, fileNameMaxLength=0;
  29. if(!program.csv && res.diffSet){
  30. res.diffSet.forEach(function (detail) {
  31. if(detail.relativePath.length>relativePathMaxLength){
  32. relativePathMaxLength = detail.relativePath.length;
  33. }
  34. var len = getCompareFile(detail, '??', cmissing).length;
  35. if(len>fileNameMaxLength){
  36. fileNameMaxLength = len;
  37. }
  38. });
  39. }
  40.  
  41. // csv header
  42. if(program.csv){
  43. writer.write('path,name,state,type,size1,size2,date1,date2\n');
  44. }
  45. if(res.diffSet){
  46. for(var i = 0; i<res.diffSet.length; i++){
  47. var detail = res.diffSet[i];
  48. var color, show = true;
  49.  
  50. if(!program.wholeReport){
  51. // show only files
  52. var type = detail.type1!=='missing'?detail.type1:detail.type2;
  53. if(type!=='file'){
  54. show = false;
  55. }
  56. }
  57. if(show){
  58. switch (detail.state) {
  59. case 'equal':
  60. color = cequal;
  61. show = program.showAll || program.showEqual?true:false;
  62. break;
  63. case 'left':
  64. color = cleft;
  65. show = program.showAll || program.showLeft?true:false;
  66. break;
  67. case 'right':
  68. color = cright;
  69. show = program.showAll || program.showRight?true:false;
  70. break;
  71. case 'distinct':
  72. color = cdistinct;
  73. show = program.showAll || program.showDistinct?true:false;
  74. break;
  75. default:
  76. show = true;
  77. color = colors.gray;
  78. }
  79. if(show){
  80. if(program.csv){
  81. printCsv(writer, detail, color);
  82. } else {
  83. printPretty(writer, program, detail, color, cdir, cmissing, relativePathMaxLength, fileNameMaxLength);
  84. }
  85. }
  86. }
  87. }
  88. }
  89.  
  90. // PRINT STATISTICS
  91. var statTotal, statEqual, statLeft, statRight, statDistinct;
  92. if(program.wholeReport){
  93. statTotal = res.total;
  94. statEqual = res.equal;
  95. statLeft = res.left;
  96. statRight = res.right;
  97. statDistinct = res.distinct;
  98. } else{
  99. statTotal = res.totalFiles;
  100. statEqual = res.equalFiles;
  101. statLeft = res.leftFiles;
  102. statRight = res.rightFiles;
  103. statDistinct = res.distinctFiles;
  104. }
  105. if(!program.noDiffIndicator){
  106. writer.write(res.same?cequal('Entries are identical\n'):cdistinct('Entries are different\n'));
  107. }
  108. writer.write(util.format('total: %s, equal: %s, distinct: %s, only left: %s, only right: %s\n',
  109. statTotal,
  110. cequal(statEqual),
  111. cdistinct(statDistinct),
  112. cleft(statLeft),
  113. cright(statRight)
  114. ));
  115. }
  116.  
  117. /**
  118. * Print details for default view mode
  119. */
  120. var printPretty = function(writer, program, detail, color, dircolor, missingcolor, relativePathMaxLength, fileNameMaxLength){
  121. var path = detail.relativePath===''?'/':detail.relativePath;
  122.  
  123. var state;
  124. switch (detail.state) {
  125. case 'equal':
  126. state = '==';
  127. break;
  128. case 'left':
  129. state = '->';
  130. break;
  131. case 'right':
  132. state = '<-';
  133. break;
  134. case 'distinct':
  135. state = '<>';
  136. break;
  137. default:
  138. state = '?';
  139. }
  140. var spacePad = relativePathMaxLength - path.length;
  141. var type ='';
  142. type = detail.type1!=='missing' ? detail.type1 : detail.type2;
  143. if(type==='directory'){
  144. type = dircolor(type);
  145. }
  146. var cmpentrylen = getCompareFile(detail, "??", missingcolor).length;
  147. var cmpentry = getCompareFile(detail, color(state), missingcolor);
  148. if(program.wholeReport){
  149. writer.write(util.format('[%s] %s(%s)\n', path, cmpentry, type));
  150. } else{
  151. writer.write(util.format('[%s] %s\n', path, cmpentry));
  152. }
  153. }
  154.  
  155. var getCompareFile = function(detail, state, missingcolor){
  156. p1 = detail.name1 ? detail.name1 : '';
  157. p2 = detail.name2 ? detail.name2 : '';
  158. var missing1 = detail.type1==='missing' ? missingcolor('missing') : '';
  159. var missing2 = detail.type2==='missing' ? missingcolor('missing') : '';
  160. return util.format('%s%s%s%s%s', missing1, p1, state, missing2, p2);
  161. }
  162.  
  163. /**
  164. * Print csv details.
  165. */
  166. var printCsv = function(writer, detail, color){
  167. var size1='', size2='';
  168. if(detail.type1==='file'){
  169. size1 = detail.size1!=undefined ? detail.size1 : '';
  170. }
  171. if(detail.type2==='file'){
  172. size2 = detail.size2!=undefined ? detail.size2 : '';
  173. }
  174.  
  175. var date1='', date2='';
  176. date1 = detail.date1!=undefined ? detail.date1.toISOString() : '';
  177. date2 = detail.date2!=undefined ? detail.date2.toISOString() : '';
  178.  
  179. var type ='';
  180. type = detail.type1!=='missing' ? detail.type1 : detail.type2;
  181.  
  182. var path = detail.relativePath?detail.relativePath:'/';
  183. var name = (detail.name1?detail.name1:detail.name2);
  184.  
  185. writer.write(util.format('%s,%s,%s,%s,%s,%s,%s,%s\n', path, name, color(detail.state), type, size1, size2, date1, date2));
  186. };
  187.  
  188. module.exports = print;
Buy Me A Coffee