/**
|
* Created by xujianfei on 18/2/28.
|
*/
|
|
var fs = require("fs");
|
var path = require('path');
|
|
|
module.exports = {
|
|
moveDir: function(fromDir,toDir){
|
var files = [];
|
if (fs.existsSync(fromDir)) {
|
files = fs.readdirSync(fromDir);
|
files.forEach(function (file, index) {
|
var curPath = fromDir + "/" + file;
|
var toPath = toDir + "/" + file;
|
if (fs.statSync(curPath).isDirectory()) { // recurse
|
this.moveDir(curPath,toPath);
|
} else {
|
this.moveFile(curPath,toPath);
|
}
|
//fs.rmdir(fromDir);
|
}.bind(this));
|
}
|
},
|
|
moveFile: function(from,to){
|
// console.log('移动文件 from ' + from + ', to ' + to);
|
if(!fs.existsSync(from)){
|
console.log('文件不存在!');
|
return;
|
}
|
|
this.checkAndCreateDir(to);
|
|
fs.renameSync(from, to, function (err) {
|
if (err) throw err;
|
});
|
},
|
|
copyDir: function(fromDir,toDir,filter){
|
var files = [];
|
if (fs.existsSync(fromDir)) {
|
files = fs.readdirSync(fromDir);
|
files.forEach(function (file, index) {
|
var curPath = fromDir + "/" + file;
|
var toPath = toDir + "/" + file;
|
if (fs.statSync(curPath).isDirectory()) { // recurse
|
this.copyDir(curPath,toPath,filter);
|
}
|
else {
|
var need_copy = true;
|
//console.log('file = ' + file + " filter = " + filter);
|
if(filter) {
|
if(file.indexOf(filter) > -1){
|
need_copy = true;
|
}
|
else{
|
need_copy = false;
|
}
|
|
}
|
if(need_copy){
|
this.copyFile(curPath,toPath);
|
}
|
|
}
|
}.bind(this));
|
}
|
},
|
|
copyFile: function(from,to){
|
//console.log('copy file from ' + from + ', to ' + to);
|
if(!fs.existsSync(from)){
|
console.log('文件不存在!' + from);
|
return;
|
}
|
|
this.checkAndCreateDir(to);
|
|
//fs.createReadStream(from).pipe(fs.createWriteStream(to));
|
fs.writeFileSync(to, fs.readFileSync(from));
|
},
|
|
checkAndCreateDir: function(dirOrFilePath) {
|
|
var lineArray = dirOrFilePath.split('/');
|
var root = '';
|
for (var lineIdx = 0; lineIdx < lineArray.length; lineIdx++) {
|
//目录
|
var dic = lineArray[lineIdx];
|
root += (dic + '/');
|
|
//console.log('root = ' + root);
|
if (dic.indexOf('.') < 0) {
|
//创建目录
|
if (!fs.existsSync(root)) {
|
//console.log('创建目录 ' + root);
|
fs.mkdirSync(root);
|
|
}
|
}
|
}
|
},
|
|
deleteFile: function(filePath){
|
fs.unlinkSync(filePath);
|
},
|
|
//删除文件夹
|
deleteDir: function(path) {
|
var self = this;
|
var files = [];
|
if( fs.existsSync(path) ) {
|
files = fs.readdirSync(path);
|
files.forEach(function(file,index){
|
var curPath = path + "/" + file;
|
if(fs.statSync(curPath).isDirectory()) {
|
// recurse
|
self.deleteDir(curPath);
|
} else {
|
// delete file
|
fs.unlinkSync(curPath);
|
}
|
});
|
fs.rmdirSync(path);
|
}
|
},
|
|
//清空文件夹
|
cleanDir: function(rootPath) {
|
|
var deleteDir = function(path){
|
var files = [];
|
if( fs.existsSync(path) ) {
|
files = fs.readdirSync(path);
|
files.forEach(function(file,index){
|
var curPath = path + "/" + file;
|
if(fs.statSync(curPath).isDirectory()) {
|
// recurse
|
deleteDir(curPath);
|
} else {
|
// delete file
|
fs.unlinkSync(curPath);
|
}
|
});
|
if(path != rootPath){
|
fs.rmdirSync(path);
|
}
|
}
|
};
|
|
deleteDir(rootPath);
|
},
|
|
// 递归创建目录 同步方法
|
mkDir : function(dirname) {
|
var self = this;
|
if (fs.existsSync(dirname)) {
|
return true;
|
} else {
|
if (self.mkDir(path.dirname(dirname))) {
|
fs.mkdirSync(dirname);
|
return true;
|
}
|
}
|
}
|
};
|