-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs2.js
1837 lines (1481 loc) · 47.9 KB
/
fs2.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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Seems like this will be superceded by fnlfs.
var jsgui = require('jsgui3-html');
var child_process = require('child_process');
var ncp_module = require('ncp');
//var checksum = require('./file-checksum');
var rimraf = require('rimraf');
//var child_process = require('child_process');
var ncp = ncp_module.ncp;
var log = console.log;
// removing libxmljs?
var each = jsgui.each,
stringify = jsgui.stringify,
is_array = jsgui.is_array;
var is_defined = jsgui.is_defined;
var tof = jsgui.tof,
arrayify = jsgui.arrayify,
mapify = jsgui.mapify;
var dir_separator = '/';
//if (process.platform === 'win32') dir_separator = '\\';
var fs = require('fs');
var node_path = require('path');
//var im = require('imagemagick');
//var libxmljs = require("libxmljs");
var exec = child_process.exec;
var fp = jsgui.fp;
var call_multiple_callback_functions = jsgui.call_multiple_callback_functions;
var call_multi = jsgui.call_multi;
var Fns = jsgui.Fns;
// promisify these, put them into fnlfs.
// do more work on fnlfs to make it observables.
// then put that into vhl.
// maybe with a bit of options and config done / automated by vhl.
var file_checksum = function (file_path, callback) {
var algo = 'sha256';
var shasum = crypto.createHash(algo);
var file = file_path;
var s = fs.ReadStream(file);
s.on('data', function (d) {
shasum.update(d);
});
s.on('end', function () {
//var d = shasum.digest('hex');
var digest = shasum.digest('base64');
//console.log(d);
// base64
callback(null, digest);
});
}
var recursive_xml_traversal = function (xml, el_callback) {
if (xml.root) {
var root = xml.root();
recursive_xml_traversal(root, el_callback);
} else {
if (xml.attrs) {
el_callback(xml);
var cns = xml.childNodes();
each(cns, function (i, v) {
recursive_xml_traversal(v, el_callback);
});
}
}
};
var map_extension_mime_types = {
'html': 'text/html',
'htm': 'text/html',
'ico': 'image/x-icon',
'txt': 'text/plain',
'xml': 'application/xml',
'png': 'image/png',
'gif': 'image/gif',
'jpeg': 'image/jpeg',
'jpg': 'image/jpeg',
'bmp': 'image/bmp',
'svg': 'image/svg+xml',
'mp3': 'audio/mpeg3',
'ogg': 'audio/ogg',
}
var sequential_execute_fs_child_processes = function (arr_fs_process_commands, callback) {
// not sure about doing the unused simultaneous method of multithreading.
// may do that version later.
var l = arr_fs_process_commands.length;
var c = 0;
var process = function () {
var command = arr_fs_process_commands[c];
c++;
//console.log('pre exec command ' + command);
exec(command, function (error, stdout, stderr) {
if (error) {
throw (error);
} else {
if (c < l) {
process();
} else {
callback(null);
}
}
})
}
if (c < l) process();
}
var fs2 = {
'path_parent': function (strPath) {
var dirSep = '/';
//console.log('strPath', strPath);
var pos1 = strPath.lastIndexOf(dirSep);
if (pos1 > -1) {
var beginning = strPath.substr(0, pos1);
//console.log('beginning ' + beginning);
return beginning;
}
},
'path_last_part': function (strPath) {
// the last part is not necessarily a file name, it may be.
var dirSep = '/';
var pos1 = strPath.lastIndexOf(dirSep);
if (pos1 > -1) {
var theRest = strPath.substr(pos1 + 1);
//console.log('theRest ' + theRest);
return theRest;
} else {
return strPath;
}
},
// depth first walk?
// The contents could be deleted when deleting files.
// Could have a callback for the directory itself, and only later go into the contents.
// Recursive delete could have a function return whether to delete it or not, and use rimraf to
// carry out the deletion.
// Getting all directory names may be useful.
// Getting the whole file tree.
// Get all directory names
//
'recursive_dir_names': function (start_path, callback) {
var arr_dir_names = [];
fs2.walk(start_path, function (dir_path, dir_contents) {
arr_dir_names.push(dir_path);
}, function (err, res_all) {
if (err) {
callback(err);
} else {
callback(null, arr_dir_names);
}
})
},
'recursive_dir_delete': function (start_path, fn_check, callback) {
var arr_dir_names = [];
// Walk, but wait until callback of inner function
// walk, async inner.
fs2.walk_async_inner(start_path, function (dir_path, dir_contents, cb) {
//arr_dir_names.push(dir_path);
var to_delete = fn_check(dir_path);
if (to_delete) {
fs2.delete(dir_path, cb);
cb(null, true);
} else {
cb(null, false);
}
}, function (err, res_all) {
if (err) {
callback(err);
} else {
callback(null, arr_dir_names);
}
})
},
// dir_svg_dirs_to_png_dirs
// creates the png directories according to output format.
// could output to multiple formats as well.
// With an asyncronous inner function, where it then checks to see if the directory still exists...?
'walk_async_inner': function (start_path, dir_callback, callback) {
// May also need to know how many to do asymcronously at once.
// may be recursive inside... that could be easier.
// think this does need a callback for call_multi to work right.
console.log('walk start_path', start_path);
var rec = function (path, rcb) {
//console.log('rec path ' + path);
// read the contents from that path.
fs2.dir_contents(path, function (err, contents) {
if (err) {
callback(err);
} else {
//console.log('contents ' + stringify(contents));
// What about an async callback function, possibly where it deletes that directory?
dir_callback(path, contents, function (err, res) {
if (err) {
//callback(err)
} else {
fs2.exists(path, function (err, exists) {
if (exists) {
fs2.dir_contents(path, function (err, contents) {
if (err) {
callback(err);
} else {
if (contents.directories) {
// will use call_multi
var fns = [];
//console.log('contents.directories.length', contents.directories.length);
each(contents.directories, function (directory, i) {
//console.log('directory ' + stringify(directory));
var dirPath = path + '/' + directory;
//log('dirPath ' + dirPath);
fns.push([rec, [dirPath]]);
});
call_multi(1, fns, function (err, res) {
if (err) {
//log('call_multi err ' + err);
throw err;
} else {
//log('call multi cb');
rcb();
}
})
} else {
rcb();
}
}
});
}
})
}
});
// have the directory callback with all the contents here.
// however, need to go through each of the directories, processing them.
// don't want too much code branching.
}
})
};
rec(start_path, callback);
},
'walk': function (start_path, dir_callback, callback) {
// May also need to know how many to do asymcronously at once.
// may be recursive inside... that could be easier.
// think this does need a callback for call_multi to work right.
console.log('walk start_path', start_path);
var rec = function (path, rcb) {
//console.log('rec path ' + path);
// read the contents from that path.
fs2.dir_contents(path, function (err, contents) {
if (err) {
callback(err);
} else {
//console.log('contents ' + stringify(contents));
// What about an async callback function, possibly where it deletes that directory?
dir_callback(path, contents);
// have the directory callback with all the contents here.
// however, need to go through each of the directories, processing them.
// don't want too much code branching.
if (contents.directories) {
// will use call_multi
var fns = [];
//console.log('contents.directories.length', contents.directories.length);
each(contents.directories, function (directory, i) {
//console.log('directory ' + stringify(directory));
var dirPath = path + '/' + directory;
//log('dirPath ' + dirPath);
fns.push([rec, [dirPath]]);
});
call_multi(fns, function (err, res) {
if (err) {
//log('call_multi err ' + err);
throw err;
} else {
//log('call multi cb');
rcb();
}
})
} else {
rcb();
}
}
})
};
rec(start_path, callback);
},
'exists': function (path, callback) {
fs.exists(path, function (res) {
callback(null, res);
})
},
// want to save a binary file as well.
// But loading binary gets more complicated as we need to know the format in many cases, so will have specific functions
// to load typed arrays.
// Seems to work OK.
// Will also want to load binary files.
'save_binary': function (file_path, file_content, callback) {
// the file content could be a typed array.
// just try to create a buffer out of it.
//console.log('save_binary ' + file_path);
if (file_content.buffer.byteLength) {
// its a typed array
console.log('file_content.length', file_content.length);
var buf = Buffer.from(file_content.buffer);
// but does it calculate the buffer length correctly?
console.log('buf.length', buf.length);
fs.open(file_path, "w", function (err, fd) {
if (err) {
callback(err);
} else {
//console.log('file opened');
fs.write(fd, buf, 0, buf.length, function (err, res_write) {
if (err) {
console.log('err', err);
callback(err);
} else {
//console.log('file written');
fs.close(fd);
//console.log('written to file_path ' + file_path);
callback(null, file_path);
}
})
}
});
}
// But does the buf have a bytelength
},
// could put in filter objects...
// so the text begins with something, has an extension, file size
// plain mapify to use here I think.
// may need to modify mapify to support callback functions like arrayify.
// Can save a whole bunch of files as strings.
'save_file_as_string': mapify(fp(function (a, sig) {
//console.log('save_file_as_string sig ' + sig);
// and could JSON stringify an object.
if (sig == '[s,o,f]' || sig == '[s,a,f]') {
var file_path = a[0];
var file_content = JSON.stringify(a[1]);
var callback = a[2];
return this.save_file_as_string(file_path, file_content, callback);
}
if (sig == '[s,s,f]') {
var file_path = a[0];
var file_content = a[1];
var callback = a[2];
//console.log('pre write file file_path', file_path);
fs.writeFile(file_path, file_content, function (err) {
if (err) {
//console.log(err);
callback(err);
} else {
//console.log("The file was saved!");
callback(null, true);
// could return the file path?
// returning timing info would be cool as well.
// could maybe make a function that keeps track of it as a function is executed.
}
});
};
})),
'process_file_as_string': function (file_path, fnProcess, callback) {
fs2.load_file_as_string(file_path, function (err, str_file) {
if (err) {
callback(err);
} else {
fs2.save_file_as_string(file_path, fnProcess(str_file), callback);
}
});
},
// Loading a file as a Uint8 Typed array will become more important.
// Will be using them rather than buffers in many cases.
// Will be easier to interface with C++ code.
// Probably not needed - as buffer is a Uint8Array.
// May be easier to carry out operations on a Uint8Array in general though.
// Also want to be able to load a file as a buffer.
'load_file_as_buffer': fp(function (a, sig) {
//source_path, callback
// should work as an array...
// so when you give it an array of source paths, it loads them all, and returns the result as an array by default.
// can return a map if asked to... would be easier to access in many cases, but problems when there are repeated params.
// I think return map by default.
// Arrayify input, mapify output.
// could use arrayify here possibly...
// could have a concurrency_limit argument.
// would be useful to incorporate that into a lot of code as an option.
var source_path, concurrency_limit = 4,
callback;
// console.log('load_file_as_string sig ' + sig);
if (sig == '[a,n,f]') {
source_path = a[0];
concurrency_limit = a[1];
callback = a[2];
}
if (sig == '[s,n,f]') {
source_path = a[0];
concurrency_limit = a[1];
callback = a[2];
}
if (sig == '[a,f]') {
source_path = a[0];
callback = a[1];
}
if (sig == '[s,f]') {
source_path = a[0];
callback = a[1];
}
if (tof(source_path) == 'string') {
fs.readFile(source_path, function (err, data_buffer) {
if (err) {
//console.log('error reading file at ' + source_path);
//var stack = new Error().stack;
// callback with the error.
//console.log(stack);
//throw err;
callback(err);
} else {
callback(null, data_buffer);
}
//console.log('OK: ' + filename);
//console.log(data)
});
} else if (tof(source_path) == 'array') {
var res = {};
var fns = [];
each(source_path, function (i, source_path_item) {
fns.push([fs2.load_file_as_buffer, [source_path_item], function (err, res_loaded) {
if (err) throw err;
res[source_path_item] = res_loaded;
}]);
});
call_multi(fns, function (err, res_multi) {
if (err) throw err;
callback(null, res);
});
}
//fs.readFile(filename, 'utf8', function(err, data_buffer) {
}),
'load_json_as_object': function (path, callback) {
this.load_file_as_string(path, function (err, str_file) {
if (err) {
callback(err);
} else {
// try/catch?
//return JSON.parse(str_file);
callback(null, JSON.parse(str_file));
}
});
},
'load_file_as_string': fp(function (a, sig) {
//source_path, callback
// should work as an array...
// so when you give it an array of source paths, it loads them all, and returns the result as an array by default.
// can return a map if asked to... would be easier to access in many cases, but problems when there are repeated params.
// I think return map by default.
// Arrayify input, mapify output.
// could use arrayify here possibly...
// could have a concurrency_limit argument.
// would be useful to incorporate that into a lot of code as an option.
var source_path, concurrency_limit = 4,
callback;
//console.log('load_file_as_string sig ' + sig);
if (sig == '[a,n,f]') {
source_path = a[0];
concurrency_limit = a[1];
callback = a[2];
}
if (sig == '[s,n,f]') {
source_path = a[0];
concurrency_limit = a[1];
callback = a[2];
}
if (sig == '[a,f]') {
source_path = a[0];
callback = a[1];
}
if (sig == '[s,f]') {
source_path = a[0];
callback = a[1];
}
if (tof(source_path) == 'string') {
fs.readFile(source_path, function (err, data_buffer) {
if (err) {
callback(err);
} else {
callback(null, data_buffer.toString());
}
//console.log('OK: ' + filename);
//console.log(data)
});
} else if (tof(source_path) == 'array') {
var res = {};
var fns = [];
each(source_path, function (source_path_item) {
fns.push([fs2.load_file_as_string, [source_path_item], function (err, res_loaded) {
if (err) throw err;
res[source_path_item] = res_loaded;
}]);
});
call_multi(fns, function (err, res_multi) {
if (err) throw err;
callback(null, res);
});
}
//fs.readFile(filename, 'utf8', function(err, data_buffer) {
}),
'dir_dirs_beginning': function (dir_path, beginning_text, callback) {
//console.log('dir_path ' + dir_path);
//console.log('dir_path ' + tof(dir_path));
// dir_contents has been arrayified, need to make sure it still works.
//console.log('pre dir_contents');
fs2.dir_contents(dir_path, function (err, dir_contents) {
if (err) {
throw err;
} else {
var directories = dir_contents.directories;
var target_directories = [];
each(directories, function (i, v) {
if (v.substr(0, beginning_text.length) == beginning_text) target_directories.push(v);
});
callback(null, target_directories);
}
})
},
'ensure_directory_exists': function (dir_path, callback) {
fs.exists(dir_path, function (exists) {
if (exists) {
callback(null, true);
} else {
// Maybe go backwards recursively and make sure parents exist.
fs.mkdir(dir_path, function (err) {
if (err) {
console.trace();
console.log('dir_path', dir_path);
throw (err);
} else {
callback(null, true);
}
})
}
})
},
// could use an arrayify function for functions with callbacks.
'dir_ensure': function (dir_path, callback) {
if (tof(dir_path) == 'array') {
var fns = [];
each(dir_path, function (i, v) {
fns.push([fs2.dir_ensure, [v]]);
});
call_multiple_callback_functions(fns, callback)
} else {
fs.exists(dir_path, function (exists) {
if (exists) {
callback(null, dir_path);
} else {
fs.mkdir(dir_path, function (err, res) {
if (err) {
throw err;
} else {
callback(null, dir_path);
}
})
}
});
}
},
// dir_ensure_named_numbered
'dir_ensure_named_numbered': function (parentPath, name, callback) {
// get the list of directories in the parent, make a map.
this.dir_dirs_beginning(parentPath, name, function (err, resDirsBeginning) {
if (err) {
callback(err);
} else {
//console.log('resDirsBeginning ' + stringify(resDirsBeginning));
var map_dirs_beginning = {};
each(resDirsBeginning, function (i, v) {
map_dirs_beginning[v] = true;
});
//console.log('map_dirs_beginning ' + stringify(map_dirs_beginning));
// then loop to generate the name
var c = 0,
genName = name + '_' + c;
while (map_dirs_beginning[genName]) {
c++;
genName = name + '_' + c;
}
// then we create the directory.
var dirPath = parentPath + '/' + genName;
//console.log('dirPath ' + dirPath);
fs2.dir_ensure(dirPath, callback);
}
})
},
// likely to use an extension filer on dir_contents?
// that way could also do a traversal_depth, so it looks in subdirectories
'dir_files_by_extension': fp(function (a, sig) {
var path = a[0],
extension = a[1],
include_metadata = false,
callback;
if (a.l == 3) {
callback = a[2];
}
if (a.l == 4) {
include_metadata = a[2];
callback = a[3];
}
var contents_cb = function (err, dir_contents) {
if (err) {
} else {
//console.log('dir_contents ' + stringify(dir_contents));
var res = [];
each(dir_contents.files, function (i, v) {
//console.log('v ' + stringify(v));
var file_name;
if (tof(v) == 'array') {
file_name = v[0];
}
if (tof(v) == 'string') {
file_name = v[1];
}
var file_extension = node_path.extname(file_name);
if (extension == file_extension) {
res.push(v);
}
})
//return res;
//console.log('res ' + res);
callback(null, res);
}
};
if (include_metadata) {
fs2.dir_contents(path, include_metadata, contents_cb);
} else {
fs2.dir_contents(path, contents_cb);
}
}),
// option include_metadata?
// can include an extension filter.
// dir_dirs?
// and have this arrayified on param 0?
// we may then want to get the contents of a bunch of dirs.
'dir_dirs': fp(function (a, sig) {
// can still apply to multiple dirs.
// source_dir, callback
var source_dir, callback;
//console.log('dir_dirs sig ' + sig);
if (sig == '[s,f]') {
source_dir = a[0];
callback = a[1];
this.dir_contents(source_dir, {
'files_or_directories': 'directories'
}, callback);
}
if (sig == '[s,o,f]') {
source_dir = a[0];
options = a[1];
callback = a[2];
options.files_or_directories = 'directories';
this.dir_contents(source_dir, options, callback);
}
// but do want this to allow a filter as well.
}),
// want to be able to load the file contents too.
// could have this get directory contents recursively to a path... or maybe use the specialised function for that.
'dir_contents': arrayify(0, fp(function (a, sig) {
// path, callback
var path = a[0],
filter, include_metadata = false,
include_file_contents = false,
callback, t_filter;
var fs_paths = false;
// with an extension filter...
var extension_filter;
var regex_filter;
var res_format = 'array';
//console.log('dir_contents sig ' + sig);
// may want to specify options object.
// may want to load the directories, that could be specified as an option.
var files_or_directories;
if (a.l == 2) {
// [s,f]
callback = a[1];
}
if (a.l == 3) {
// [s,s,f]
// it has an extension filter.
if (sig == '[s,s,f]') {
filter = a[1];
extension_filter = filter;
t_filter = 'extension';
callback = a[2];
var options = {};
options.files_or_directories = 'files';
}
// [s,b,f]
// [s,r,f]
// [s,o,f]
// can have an options object there.
// another option can be files_or_directories??? better name?
if (sig == '[s,o,f]') {
//include_metadata = a[1];
var options = a[1];
if (options.include_metadata) include_metadata = options.include_metadata;
if (options.files_or_directories) files_or_directories = options.files_or_directories;
if (options.include_file_contents) include_file_contents = options.include_file_contents;
if (options.res_format) res_format = options.res_format;
if (options.filter) {
filter = options.filter;
t_filter = tof(filter);
// then if t_filter is 'object', there will be more filter properties.
if (t_filter == 'object') {
if (filter.extension) {
//filter = filter.extension;
//t_filter = 'extension';
extension_filter = filter.extension;
}
if (filter.regex) {
//filter = filter.extension;
//t_filter = 'extension';
regex_filter = filter.regex;
}
}
}
if (options.fs_paths) {
fs_paths = options.fs_paths;
//throw 'stop';
}
callback = a[2];
//console.log('sof');
}
if (sig == '[s,b,f]') {
include_metadata = a[1];
callback = a[2];
}
if (sig == '[s,r,f]') {
filter = a[1];
t_filter = 'regex';
regex_filter = filter;
callback = a[2];
}
// regex being the filter
}
if (a.l == 4) {
if (sig == '[s,s,b,f]') {
filter = a[1];
t_filter = 'extension';
include_metadata = a[2];
callback = a[3];
}
if (sig == '[s,s,o,f]') {
filter = a[1];
t_filter = 'extension';
extension_filter = filter;
//include_metadata = a[2];
var options = a[2];
if (options.metadata) {
include_metadata = options.metadata;
}
if (options.include_metadata) {
include_metadata = options.include_metadata;
}
if (options.fs_paths) {
fs_paths = options.fs_paths;
//throw 'stop';
}
if (options.files_or_directories) files_or_directories = options.files_or_directories;
if (options.include_file_contents) include_file_contents = options.include_file_contents;
if (options.res_format) res_format = options.res_format;
callback = a[3];
}
}
// ssbff?
//console.log ('!callback ' + tof(callback));
//path = path.replace(/\//g, '\\');
//console.log('dir_contents path ' + path);
//console.log(fs.existsSync(path));
// readdir working on a bunch of paths automatically?
// or have this function arrayified?
//console.log('pre fs.readdir');
//throw 'stop';
fs.readdir(path, function (err, files) {
if (err) {
//console.log('err ' + err);
var stack = new Error().stack;
console.log(stack);
throw ('err ' + err);
} else {
//console.log('files ' + stringify(files));
//console.log('typeof files ' + stringify(typeof files));
//console.log('dir_contents path ' + path);
var res = {};
var res_files = [];
var res_directories = [];
var c = files.length;
//console.log('c ' + c);
//console.log ('!2callback ' + tof(callback));
var cb = function () {
//console.log('cb ');
//console.log('files_or_directories', files_or_directories);
if (files_or_directories == 'files') {
callback(null, res_files);
} else if (files_or_directories == 'directories') {
callback(null, res_directories);
} else {
if (res_files.length > 0) {
res.files = res_files;
}
if (res_directories.length > 0) {
res.directories = res_directories;
}
//return res;
//console.log('res ' + stringify(res));
//console.log('tof callback ' + tof(callback));
//console.log('callback ' + stringify(callback));
callback(null, res);
}
}
// maybe this could be done sequentially, perhaps doing n at once.
// limit the simultaneous lookups.
// want to do sequential function calls, waiting for the callback on each.
// putting the result callback into the result, but calling it with different items.
if (!files || files.length == 0) {
cb();
}
//console.log('files.length', files.length);
//console.log('files', files);
each(files, function (v, i) {
var file_name = v;
//console.log('file_name', file_name);
var file_or_dir_full_path;
if (path.substr(path.length - 1) == '/') {
file_or_dir_full_path = path + v;
} else {
file_or_dir_full_path = path + dir_separator + v;
}
//console.log('file_or_dir_full_path ' + stringify(file_or_dir_full_path));
//console.log('i ' + stringify(i));
//console.log('v ' + stringify(v));
// find out if it is a directory.
// depending on the name and filter, do the next step.
var proceed = true;
//console.log('extension_filter ' + extension_filter);
//console.log('regex_filter ' + regex_filter);
if (extension_filter) {
var ext = node_path.extname(file_name);
//console.log('ext ' + ext);