-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.js
578 lines (493 loc) · 12 KB
/
file.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
'use strict';
/**
* Copyright (c) 2017 Copyright brainpoint All Rights Reserved.
* Author: lipengxiang
* Desc:
*/
var fs = require('fs');
var path = require('path');
/**
* @desc: 判断文件夹是否存在.
* @return: boolean.
*/
function dirIsExist(dir) {
if (!dir || dir == "")
return false;
var stat;
dir = path.normalize(dir);
if (fs.existsSync(dir))
{
stat = fs.statSync(dir);
if (stat.isDirectory()) {
return true;
}
}
return false;
}
exports.dirIsExist = dirIsExist;
/**
* @desc: 保证文件夹存在.
* @return: bool. 若不存在新建; 文件夹存在返回true.
*/
function dirAssure(dir) {
if (!dir || dir == "")
return true;
var stat;
dir = path.normalize(dir);
if (fs.existsSync(dir))
{
stat = fs.statSync(dir);
if (stat.isDirectory()) {
return true;
}
}
if (dir[dir.length-1] != '/' || dir[dir.length-1] != '\\')
{
dir += '/';
}
var j = 0;
var paths = new Array();
for (var i = 0; i < dir.length; i++) {
if ((dir[i] == '/' || dir[i] == '\\') && i != 0)
{
paths.push( dir.substring(j, i) );
j = i+1;
}
}
dir = '';
for (var i = 0; i < paths.length; i++) {
if (i == 0)
dir += paths[i];
else {
dir += '/' + paths[i];
}
if (fs.existsSync(dir))
{
stat = fs.statSync(dir);
if (stat.isDirectory()) {
continue;
}
}
fs.mkdirSync(dir);
}
return false;
};
exports.dirAssure = dirAssure;
/**
* @desc: copy dir.
* @param callback: (err) => {}, 执行此函数时表示复制完成.
* @return: bool.
*/
exports.dirCopy = function(src, dest, callback) {
if (!src || !dest || !dirIsExist(src)) {
callback && callback(`dirCopy src or dest error; src: ${src}. dest: ${dest} `);
return false;
}
var arrFiles = [];
var arrEmptyDirs = [];
function dirCopy1(dirSrc, dirDest, arrF) {
dirAssure(dirDest);
var src1;
var dest1;
var stat;
for (var i = 0; i < arrF.length; i++) {
src1 = path.join(dirSrc, arrF[i]);
dest1 = path.join(dirDest, arrF[i]);
stat = fs.statSync(src1);
if (stat.isDirectory()) {
var arrF1 = fs.readdirSync(src1);
if (!arrF1 || arrF1.length == 0) {
arrEmptyDirs.push(dest1);
} else {
dirCopy1(src1, dest1, arrF1);
}
} else {
arrFiles.push(src1);
arrFiles.push(dest1);
}
}
} // function.
var arrF = fs.readdirSync(src);
if (!arrF || arrF.length == 0) {
arrEmptyDirs.push(dest);
} else {
dirCopy1(src, dest, arrF);
}
// copy.
for (let i = 0; i < arrEmptyDirs.length; i++) {
dirAssure(arrEmptyDirs[i]);
}
var index = 0;
function copy1(err) {
if (err) {
callback && callback(err);
return;
}
if (index < arrFiles.length) {
var i1 = index++;
var i2 = index++;
fileCopy(arrFiles[i1], arrFiles[i2], copy1);
} else {
callback && callback();
}
}
copy1();
}
/**
* @desc: 返回promise方式.
* @return: Promise(()=>{})
*/
exports.dirCopyAsync = function(src, dest) {
return new Promise((resolve, reject)=>{
exports.dirCopy(src, dest, (err)=>{
if (err) {
reject(err);
}
else {
resolve();
}
});
});
}
/**
* @desc: copy dir exclude specify path.
* @param excludePath: regex.
* @return: Promise(()=>{})
*/
exports.dirCopyExcludeAsync = function(src, dest, excludePath = null) {
return new Promise((resolve, reject)=>{
if (!src || !dest || !dirIsExist(src)) {
reject(`dirCopy src or dest error; src: ${src}. dest: ${dest} `);
return;
}
var arrFiles = [];
var arrEmptyDirs = [];
function dirCopy1(dirSrc, dirDest, arrF) {
dirAssure(dirDest);
var src1;
var dest1;
var stat;
for (var i = 0; i < arrF.length; i++) {
src1 = path.join(dirSrc, arrF[i]);
dest1 = path.join(dirDest, arrF[i]);
if (!excludePath || !excludePath.test(src1)) {
stat = fs.statSync(src1);
if (stat.isDirectory()) {
var arrF1 = fs.readdirSync(src1);
if (!arrF1 || arrF1.length == 0) {
arrEmptyDirs.push(dest1);
} else {
dirCopy1(src1, dest1, arrF1);
}
} else {
arrFiles.push(src1);
arrFiles.push(dest1);
}
}
}
} // function.
var arrF = fs.readdirSync(src);
if (!arrF || arrF.length == 0) {
arrEmptyDirs.push(dest);
} else {
dirCopy1(src, dest, arrF);
}
// copy.
for (let i = 0; i < arrEmptyDirs.length; i++) {
dirAssure(arrEmptyDirs[i]);
}
var index = 0;
function copy1(err) {
if (err) {
reject(err);
return;
}
if (index < arrFiles.length) {
var i1 = index++;
var i2 = index++;
fileCopy(arrFiles[i1], arrFiles[i2], copy1);
} else {
resolve();
}
}
copy1();
});
}
/**
* @desc: 删除文件夹.
* @return: bool. 指明是否删除.
*/
function dirRemoveRecursive(dir) {
try {
if (dir && fs.existsSync(dir))
{
var fspath;
var stat;
var dirList = fs.readdirSync(dir);
for (var i = 0; i < dirList.length; i++) {
fspath = path.join(dir, dirList[i]);
stat = fs.statSync(fspath);
if (stat.isDirectory()) {
dirRemoveRecursive(fspath);
} else {
fs.unlinkSync(fspath);
}
}
fs.rmdirSync(dir);
}
return true;
} catch(e) {
return false;
}
};
exports.dirRemoveRecursive = dirRemoveRecursive;
/**
* @desc: 获取当前目录下的子文件与子目录.
* @param dir: 要搜索的目录路径.
* @param pattern: 子文件或子目录名称,匹配的正则表达式
* 仅从名称的第一个字符开始匹配, 例如: / a.* /, 匹配 a开头的文件名.
* @return: {files:[], dirs:[]}; 发生错误返回null.
*/
exports.dirExplorer = function(dir, pattern) {
let ret = {files:[], dirs:[]};
let fspath;
let stat;
try {
let dirList = fs.readdirSync(dir);
for (let i = 0; i < dirList.length; i++) {
if (pattern) {
let pr = pattern.exec(dirList[i]);
if (!pr || pr.index != 0)
continue;
}
fspath = path.join(dir, dirList[i]);
stat = fs.statSync(fspath);
if (stat.isDirectory()) {
ret.dirs.push(dirList[i]);
} else if (stat.isFile()) {
ret.files.push(dirList[i]);
}
}
} catch(e) {
return null;
}
return ret;
}
/**
* @desc: 递归获取当前目录下的所有子文件.
* @param dir: 要搜索的目录路径.
* @param pattern: 子文件或子目录名称,匹配的正则表达式
* 仅从名称的第一个字符开始匹配, 例如: / a.* /, 匹配 a开头的文件名.
* @return: Array; 发生错误返回null.
*/
exports.dirExplorerFilesRecursive = function(dir, pattern) {
let ret = [];
let fspath;
let stat;
let dirs = [];
try {
let dirList = fs.readdirSync(dir);
for (let i = 0; i < dirList.length; i++) {
fspath = path.join(dir, dirList[i]);
stat = fs.statSync(fspath);
if (stat.isDirectory()) {
dirs.push(dirList[i]);
} else if (stat.isFile()) {
if (pattern) {
let pr = pattern.exec(dirList[i]);
if (!pr || pr.index != 0)
continue;
}
ret.push(dirList[i]);
}
}
for (let j = 0; j < dirs.length; j++) {
let dirList = fs.readdirSync(path.join(dir, dirs[j]));
for (let i = 0; i < dirList.length; i++) {
fspath = path.join(dir, dirs[j], dirList[i]);
stat = fs.statSync(fspath);
if (stat.isDirectory()) {
dirs.push(path.join(dirs[j], dirList[i]));
} else if (stat.isFile()) {
if (pattern) {
let pr = pattern.exec(dirList[i]);
if (!pr || pr.index != 0)
continue;
}
ret.push(path.join(dirs[j], dirList[i]));
}
} // for.
} // for.
} catch(e) {
return null;
}
return ret;
}
/**
* @desc: 递归获取当前目录下的所有子目录.
* @param dir: 要搜索的目录路径.
* @param pattern: 子文件或子目录名称,匹配的正则表达式
* 仅从名称的第一个字符开始匹配, 例如: / a.* /, 匹配 a开头的文件名.
* @return: Array; 发生错误返回null.
*/
exports.dirExplorerDirsRecursive = function(dir, pattern) {
let ret = [];
let fspath;
let stat;
let dirs = [];
try {
let dirList = fs.readdirSync(dir);
for (let i = 0; i < dirList.length; i++) {
fspath = path.join(dir, dirList[i]);
stat = fs.statSync(fspath);
if (stat.isDirectory()) {
dirs.push(dirList[i]);
if (pattern) {
let pr = pattern.exec(dirList[i]);
if (!pr || pr.index != 0)
continue;
}
ret.push(dirList[i]);
}
}
for (let j = 0; j < dirs.length; j++) {
let dirList = fs.readdirSync(path.join(dir, dirs[j]));
for (let i = 0; i < dirList.length; i++) {
fspath = path.join(dir, dirs[j], dirList[i]);
stat = fs.statSync(fspath);
if (stat.isDirectory()) {
dirs.push(path.join(dirs[j], dirList[i]));
if (pattern) {
let pr = pattern.exec(dirList[i]);
if (!pr || pr.index != 0)
continue;
}
ret.push(path.join(dirs[j], dirList[i]));
}
} // for.
} // for.
} catch(e) {
return null;
}
return ret;
}
/**
* @desc: 判断文件是否存在.
* @return: boolean.
*/
exports.fileIsExist = function(file) {
if (!file || file == "")
return false;
var stat;
file = path.normalize(file);
if (fs.existsSync(file))
{
stat = fs.statSync(file);
if (stat.isFile()) {
return true;
}
}
return false;
}
/**
* @desc: 获得文件的字节大小.
* @return: number.-1表示文件不存在.
*/
exports.fileSize = function(file) {
if (!file || file == "")
return -1;
var stat;
file = path.normalize(file);
if (fs.existsSync(file))
{
stat = fs.statSync(file);
if (stat.isFile()) {
return stat.size;
}
}
return -1;
}
/**
* @desc: 复制文件.
* @param callback: (err) => {}, 执行此函数时表示复制完成.
* @return: bool.
*/
function fileCopy(src, dest, callback) {
if (!src || !dest) {
callback && callback(`fileCopy src or dest error; src: ${src}. dest: ${dest} `);
return false;
}
if (!fs.existsSync(src) || (fs.existsSync(dest) && fs.statSync(dest).isFile()) ) {
callback && callback(`fileCopy src or dest error; src: ${src}. dest: ${dest} `);
return false;
}
if (fs.statSync(src).isDirectory()) {
callback && callback('src is directory');
return false;
}
dirAssure(path.dirname(dest));
// 创建读取流
var readable = fs.createReadStream( src );
// 创建写入流
var writable = fs.createWriteStream( dest );
// 通过管道来传输流
readable.pipe( writable );
if (callback) {
readable.on('end', function() {
callback(null);
});
readable.on('error', function(err) {
writable.end();
readable.end();
callback(err);
});
}
return true;
}
exports.fileCopy = fileCopy;
/**
* @desc: 返回promise方式.
* @return: Promise(()=>{})
*/
exports.fileCopyAsync = function(src, dest) {
return new Promise((resolve, reject)=>{
exports.fileCopy(src, dest, (err)=>{
if (err) {
reject(err);
}
else {
resolve();
}
});
});
}
/**
* @desc: 移除文件.
* @return: bool. 指明是否删除.
*/
exports.fileRemove = function(file) {
try {
fs.unlinkSync(file);
return true;
} catch(e) {
return false;
}
}
/**
* @desc: promise方式.
* @return: Promise(()=>{})
*/
exports.fileRemoveAsync = function(file) {
return new Promise((resolve, reject)=>{
try {
fs.unlink(file, (err)=>{
if (err)
reject(err);
else
resolve();
});
} catch(e) {
reject(e);
}
});
}