forked from iwangx/spa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackage.js
More file actions
76 lines (71 loc) · 2.08 KB
/
Copy pathpackage.js
File metadata and controls
76 lines (71 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* Created by iwangx on 16/5/10.
* 打包完成后的回调
*/
var chunkConfig=require("./readModuleConfig");
var fs = require( 'fs' ),
path=require('path');
var dev =process.env.MODE;
/*
* 复制目录中的所有文件包括子目录
* @param{ String } 需要复制的目录
* @param{ String } 复制到指定的目录
*/
var copy = function( src, dst ){
var fileList=fs.readdirSync(src);
fileList.forEach(function(item,i){
var _src = src + '/' + item,
_dst = dst + '/' + item,
readable, writable;
var stat = fs.lstatSync(_src);
if(stat.isFile()){
// 创建读取流
readable = fs.createReadStream( _src );
// 创建写入流
writable = fs.createWriteStream( _dst );
// 通过管道来传输流
readable.pipe( writable );
}else if( stat.isDirectory() ){
exists( _src, _dst, copy );
}
});
};
// 在复制目录前需要判断该目录是否存在,不存在需要先创建目录
var exists = function( src, dst, callback ){
if(!fs.existsSync(dst)){
var pathtmp;
dst.split(path.sep).forEach(function(dirname) {
if (pathtmp) {
pathtmp = path.join(pathtmp, dirname);
}
else {
pathtmp = dirname;
}
if (!fs.existsSync(pathtmp)) {
fs.mkdirSync(pathtmp);
callback( src, dst );
}
});
}else{
callback( src, dst );
}
};
//删除文件夹
var deletePath = 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
deletePath(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
//复制images目录
exists( 'images', 'dist/store/images', copy );
deletePath("images");