-
Notifications
You must be signed in to change notification settings - Fork 21
/
file-system.js
335 lines (286 loc) · 8.01 KB
/
file-system.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
/**
* @fileoverview Strengthen the ability of file system
* @author wliao <[email protected]>
*/
var fs = require('fs');
var util = require('utils-extend');
var path = require('path');
var fileMatch = require('file-match');
function checkCbAndOpts(options, callback) {
if (util.isFunction(options)) {
return {
options: null,
callback: options
};
} else if (util.isObject(options)) {
return {
options: options,
callback: callback
};
} else {
return {
options: null,
callback: util.noop
};
}
}
function getExists(filepath) {
var exists = fs.existsSync(filepath);
if (exists) {
return filepath;
} else {
return getExists(path.dirname(filepath));
}
}
util.extend(exports, fs);
/**
* @description
* Assign node origin methods to fs
*/
exports.fs = fs;
exports.fileMatch = fileMatch;
/**
* @description
* Create dir, if dir exist, it will only invoke callback.
*
* @example
* ```js
* fs.mkdir('1/2/3/4/5', 511);
* fs.mkdir('path/2/3', function() {});
* ```
*/
exports.mkdir = function(filepath, mode, callback) {
var root = getExists(filepath);
var children = path.relative(root, filepath);
if (util.isFunction(mode)) {
callback = mode;
mode = null;
}
if (!util.isFunction(callback)) {
callback = util.noop;
}
mode = mode || 511;
if (!children) return callback();
children = children.split(path.sep);
function create(filepath) {
if (create.count === children.length) {
return callback();
}
filepath = path.join(filepath, children[create.count]);
fs.mkdir(filepath, mode, function(err) {
create.count++;
create(filepath);
});
}
create.count = 0;
create(root);
};
/**
* @description
* Same as mkdir, but it is synchronous
*/
exports.mkdirSync = function(filepath, mode) {
var root = getExists(filepath);
var children = path.relative(root, filepath);
if (!children) return;
children = children.split(path.sep);
children.forEach(function(item) {
root = path.join(root, item);
fs.mkdirSync(root, mode);
});
};
/**
* @description
* Create file, if path don't exists, it will not throw error.
* And will mkdir for path, it is asynchronous
*
* @example
* ```js
* fs.writeFile('path/filename.txt', 'something')
* fs.writeFile('path/filename.txt', 'something', {})
* ```
*/
exports.writeFile = function(filename, data, options, callback) {
var result = checkCbAndOpts(options, callback);
var dirname = path.dirname(filename);
options = result.options;
callback = result.callback;
// Create dir first
exports.mkdir(dirname, function() {
fs.writeFile(filename, data, options, callback);
});
};
/**
* @description
* Same as writeFile, but it is synchronous
*/
exports.writeFileSync = function(filename, data, options) {
var dirname = path.dirname(filename);
exports.mkdirSync(dirname);
fs.writeFileSync(filename, data, options);
};
/**
* @description
* Asynchronously copy a file
* @example
* file.copyFile('demo.txt', 'demo.dest.txt', { done: function(err) { }})
*/
exports.copyFile = function(srcpath, destpath, options) {
options = util.extend({
encoding: 'utf8',
done: util.noop
}, options || {});
if (!options.process) {
options.encoding = null;
}
fs.readFile(srcpath, {
encoding: options.encoding
}, function(err, contents) {
if (err) return options.done(err);
if (options.process) {
contents = options.process(contents);
}
exports.writeFile(destpath, contents, options, options.done);
});
};
/**
* @description
* Copy file to dest, if no process options, it will only copy file to dest
* @example
* file.copyFileSync('demo.txt', 'demo.dest.txt' { process: function(contents) { }});
* file.copyFileSync('demo.png', 'dest.png');
*/
exports.copyFileSync = function(srcpath, destpath, options) {
options = util.extend({
encoding: 'utf8'
}, options || {});
var contents;
if (options.process) {
contents = fs.readFileSync(srcpath, options);
contents = options.process(contents, srcpath, options.relative);
if (util.isObject(contents) && contents.filepath) {
destpath = contents.filepath;
contents = contents.contents;
}
exports.writeFileSync(destpath, contents, options);
} else {
contents = fs.readFileSync(srcpath);
exports.writeFileSync(destpath, contents);
}
};
/**
* @description
* Recurse into a directory, executing callback for each file and folder
* if the filename is undefiend, the callback is for folder, otherwise for file.
* and it is asynchronous
* @example
* file.recurse('path', function(filepath, filename) { });
* file.recurse('path', ['*.js', 'path/**\/*.html'], function(filepath, relative, filename) { });
*/
exports.recurse = function(dirpath, filter, callback) {
if (util.isFunction(filter)) {
callback = filter;
filter = null;
}
var filterCb = fileMatch(filter);
var rootpath = dirpath;
function recurse(dirpath) {
fs.readdir(dirpath, function(err, files) {
if (err) return callback(err);
files.forEach(function(filename) {
var filepath = path.join(dirpath, filename);
fs.stat(filepath, function(err, stats) {
var relative = path.relative(rootpath, filepath);
var flag = filterCb(relative);
if (stats.isDirectory()) {
recurse(filepath);
if (flag) callback(filepath, relative);
} else {
if (flag) callback(filepath, relative, filename);
}
});
});
});
}
recurse(dirpath);
};
/**
* @description
* Same as recurse, but it is synchronous
* @example
* file.recurseSync('path', function(filepath, filename) {});
* file.recurseSync('path', ['*.js', 'path/**\/*.html'], function(filepath, relative, filename) {});
*/
exports.recurseSync = function(dirpath, filter, callback) {
if (util.isFunction(filter)) {
callback = filter;
filter = null;
}
var filterCb = fileMatch(filter);
var rootpath = dirpath;
function recurse(dirpath) {
fs.readdirSync(dirpath).forEach(function(filename) {
var filepath = path.join(dirpath, filename);
var stats = fs.statSync(filepath);
var relative = path.relative(rootpath, filepath);
var flag = filterCb(relative);
if (stats.isDirectory()) {
recurse(filepath);
if (flag) callback(filepath, relative);
} else {
if (flag) callback(filepath, relative, filename);
}
});
}
recurse(dirpath);
};
/**
* @description
* Remove folder and files in folder, but it's synchronous
* @example
* file.rmdirSync('path');
*/
exports.rmdirSync = function(dirpath) {
exports.recurseSync(dirpath, function(filepath, relative, filename) {
// it is file, otherwise it's folder
if (filename) {
fs.unlinkSync(filepath);
} else {
fs.rmdirSync(filepath);
}
});
fs.rmdirSync(dirpath);
};
/**
* @description
* Copy dirpath to destpath, pass process callback for each file hanlder
* if you want to change the dest filepath, process callback return { contents: '', filepath: ''}
* otherwise only change contents
* @example
* file.copySync('path', 'dest');
* file.copySync('src', 'dest/src');
* file.copySync('path', 'dest', { process: function(contents, filepath) {} });
* file.copySync('path', 'dest', { process: function(contents, filepath) {} }, noProcess: ['']);
*/
exports.copySync = function(dirpath, destpath, options) {
options = util.extend({
encoding: 'utf8',
filter: null,
noProcess: ''
}, options || {});
var noProcessCb = fileMatch(options.noProcess);
// Make sure dest root
exports.mkdirSync(destpath);
exports.recurseSync(dirpath, options.filter, function(filepath, relative, filename) {
if (!filename) return;
var newpath = path.join(destpath, relative);
var opts = {
relative: relative
};
if (options.process && !noProcessCb(relative)) {
opts.encoding = options.encoding;
opts.process = options.process;
}
exports.copyFileSync(filepath, newpath, opts);
});
};