Skip to content

Commit

Permalink
文件缓存修改
Browse files Browse the repository at this point in the history
  • Loading branch information
lilinsen committed May 7, 2019
1 parent 8da5d31 commit 475aeb7
Show file tree
Hide file tree
Showing 4 changed files with 335 additions and 339 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ docshashCache.text
srchashCache.text
localsrc.cache
localdocs.cache
cache/src.cache
cache/mdToVue.cache
cache/docs.cache
1 change: 0 additions & 1 deletion build/webpack.doc.base.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const webpackBaseConf = require('./webpack.base.conf.js');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const merge = require('webpack-merge');

const mdtohtml = require('../scripts/mdToVue');
const isDev = process.env.NODE_ENV === 'development';

Expand Down
132 changes: 132 additions & 0 deletions scripts/contrast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
const path = require('path');
const fs = require('fs');
let { hashElement } = require('folder-hash');
/**
* 文件对比
* entry {string} 要对比的文件路径
* include {array} 要对比的文件格式 以数组形式
*/
class contrast{
constructor(options){
this.entry = options.entry;
this.include = options.include || ['*.md'] ;
this.fileHash = null;
this.oldHash = null;
this.differentHash = null;
this.outPath = path.join("./cache",options.entry.split(path.sep).pop().replace(/(\.md|\.js)/,'')+ '.cache');

}
run(){
let _that = this;
return new Promise((reslove,reject)=>{
_that.start().then(res=>{
this.getCache().then(out=>{
_that.compare();
_that.record();
reslove(_that.differentHash);
},err=>{
reslove(_that.fileHash);
_that.record();
})
})
})
}
start(){
let _that = this;
return new Promise((reslove,reject)=>{
hashElement(_that.entry, {
folders: { exclude: ['.*', 'node_modules', 'test_coverage','__test__','img','svg'] },
files: { include: _that.include, },
matchBasename: true
}).then(res=>{
//获取当前hash
_that.fileHash = _that.arraydownGrade(res);
if(_that.fileHash){
reslove(_that.fileHash)
}else{
reject("err")
}
})
})

}
arraydownGrade(ay){
let arys = {};
let paths = "";
function downGrade(data){
if(data.children && data.children.constructor == Array ){
paths = data.name;
data.children.map(item => {
downGrade(item)
})
}else{
let key = data.name.replace(/(\.md|\.js)/,'');
if(key =="doc"){
arys[paths] = data.hash;
}else{
arys[key] = data.hash;
}
}
}
downGrade(ay)
return arys;
}
/**
* 获取本地hash记录
*/
getCache(){
let _that = this;
return new Promise((reslove,reject)=>{
fs.readFile(this.outPath,"utf8",(err,data) => {
if(!err){
_that.oldHash = JSON.parse(data);
reslove();
}else{
console.error("There are no cached files in your locality");
reject();
}
})
})
}
compare(){
let fileHash = this.fileHash;
let oldHash = this.oldHash;
let differenthash = {};
for(let newkey in fileHash){
if(oldHash[newkey]){
if(oldHash[newkey] != fileHash[newkey]){
differenthash[newkey] = fileHash[newkey]
}
}else{
differenthash[newkey] = fileHash[newkey]
}
}
this.differentHash = differenthash;
}
record(){
let _that = this;
this.ishasOutFile("./cache").then(res=>{
fs.writeFile(_that.outPath,JSON.stringify(_that.fileHash),'utf8',(err,res)=>{ })
})

}
ishasOutFile(outPath){
return new Promise((resolve,reject)=>{
fs.stat(outPath,(err,res)=>{
if(err){
fs.mkdir(outPath,err=>{
if(err){
reject(err)
}else{
resolve(true)
}
})
}else{
resolve(true)
}
})
})
}

}
module.exports = contrast
Loading

0 comments on commit 475aeb7

Please sign in to comment.