1 | var Path = require('path');
|
2 | var fs = require('fs');
|
3 |
|
4 | exports.mkdirp = function (path) {
|
5 | var dirs = [];
|
6 | while (!fs.existsSync(path)) {
|
7 | dirs.push(Path.basename(path));
|
8 | path = Path.dirname(path);
|
9 | }
|
10 |
|
11 | while (dirs.length) {
|
12 | path = Path.join(path, dirs.pop());
|
13 | fs.mkdirSync(path);
|
14 | }
|
15 | };
|
16 |
|
17 |
|
18 | exports.rmDir = function (path) {
|
19 | var files = fs.readdirSync(path);
|
20 | files.forEach(function (file) {
|
21 | var thisPath = Path.join(path, file),
|
22 | stat = fs.statSync(thisPath);
|
23 |
|
24 | if (stat.isFile()) {
|
25 | fs.unlinkSync(thisPath);
|
26 | } else if (stat.isDirectory()) {
|
27 | exports.rmDir(thisPath);
|
28 | }
|
29 | });
|
30 |
|
31 | fs.rmdirSync(path);
|
32 | };
|
33 |
|
34 |
|
35 | exports.scanDir = function (path, ext, fn) {
|
36 | if (fn === undefined) {
|
37 | fn = ext;
|
38 | ext = null;
|
39 | }
|
40 |
|
41 | var files = fs.readdirSync(path);
|
42 |
|
43 | files.forEach(function (file) {
|
44 | var thisPath = Path.join(path, file),
|
45 | stat = fs.statSync(thisPath);
|
46 |
|
47 | if (stat.isFile() &&
|
48 | (!ext || Path.extname(thisPath) === ext)) {
|
49 | return fn(thisPath);
|
50 | }
|
51 |
|
52 | if (stat.isDirectory()) {
|
53 | return exports.scanDir(thisPath, ext, fn);
|
54 | }
|
55 | });
|
56 | };
|
57 |
|
58 |
|
59 | exports.copyDir = function (src, des, filter, progress) {
|
60 | var files = fs.readdirSync(src);
|
61 |
|
62 | exports.mkdirp(des);
|
63 |
|
64 | files.forEach(function (file) {
|
65 | var srcFile = Path.join(src, file),
|
66 | desFile = Path.join(des, file),
|
67 | stat = fs.statSync(srcFile);
|
68 |
|
69 | if (filter && !filter(file, srcFile, stat)) {
|
70 | return;
|
71 | }
|
72 |
|
73 | if (stat.isFile()) {
|
74 | exports.copyFile(srcFile, desFile);
|
75 | progress && progress(srcFile, desFile);
|
76 | } else if (stat.isDirectory()) {
|
77 | exports.copyDir(srcFile, desFile, filter, progress);
|
78 | }
|
79 | });
|
80 |
|
81 | if (!fs.readdirSync(des).length) {
|
82 | exports.rmDir(des);
|
83 | }
|
84 | };
|
85 |
|
86 |
|
87 | exports.copyFile = function (src, des) {
|
88 | var r = fs.openSync(src, 'r'),
|
89 | w = fs.openSync(des, 'w'),
|
90 | buf = new Buffer(4096),
|
91 | rsize = null,
|
92 | wsize = null;
|
93 |
|
94 | try {
|
95 | while ((rsize = fs.readSync(r, buf, 0, buf.length)) > 0) {
|
96 | wsize = fs.writeSync(w, buf, 0, rsize);
|
97 | if (wsize !== rsize) {
|
98 | throw new Error('copy file error');
|
99 | }
|
100 | }
|
101 | } finally {
|
102 | fs.closeSync(r);
|
103 | fs.closeSync(w);
|
104 | }
|
105 |
|
106 | };
|
107 |
|
108 |
|
109 | var globCache = {};
|
110 | exports.glob = function (pattern, path, stat) {
|
111 | if (/\/$/.test(pattern)) {
|
112 | if (!stat.isDirectory()) {
|
113 | return false;
|
114 | }
|
115 | } else {
|
116 | if (!stat.isFile()) {
|
117 | return false;
|
118 | }
|
119 | }
|
120 |
|
121 | path = '/' + path.replace(/\\/g, '/').replace(/^\//, '');
|
122 |
|
123 | var r = globCache[pattern];
|
124 | if (!r) {
|
125 | r = pattern.replace(/\/$/, '')
|
126 | .replace(/[^-.\w*?\/]/, '')
|
127 | .replace(/\./g, '\\.')
|
128 | .replace(/\?/g, '[-\\w]')
|
129 | .replace(/\*{2,}/g, '[-\\w\\/]+')
|
130 | .replace(/\*/g, '[-\\w]+')
|
131 | .replace(/\+/g, '*') + '$';
|
132 |
|
133 | r = /^\//.test(pattern) ? '^' + r : '/' + r;
|
134 | r = globCache[pattern] = new RegExp(r);
|
135 | }
|
136 |
|
137 | return r.test(path);
|
138 | };
|
139 |
|
140 |
|
141 | exports.copyDirGlob = function (from, to, rule, options) {
|
142 | var include = rule.include;
|
143 | var ignore = rule.ignore;
|
144 | var filter = function (file, path, stat) {
|
145 | path = Path.relative(from, path);
|
146 | if (include) {
|
147 | return stat.isDirectory() || globs(include, path, stat);
|
148 | }
|
149 |
|
150 | if (ignore) {
|
151 | return !globs(ignore, path, stat);
|
152 | }
|
153 |
|
154 | return false;
|
155 | };
|
156 |
|
157 | exports.copyDir(from, to, filter, function (src, des) {
|
158 |
|
159 | });
|
160 | };
|
161 |
|
162 | function globs(rules, path, stat) {
|
163 | for (var i = 0, c = rules.length; i < c; i++) {
|
164 | if (exports.glob(rules[i], path, stat)) {
|
165 | return true;
|
166 | }
|
167 | }
|
168 | return false;
|
169 | } |
\ | No newline at end of file |