contrast.js 4.1 KB

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