Newer
Older
CVSS_3.0_GUI / node_modules / nwjs-builder-phoenix / node_modules / dir-compare / common.js
root on 7 May 2019 3 KB Initial commit
  1. var fs = require('fs');
  2. var minimatch = require('minimatch');
  3. var pathUtils = require('path');
  4.  
  5. module.exports = {
  6. detectLoop : function(entry, symlinkCache){
  7. if(entry && entry.symlink){
  8. var realPath = pathUtils.normalize(fs.realpathSync(entry.path)).toLowerCase();
  9. if(symlinkCache[realPath]){
  10. return true;
  11. }
  12. }
  13. return false;
  14. },
  15.  
  16. cloneSymlinkCache : function(symlinkCache){
  17. return {
  18. dir1 : this.shallowClone(symlinkCache.dir1),
  19. dir2 : this.shallowClone(symlinkCache.dir2)
  20. }
  21. },
  22.  
  23. shallowClone : function(obj){
  24. var cloned = {};
  25. Object.keys(obj).forEach(function(key){
  26. cloned[key] = obj[key];
  27. });
  28. return cloned;
  29. },
  30.  
  31. buildEntry : function(path, name){
  32. var statEntry = fs.statSync(path);
  33. var lstatEntry = fs.lstatSync(path);
  34. var isSymlink = lstatEntry.isSymbolicLink();
  35. return {
  36. name : name,
  37. path : path,
  38. stat : statEntry,
  39. lstat : lstatEntry,
  40. symlink : isSymlink,
  41. toString : function () {
  42. return this.name;
  43. }
  44. };
  45. },
  46.  
  47. /**
  48. * One of 'missing','file','directory'
  49. */
  50. getType : function(fileStat) {
  51. if (fileStat) {
  52. if (fileStat.isDirectory()) {
  53. return 'directory';
  54. } else {
  55. return 'file';
  56. }
  57. } else {
  58. return 'missing';
  59. }
  60. },
  61. /**
  62. * Matches fileName with pattern.
  63. */
  64. match : function(fileName, pattern){
  65. var patternArray = pattern.split(',');
  66. for(var i = 0; i<patternArray.length; i++){
  67. var pat = patternArray[i];
  68. if(minimatch(fileName, pat, { dot: true })){ //nocase
  69. return true;
  70. }
  71. }
  72. return false;
  73. },
  74.  
  75. /**
  76. * Filter entries by file name. Returns true if the file is to be processed.
  77. */
  78. filterEntry : function(entry, options){
  79. if (entry.symlink && options.skipSymlinks){
  80. return false;
  81. }
  82.  
  83. if ((entry.stat.isFile() && options.includeFilter) && (!this.match(entry.name, options.includeFilter))) {
  84. return false;
  85. }
  86.  
  87. if ((options.excludeFilter) && (this.match(entry.name, options.excludeFilter))) {
  88. return false;
  89. }
  90.  
  91. return true;
  92. },
  93. /**
  94. * Comparator for directory entries sorting.
  95. */
  96. compareEntryCaseSensitive : function (a, b, ignoreCase) {
  97. if (a.stat.isDirectory() && b.stat.isFile()) {
  98. return -1;
  99. } else if (a.stat.isFile() && b.stat.isDirectory()) {
  100. return 1;
  101. } else {
  102. // http://stackoverflow.com/questions/1179366/is-there-a-javascript-strcmp
  103. var str1 = a.name, str2 = b.name;
  104. return ( ( str1 == str2 ) ? 0 : ( ( str1 > str2 ) ? 1 : -1 ) );
  105. }
  106. },
  107. /**
  108. * Comparator for directory entries sorting.
  109. */
  110. compareEntryIgnoreCase : function (a, b, ignoreCase) {
  111. if (a.stat.isDirectory() && b.stat.isFile()) {
  112. return -1;
  113. } else if (a.stat.isFile() && b.stat.isDirectory()) {
  114. return 1;
  115. } else {
  116. // http://stackoverflow.com/questions/1179366/is-there-a-javascript-strcmp
  117. var str1 = a.name.toLowerCase(), str2 = b.name.toLowerCase();
  118. return ( ( str1 == str2 ) ? 0 : ( ( str1 > str2 ) ? 1 : -1 ) );
  119. }
  120. },
  121. /**
  122. * Compares two dates and returns true/false depending on tolerance (milliseconds).
  123. * Two dates are considered equal if the difference in milliseconds between them is less or equal than tolerance.
  124. */
  125. sameDate : function(date1, date2, tolerance){
  126. return Math.abs(date1.getTime() - date2.getTime()) <= tolerance ? true : false;
  127. },
  128. isNumeric : function(n) {
  129. return !isNaN(parseFloat(n)) && isFinite(n);
  130. }
  131. }
Buy Me A Coffee