UNPKG

21.7 kBMarkdownView Raw
1# nodejs-fs-utils
2
3> NodeJs FileSystem (FS) extra utilities
4
5![build-passing](https://img.shields.io/badge/build-passing-brightgreen.svg) ![dependencies: up-to-date](https://img.shields.io/badge/dependencies-up--to--date-brightgreen.svg) ![status: stable](https://img.shields.io/badge/status-stable-brightgreen.svg) ![downloads: 3k / month](https://img.shields.io/badge/downloads-3k%2Fmonth-brightgreen.svg)
6
7![mail-sergiu.gordienco@gmail.com](https://img.shields.io/badge/author-sergiu.gordienco@gmail.com-blue.svg)
8
9## Methods
10
11 * [rmdirs](#rmdirs) - remove a file or folder even it isn't empty ( accept specifying logic for symlinks )
12 * [rmdirsSync](#rmdirssync) - _synchronous version of_ **rmdirs**
13 * [mkdirs](#mkdirs-build-a-directory-tree) - creates a folder and it parent if it is needed ( accept specifying logic for symlinks )
14 * [mkdirsSync](#mkdirssync) - _synchronous version of_ **mkdirs**
15 * [emptyDir](#emptydir) - remove contents of a dir
16 * [emptyDirSync](#emptydirsync) - _synchronous version of_ **emptyDir**
17 * [isEmpty](#isempty) - return if file is empty or not
18 * [isEmptySync](#isemptysync) - _synchronous version of_ **isEmpty**
19 * [remove](#remove) - remove file, directory or link
20 * [removeSync](#removesync) - _synchronous version of_ **remove**
21 - `symbolicLinks` - treat symbolic links as files ( default `true` )
22 - `skipErrors` - skip errors just log them ( default `false` )
23 * [fsize](#fsize-advanced-file-size-scan-for-links-folders-or-files) - count file or folder size, has additional options
24 - `symbolicLinks` - treat symbolic links as files ( default `true` )
25 - `countFolders` - counts and folder inode size ( default `true` )
26 - `countSymbolicLinks` - counts symbolic links inode size ( default `true` )
27 - `logErrors` - log all error in an array ( default `false` )
28 - `skipErrors` - skip errors just log them ( default `false` )
29 * [fsizeSync](#fsizesync-file-or-folder-size-synchronous) - _synchronous version of_ **fsize**
30 * [move](#move) - move files or folders
31 * [moveSync](#movesync) - _synchronous version of_ **move**
32 - `symlinksKeep` - specify how to treat symlinks
33 - accepted values: *"file", "directory", "all"*
34 - `symlinksNormalize` - specify if is needed link normalizing
35 - accepted values: *"auto", "none", "relative", "absolute"*
36 - `linkFiles` - for files linking instead of coping
37 - accepted values: *"auto", "none", "relative", "absolute"*
38 - `symbolicLinks` - treat symbolic links as files ( default `true` )
39 - `countFolders` - counts and folder inode size ( default `true` )
40 - `countSymbolicLinks` - counts symbolic links inode size ( default `true` )
41 - `logErrors` - log all error in an array ( default `false` )
42 - `skipErrors` - skip errors just log them ( default `false` )
43 * [copy](#copy) - copy files or folders
44 * [copySync](#copysync) - _synchronous version of_ **copy**
45 - `symlinksKeep` - specify how to treat symlinks
46 - accepted values: *"file", "directory", "all"*
47 - `symlinksNormalize` - specify if is needed link normalizing
48 - accepted values: *"auto", "none", "relative", "absolute"*
49 - `linkFiles` - for files linking instead of coping
50 - accepted values: *"auto", "none", "relative", "absolute"*
51 - `symbolicLinks` - treat symbolic links as files ( default `true` )
52 - `countFolders` - counts and folder inode size ( default `true` )
53 - `countSymbolicLinks` - counts symbolic links inode size ( default `true` )
54 - `logErrors` - log all error in an array ( default `false` )
55 - `skipErrors` - skip errors just log them ( default `false` )
56 * [walk](#walk-walk-throuth-files-folder-and-links-advanced-configurations) - walk throuth files, folder and links ( advanced configurations )
57 * [walkSync](#walksync-walk-sync-throuth-files-folder-and-links-advanced-configurations) - synchronous version of walk
58 * [walk method - examples](#walk-examples)
59
60## rmdirs
61
62> optional can be send *fs* module in `"fs"` option, P.S. it removes link files or directories.
63
64```javascript
65 var fsUtils = require("nodejs-fs-utils");
66
67 //removing a folder
68 fsUtils.rmdirs("test/folder", function (err) {
69 // callback code
70 });
71
72 // removing a folder and remove recursive in symbolic links
73 // treat the as folders if necessary
74 fsUtils.rmdirs("test/folder", function (err) {
75 // callback code
76 }, {
77 symbolicLinks : false
78 });
79
80 // try to remove, skip errors
81 fsUtils.rmdirs("test/folder", function (err) {
82 // callback code
83 }, {
84 skipErrors : true
85 });
86```
87
88## rmdirsSync
89
90```javascript
91 var fsUtils = require("nodejs-fs-utils");
92
93 // removing a folder
94 // symbolic links will be unlinked instead of removing files from them
95 fsUtils.rmdirsSync("test/folder");
96
97 // removing a folder and remove recursive in symbolic links
98 // treat the symbolic links as folders if these links to directories
99 fsUtils.rmdirsSync("test/folder", {
100 symbolicLinks : false
101 });
102
103 // try to remove, skip errors
104 fsUtils.rmdirsSync("test/folder", {
105 skipErrors : true
106 });
107```
108
109
110## emptyDir
111
112> remove contents of a directory
113
114```javascript
115 var fsUtils = require("nodejs-fs-utils");
116
117 // removing folder's contents
118 fsUtils.emptyDir("test/folder", function (err) {
119 // callback code
120 });
121```
122
123## emptyDirSync
124
125> remove contents of a directory, synchronous
126
127```javascript
128 var fsUtils = require("nodejs-fs-utils");
129
130 // removing folder's contents
131 fsUtils.emptyDirSync("test/folder");
132```
133
134## isEmpty
135
136> checks if folder is empty
137
138```javascript
139 var fsUtils = require("nodejs-fs-utils");
140
141 // return state = true if folder is empty
142 fsUtils.isEmpty("test/folder", function (err, state) {
143 // callback code
144 });
145```
146
147## isEmptySync
148
149> checks if folder is empty, synchronous
150
151```javascript
152 var fsUtils = require("nodejs-fs-utils");
153
154 // return state = true if folder is empty
155 fsUtils.isEmptySync("test/folder");
156```
157
158
159
160
161## mkdirs - build a directory tree
162
163> optional can be send *fs* module in `"fs"` option
164
165```javascript
166 var fsUtils = require("nodejs-fs-utils");
167
168 // removing a folder
169 // the function will stop an exception on symlink detection
170 fsUtils.mkdirs("newfolder/folder/subfolder", function (err) {
171 // callback code
172 });
173
174 // treat the symbolic links as folders if these links to directories
175 fsUtils.rmdirs("newfolder/folder/symbolic-link/subfolder", {
176 symbolicLinks : false
177 }, function (err) {
178 // callback code
179 });
180```
181
182## mkdirsSync
183
184```javascript
185 var fsUtils = require("nodejs-fs-utils");
186
187 // removing a folder
188 // the function will throw an exception on symlink detection
189 fsUtils.mkdirs("newfolder/folder/subfolder");
190
191 // treat the symbolic links as folders if these links to directories
192 fsUtils.rmdirs("newfolder/folder/symbolic-link/subfolder", {
193 symbolicLinks : false
194 });
195```
196
197
198## remove
199
200> removing file or directories
201
202> similar / alias as [rmdirs](#rmdirs), click [here](#rmdirs) to view.
203
204## removeSync
205
206> removing file or directories
207
208> similar / alias as [rmdirs](#rmdirssync), click [here](#rmdirssync) to view.
209
210
211## fsize - advanced file size scan for links folders or files
212
213> optional can be send *fs* module in `"fs"` option
214
215```javascript
216 var fsUtils = require("nodejs-fs-utils");
217
218 // return file size
219 fsUtils.fsize("videos/video.mp4", function (err, size) {
220 // callback code
221 });
222
223
224 // the function will stop on a symlink detection
225 fsUtils.fsize("newfolder/folder/subfolder", function (err, size) {
226 // callback code
227 });
228
229
230 // treat the symbolic links as folders if these links to directories
231 fsUtils.fsize("newfolder/folder/symbolic-link/subfolder", {
232 symbolicLinks : false
233 }, function (err, size) {
234 // callback code
235 });
236
237
238 // don't stop scanning on errors
239 fsUtils.fsize("newfolder/folder/symbolic-link/subfolder", {
240 skipErrors : true
241 }, function (err, size) {
242 // callback code
243 });
244
245
246 // don't stop scanning on errors
247 // return an array of all errors
248 fsUtils.fsize("newfolder/folder/symbolic-link/subfolder", {
249 skipErrors : true,
250 logErrors : true
251 }, function (err, size) {
252 // callback code
253 });
254
255
256 // don't count folders size
257 fsUtils.fsize("newfolder/folder/symbolic-link/subfolder", {
258 countFolders : false
259 }, function (err, size) {
260 // callback code
261 });
262
263
264 // don't scan links and don't count links size
265 fsUtils.fsize("newfolder/folder/symbolic-link/subfolder", {
266 countSymbolicLinks : false
267 }, function (err, size) {
268 // callback code
269 });
270```
271
272## fsizeSync - file or folder size synchronous
273
274```javascript
275 var fsUtils = require("nodejs-fs-utils");
276
277 // return file size
278 var size = fsUtils.fsizeSync("videos/video.mp4");
279
280
281 // the function will stop on a symlink detection
282 var size = fsUtils.fsizeSync("newfolder/folder/subfolder", function (err, size) {
283 // callback code
284 });
285
286
287 // treat the symbolic links as folders if these links to directories
288 var size = fsUtils.fsizeSync("newfolder/folder/symbolic-link/subfolder", {
289 symbolicLinks : false
290 });
291
292
293 // don't stop scanning on errors
294 var config = { skipErrors : true };
295 var size = fsUtils.fsizeSync("newfolder/folder/symbolic-link/subfolder", config);
296 if (config.errors.length) {
297 console.log("Error detected: ", config.errors[0])
298 }
299
300
301 // don't stop scanning on errors
302 // return an array of all errors
303 var config = { skipErrors : true };
304 var size = fsUtils.fsizeSync("newfolder/folder/symbolic-link/subfolder", config);
305 if (config.errors.length) {
306 console.log("Error detected: ", config.errors[0])
307 }
308
309
310 // don't count folders size
311 var size = fsUtils.fsizeSync("newfolder/folder/symbolic-link/subfolder", {
312 countFolders : false
313 });
314
315
316 // don't scan links and don't count links size
317 var size = fsUtils.fsizeSync("newfolder/folder/symbolic-link/subfolder", {
318 countSymbolicLinks : false
319 });
320```
321
322## move
323> move file of folders or links
324
325### options for move function:
326
327- `symlinksNormalize` - specify how to treat symlinks
328 specify if is needed link normalizing
329 accepted values: `"auto"`, `"none"`, `"relative"`, `"absolute"`
330 `"auto"`, `"none"` or `"absolute"` - uses absolute path
331 `"relative"` - uses relative paths for links
332 **P.S** `"auto"` will be dynamic in future, will try to use relative if it is posible
333 - `symlinksKeep` - specify if is needed to keep simplinks or to move files or folders
334 accepted values: *"file", "directory", "all"*
335 - `linkFiles` - for files linking instead of moving
336 accepted values: *"auto", "none", "relative", "absolute"*
337 - `symbolicLinks` - treat symbolic links as files ( default `true` )
338 - `countFolders` - counts and folder inode size ( default `true` )
339 - `countSymbolicLinks` - counts symbolic links inode size ( default `true` )
340 - `logErrors` - log all error in an array ( default `false` )
341 - `skipErrors` - skip errors just log them ( default `false` )
342 **P.S.** if is `true` the errors will be an array
343
344### move examples:
345
346### moveSync examples:
347
348```javascript
349 var fsUtils = require("nodejs-fs-utils");
350
351 // move file or folders
352 fsUtils.move(__dirname + "/source", "./destination-path", function (err, cache) {
353 if (!err) {
354 console.log("Moved !");
355 } else {
356 console.error("Error", err)
357 }
358 });
359
360 // move files and skip errors
361 fsUtils.move(__dirname + "/source", "./destination-path", function (errors, cache) {
362 if (!errors.length) {
363 console.log("Moved !");
364 } else {
365 errors.forEach(function (err) {
366 console.error("Error", err)
367 });
368 }
369 }, {
370 skipErrors : true
371 });
372
373```
374
375## moveSync
376
377> synchronous version for move function
378
379### moveSync examples:
380
381```javascript
382 var fsUtils = require("nodejs-fs-utils");
383 var moveSync = fsUtils.moveSync;
384
385 // move file or folders
386 moveSync(__dirname + "/source", "./destination-path", function (err, cache) {
387 if (!err) {
388 console.log("Moved !");
389 } else {
390 console.error("Error", err)
391 }
392 });
393
394 // move files and skip errors
395 moveSync(__dirname + "/source", "./destination-path", function (errors, cache) {
396 if (!errors.length) {
397 console.log("Moved !");
398 } else {
399 errors.forEach(function (err) {
400 console.error("Error", err)
401 });
402 }
403 }, {
404 skipErrors : true
405 });
406
407```
408
409## copy
410> copy file of folders or links
411
412### options for copy function:
413
414- `symlinksNormalize` - specify how to treat symlinks
415 specify if is needed link normalizing
416 accepted values: `"auto"`, `"none"`, `"relative"`, `"absolute"`
417 `"auto"`, `"none"` or `"absolute"` - uses absolute path
418 `"relative"` - uses relative paths for links
419 **P.S** `"auto"` will be dynamic in future, will try to use relative if it is posible
420 - `symlinksKeep` - specify if is needed to keep simplinks or to copy files or folders
421 accepted values: *"file", "directory", "all"*
422 - `linkFiles` - for files linking instead of coping
423 accepted values: *"auto", "none", "relative", "absolute"*
424 - `symbolicLinks` - treat symbolic links as files ( default `true` )
425 - `countFolders` - counts and folder inode size ( default `true` )
426 - `countSymbolicLinks` - counts symbolic links inode size ( default `true` )
427 - `logErrors` - log all error in an array ( default `false` )
428 - `skipErrors` - skip errors just log them ( default `false` )
429 **P.S.** if is `true` the errors will be an array
430
431### copy examples:
432
433### copySync examples:
434
435```javascript
436 var fsUtils = require("nodejs-fs-utils");
437
438 // copy file or folders
439 fsUtils.copy(__dirname + "/source", "./destination-path", function (err, cache) {
440 if (!err) {
441 console.log("Copied !");
442 } else {
443 console.error("Error", err)
444 }
445 });
446
447 // copy files and skip errors
448 fsUtils.copy(__dirname + "/source", "./destination-path", function (errors, cache) {
449 if (!errors.length) {
450 console.log("Copied !");
451 } else {
452 errors.forEach(function (err) {
453 console.error("Error", err)
454 });
455 }
456 }, {
457 skipErrors : true
458 });
459
460 // link files instead of copying
461 fsUtils.copy(__dirname + "/source", "./destination-path", function (err, cache) {
462 if (!err) {
463 console.log("Files were linked !");
464 } else {
465 console.error("Error", err)
466 }
467 }, {
468 linkFiles : "relative"
469 });
470
471```
472
473## copySync
474
475> synchronous version for copy function
476
477### copySync examples:
478
479```javascript
480 var fsUtils = require("nodejs-fs-utils");
481 var copySync = fsUtils.copySync;
482
483 // copy file or folders
484 copySync(__dirname + "/source", "./destination-path", function (err, cache) {
485 if (!err) {
486 console.log("Copied !");
487 } else {
488 console.error("Error", err)
489 }
490 });
491
492 // copy files and skip errors
493 copySync(__dirname + "/source", "./destination-path", function (errors, cache) {
494 if (!errors.length) {
495 console.log("Copied !");
496 } else {
497 errors.forEach(function (err) {
498 console.error("Error", err)
499 });
500 }
501 }, {
502 skipErrors : true
503 });
504
505 // link files instead of copying
506 copySync(__dirname + "/source", "./destination-path", function (err, cache) {
507 if (!err) {
508 console.log("Files were linked !");
509 } else {
510 console.error("Error", err)
511 }
512 }, {
513 linkFiles : "relative"
514 });
515
516```
517
518## walk - walk throuth files folder and links ( advanced configurations )
519
520> optional can be send *fs* module in `"fs"` option
521> cache reference can be used for storing data while walking
522
523```javascript
524 var fsUtils = require("nodejs-fs-utils");
525
526 // walk througth a list of files and folders in a folder
527 fsUtils.walk("./videos", function (err, path, stats, next, cache) {
528 // callback code
529 // ...
530 // stats.isDirectory()
531 //
532 // cache.errors is array of errors
533 // continue walk
534 next();
535 });
536
537 // walk througth a list of files and folders in a folder
538 fsUtils.walk("./videos", function (err, path, stats, next, cache) {
539 // callback code
540 // ...
541 // stats.isDirectory()
542 //
543 // cache.errors is array of errors
544 // continue walk
545 next();
546 }, function (cache) {
547 console.log("Walk is finished");
548 });
549
550
551 // treat the symbolic links as folders if these links to directories
552 fsUtils.walk("newfolder/folder/symbolic-link/subfolder", {
553 symbolicLinks : false
554 }, function (err, path, stats, next, cache) {
555 // callback code
556 next();
557 });
558
559
560 // don't stop scanning on errors
561 fsUtils.walk("newfolder/folder/symbolic-link/subfolder", {
562 skipErrors : true
563 }, function (err, path, stats, next, cache) {
564 // callback code
565 next();
566 });
567
568
569 // don't stop scanning on errors
570 // return an array of all errors in cache reference
571 fsUtils.walk("newfolder/folder/symbolic-link/subfolder", {
572 skipErrors : true,
573 logErrors : true
574 }, function (err, path, stats, next, cache) {
575 // callback code
576 next();
577 }, function (cache) {
578 if (cache.errors.length) {
579 console.log("Errors: ", cache.errors);
580 } else {
581 console.log("No errors found");
582 }
583 });
584
585```
586
587## walkSync - walk sync throuth files folder and links ( advanced configurations )
588
589> `walkSync` has same api as `walk`, but it is synchronous
590
591```javascript
592 var fsUtils = require("nodejs-fs-utils");
593
594 // walk througth a list of files and folders in a folder
595 fsUtils.walkSync("./videos", function (err, path, stats, next, cache) {
596 // callback code
597 // ...
598 // stats.isDirectory()
599 //
600 // cache.errors is array of errors
601 // continue walk
602 next();
603 });
604
605 // walk througth a list of files and folders in a folder
606 fsUtils.walkSync("./videos", function (err, path, stats, next, cache) {
607 // callback code
608 // ...
609 // stats.isDirectory()
610 //
611 // cache.errors is array of errors
612 // continue walk
613 next();
614 }, function (cache) {
615 console.log("Walk is finished");
616 });
617
618
619 // treat the symbolic links as folders if these links to directories
620 fsUtils.walkSync("newfolder/folder/symbolic-link/subfolder", {
621 symbolicLinks : false
622 }, function (err, path, stats, next, cache) {
623 // callback code
624 next();
625 });
626
627
628 // don't stop scanning on errors
629 fsUtils.walkSync("newfolder/folder/symbolic-link/subfolder", {
630 skipErrors : true
631 }, function (err, path, stats, next, cache) {
632 // callback code
633 next();
634 });
635
636
637 // don't stop scanning on errors
638 // return an array of all errors in cache reference
639 fsUtils.walkSync("newfolder/folder/symbolic-link/subfolder", {
640 skipErrors : true,
641 logErrors : true
642 }, function (err, path, stats, next, cache) {
643 // callback code
644 next();
645 }, function (cache) {
646 if (cache.errors.length) {
647 console.log("Errors: ", cache.errors);
648 } else {
649 console.log("No errors found");
650 }
651 });
652```
653
654## walk - examples
655
656> getArray of folders in a array
657
658```javascript
659 var fsUtils = require("nodejs-fs-utils");
660 // getArray of folders in a array
661 var folders = [];
662 fsUtils.walkSync("newfolder/folder/symbolic-link/subfolder", {
663 skipErrors : true,
664 logErrors : true
665 }, function (err, path, stats, next, cache) {
666 if (!err && stats.isDirectory()) {
667 folders.push(path);
668 }
669 next();
670 });
671```
672
673> remove folders with name "tmp"
674
675```javascript
676 var fsUtils = require("nodejs-fs-utils");
677 var fs = require("fs");
678
679 // synchronous
680 fsUtils.walkSync("newfolder/folder/symbolic-link/subfolder", {
681 skipErrors : true,
682 logErrors : true
683 }, function (err, path, stats, next, cache) {
684 if (!err && stats.isDirectory() && path.match(/\/tmp$/)) {
685 fs.unlinkSync(path);
686 } else {
687 next();
688 }
689 });
690
691 // asynchronous
692 fsUtils.walk("newfolder/folder/symbolic-link/subfolder", {
693 skipErrors : true,
694 logErrors : true,
695 stackPushEnd: true // push new observed files to end of the stack insteat of beginig
696 }, function (err, path, stats, next, cache) {
697 if (!err && stats.isDirectory() && path.match(/\/tmp$/)) {
698 fs.unlinkSync(path);
699
700 // for async to tell that step is done
701 // without this row onend callback will not be trigered
702 cache.count++;
703 } else {
704 next();
705 }
706 }, function (cache) { // optional
707 console.log("Finished")
708 });
709```
\No newline at end of file