1 |
|
2 | var fs = require('fs');
|
3 | var readdirr = require('./readdirr').readdirr;
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | exports.rmdirr = function (dpath, cb) {
|
15 | readdirr(dpath, function (err, dirs, files) {
|
16 | if (err) return cb(err);
|
17 | exports.rmfiles(files, function (err) {
|
18 | if (err) return cb(err);
|
19 | exports.rmdirs(dirs, cb);
|
20 | });
|
21 | });
|
22 | }
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | exports.rmfiles = function (files, cb) {
|
33 | (function loop (index) {
|
34 | if (index == files.length) return cb();
|
35 | fs.unlink(files[index], function (err) {
|
36 | if (err) return cb(err);
|
37 | loop(++index);
|
38 | });
|
39 | }(0));
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | exports.rmdirs = function (dirs, cb) {
|
51 | dirs.sort(desc);
|
52 | (function loop (index) {
|
53 | if (index == dirs.length) return cb();
|
54 | fs.rmdir(dirs[index], function (err) {
|
55 | if (err) return cb(err);
|
56 | loop(++index);
|
57 | });
|
58 | }(0));
|
59 | }
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | function desc(a, b) {
|
70 | if (a > b) return -1;
|
71 | if (a < b) return 1;
|
72 | return 0;
|
73 | }
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|