UNPKG

4.4 kBJavaScriptView Raw
1'use strict';
2exports.__esModule = true;
3var fs_1 = require("fs");
4var path_1 = require("path");
5var Promise = require("promise");
6var barrage = require('barrage');
7function addFileSystemInfo(stat, info) {
8 stat.name = info.name;
9 stat.fullPath = info.fullPath;
10 stat.path = info.path;
11 return stat;
12}
13function tryStatSync(filename) {
14 try {
15 return fs_1.statSync(filename);
16 }
17 catch (ex) {
18 return null;
19 }
20}
21function lsrSync(dir, options) {
22 if (options === void 0) { options = {}; }
23 var directories = [''];
24 var result = [];
25 var _loop_1 = function () {
26 var subdir = directories.shift();
27 fs_1.readdirSync(path_1.join(dir, subdir))
28 .forEach(function (entry) {
29 if (options.filterPath && !options.filterPath('.' + subdir + '/' + entry))
30 return;
31 var fullPath = path_1.join(dir, subdir, entry);
32 var stat = (options.ignoreErrors
33 ? tryStatSync(fullPath)
34 : fs_1.statSync(fullPath));
35 if (!stat) {
36 return;
37 }
38 var fsEntry = addFileSystemInfo(stat, {
39 path: '.' + subdir + '/' + entry,
40 fullPath: fullPath,
41 name: entry
42 });
43 if (options.filter && !options.filter(fsEntry))
44 return;
45 if (fsEntry.isDirectory()) {
46 directories.push(subdir + '/' + entry);
47 }
48 result.push(fsEntry);
49 });
50 };
51 while (directories.length) {
52 _loop_1();
53 }
54 return result;
55}
56exports.lsrSync = lsrSync;
57function lsrAsyncBase(dir, options, push, end, reject, lazy) {
58 var directories = [''];
59 function rec1() {
60 if (directories.length === 0)
61 return end();
62 var subdir = directories.shift();
63 fs_1.readdir(path_1.join(dir, subdir), function (err, entries) {
64 if (err)
65 return reject(err);
66 entries.reverse();
67 function rec2() {
68 if (entries.length === 0)
69 return rec1();
70 var entry = entries.pop();
71 if (options.filterPath && !options.filterPath('.' + subdir + '/' + entry))
72 return rec2();
73 fs_1.stat(path_1.join(dir, subdir, entry), function (err, stat) {
74 if (err && options.ignoreErrors)
75 return rec2();
76 if (err)
77 return reject(err);
78 var fsEntry = addFileSystemInfo(stat, {
79 path: '.' + subdir + '/' + entry,
80 fullPath: path_1.join(dir, subdir, entry),
81 name: entry
82 });
83 if (options.filter && !options.filter(fsEntry))
84 return rec2();
85 if (stat.isDirectory()) {
86 directories.push(subdir + '/' + entry);
87 }
88 push(fsEntry);
89 lazy(rec2);
90 });
91 }
92 lazy(rec2);
93 });
94 }
95 rec1();
96}
97exports["default"] = lsrAsync;
98function lsrAsync(dir, options, callback) {
99 if (typeof options === 'function') {
100 callback = options;
101 options = undefined;
102 }
103 var opts = (options || {});
104 return new Promise(function (resolve, reject) {
105 var result = [];
106 lsrAsyncBase(dir, opts, result.push.bind(result), resolve.bind(null, result), reject, function (fn) { fn(); });
107 }).nodeify(callback);
108}
109exports.lsrAsync = lsrAsync;
110function lsrStream(dir, options) {
111 options = options || {};
112 var result = new barrage.Readable({ objectMode: true, highWaterMark: options.highWaterMark });
113 var cont = true;
114 var lazy;
115 lazy = function () {
116 lsrAsyncBase(dir, options, function (entry) {
117 cont = result.push(entry);
118 }, function () {
119 result.push(null);
120 }, result.emit.bind(result, 'error'), function (fn) {
121 if (cont)
122 fn();
123 else
124 lazy = fn;
125 });
126 };
127 result._read = function () {
128 cont = true;
129 if (lazy) {
130 var fn = lazy;
131 lazy = null;
132 fn();
133 }
134 };
135 return result;
136}
137exports.lsrStream = lsrStream;