contrast.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. const path = require('path');
  2. const fs = require('fs');
  3. let { hashElement } = require('folder-hash');
  4. /**
  5. * 文件对比
  6. * entry {string} 要对比的文件路径
  7. * include {array} 要对比的文件格式 以数组形式
  8. */
  9. class contrast {
  10. constructor(options) {
  11. this.entry = options.entry;
  12. this.include = options.include || ['*.md'];
  13. this.fileHash = null;
  14. this.oldHash = null;
  15. this.differentHash = null;
  16. this.outPath = path.join(
  17. './cache',
  18. options.entry
  19. .split(path.sep)
  20. .pop()
  21. .replace(/(\.md|\.js)/, '') + '.cache'
  22. );
  23. }
  24. run() {
  25. let _that = this;
  26. return new Promise((reslove, reject) => {
  27. _that.start().then((res) => {
  28. this.getCache().then(
  29. (out) => {
  30. _that.compare();
  31. _that.record();
  32. reslove(_that.differentHash);
  33. },
  34. (err) => {
  35. reslove(_that.fileHash);
  36. _that.record();
  37. }
  38. );
  39. });
  40. });
  41. }
  42. start() {
  43. let _that = this;
  44. return new Promise((reslove, reject) => {
  45. hashElement(_that.entry, {
  46. folders: { exclude: ['.*', 'node_modules', 'test_coverage', '__test__', 'img', 'svg'] },
  47. files: { include: _that.include },
  48. matchBasename: true,
  49. }).then((res) => {
  50. //获取当前hash
  51. _that.fileHash = _that.arraydownGrade(res);
  52. if (_that.fileHash) {
  53. reslove(_that.fileHash);
  54. } else {
  55. reject('err');
  56. }
  57. });
  58. });
  59. }
  60. arraydownGrade(ay) {
  61. let arys = {};
  62. let paths = '';
  63. function downGrade(data) {
  64. if (data.children && data.children.constructor == Array) {
  65. paths = data.name;
  66. data.children.map((item) => {
  67. downGrade(item);
  68. });
  69. } else {
  70. let key = data.name.replace(/(\.md|\.js)/, '');
  71. if (key == 'doc') {
  72. arys[paths] = data.hash;
  73. } else {
  74. arys[key] = data.hash;
  75. }
  76. }
  77. }
  78. downGrade(ay);
  79. return arys;
  80. }
  81. /**
  82. * 获取本地hash记录
  83. */
  84. getCache() {
  85. let _that = this;
  86. return new Promise((reslove, reject) => {
  87. fs.readFile(this.outPath, 'utf8', (err, data) => {
  88. if (!err) {
  89. _that.oldHash = JSON.parse(data);
  90. reslove();
  91. } else {
  92. console.error('There are no cached files in your locality');
  93. reject();
  94. }
  95. });
  96. });
  97. }
  98. compare() {
  99. let fileHash = this.fileHash;
  100. let oldHash = this.oldHash;
  101. let differenthash = {};
  102. for (let newkey in fileHash) {
  103. if (oldHash[newkey]) {
  104. if (oldHash[newkey] != fileHash[newkey]) {
  105. differenthash[newkey] = fileHash[newkey];
  106. }
  107. } else {
  108. differenthash[newkey] = fileHash[newkey];
  109. }
  110. }
  111. this.differentHash = differenthash;
  112. }
  113. record() {
  114. let _that = this;
  115. this.ishasOutFile('./cache').then((res) => {
  116. fs.writeFile(_that.outPath, JSON.stringify(_that.fileHash), 'utf8', (err, res) => {});
  117. });
  118. }
  119. ishasOutFile(outPath) {
  120. return new Promise((resolve, reject) => {
  121. fs.stat(outPath, (err, res) => {
  122. if (err) {
  123. fs.mkdir(outPath, (err) => {
  124. if (err) {
  125. reject(err);
  126. } else {
  127. resolve(true);
  128. }
  129. });
  130. } else {
  131. resolve(true);
  132. }
  133. });
  134. });
  135. }
  136. }
  137. module.exports = contrast;