filter-scan-dir 1.4.1 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # v1.5.0
2
+
3
+ - add `fullStat` option to allow enable using the `withFileTypes` option of readdir for huge performance boost (requires node.js 10.10+) [node.js docs](https://nodejs.org/api/fs.html#fsreaddirpath-options-callback).
4
+ - add `concurrency` option to allow using concurrent mode in async version for massive performance boost.
5
+ - convert to typescript
6
+
7
+ # v1.4.1
8
+
9
+ - use fs.lstat instead of fs.stat
10
+
11
+ # v1.4.0
12
+
13
+ - process files before directories
14
+
1
15
  # v1.3.0
2
16
 
3
17
  - `sortFiles` option
package/README.md CHANGED
@@ -12,6 +12,11 @@
12
12
 
13
13
  Recursively scan and filter directory for a flat array of files.
14
14
 
15
+ - Supports super fast concurrent mode in async version.
16
+
17
+ - **[API Docs](https://jchip.github.io/filter-scan-dir/modules.html#filterScanDir)**
18
+ - **[Github](https://github.com/jchip/filter-scan-dir)**
19
+
15
20
  # Install
16
21
 
17
22
  ```bash
@@ -20,105 +25,16 @@ npm install --save filter-scan-dir
20
25
 
21
26
  # Usage
22
27
 
23
- ```js
24
- const filterScanDir = require("filter-scan-dir");
28
+ ```ts
29
+ import { filterScanDir, filterScanDirSync } from "filter-scan-dir";
25
30
 
26
31
  // sync
27
- const files = filterScanDir.sync({ cwd: "test" });
28
- console.log(files);
32
+ console.log(filterScanDirSync({ cwd: "test" }));
29
33
 
30
34
  // async
31
- filterScanDir({ cwd: "test" }).then((files) => {
32
- console.log(files);
33
- });
34
-
35
35
  console.log(await filterScanDir({ cwd: "test" }));
36
36
  ```
37
37
 
38
- ## API
39
-
40
- `filterScanDir(options)` or `filterScanDir(dir)`
41
-
42
- Sync version: `filterScanDir.sync`
43
-
44
- `options`:
45
-
46
- | name | description | default |
47
- | -------------- | -------------------------------------------------------------------------- | --------------- |
48
- | `cwd` | current working directory to start scanning | `process.cwd()` |
49
- | `prefix` | prefix to add to the paths to scan | |
50
- | `prependCwd` | prepend CWD to paths returned | `false` |
51
- | `sortFiles` | sort files from each dir if `true`. | `false` |
52
- | `filter` | callback to filter files. it should return filter result. | |
53
- | `ignoreExt` | array or string of extensions to ignore. ext must include `.`, ie: `".js"` | |
54
- | `filterExt` | array or string of extensions to include only, apply after `ignoreExt`. | |
55
- | `filterDir` | callback to filter directories. it should return filter result | |
56
- | `includeDir` | include directories in result if `true` | |
57
- | `grouping` | enable [grouping](#grouping) if `true` | |
58
- | `maxLevel` | zero base max level of directories to recurse into | `Infinity` |
59
- | `rethrowError` | set to `true` to throw errors instead of ignoring them | `false` |
60
-
61
- `filterDir` and `filter` callback signature:
62
-
63
- ```js
64
- function filter(file, path, extras) {}
65
- ```
66
-
67
- params:
68
-
69
- | name | description |
70
- | ----------------- | ---------------------------------------------- |
71
- | `file` | name of the file being considered |
72
- | `path` | path to directory being processed |
73
- | `extras.stat` | result of `fs.stat` on the file |
74
- | `extras.dirFile` | `Path.join(path, file)` |
75
- | `extras.ext` | extension of the file including `.`, ie: `.js` |
76
- | `extras.noExt` | file name without the extension |
77
- | `extras.fullFile` | `Path.join(cwd, path, file)` |
78
-
79
- should return filter result:
80
-
81
- - `false` - skip the file or directory
82
- - _string_ - name of the group to add the file or directory (need to enable [grouping](#grouping))
83
- - _object_ - `{ group, skip, stop, formatName }` where:
84
- - `group` - name of the group to add the file or directory (need to enable [grouping](#grouping))
85
- - `skip` - if `true` then skip the file or directory, else add it.
86
- - `stop` - stop the scanning and return the result immediately.
87
- - `formatName` - if not `undefined`, then use this as the value to add to the output.
88
-
89
- ## grouping
90
-
91
- If `grouping` is true, then the results will be grouped in an object.
92
-
93
- The `filter` and `filterDir` callback can return a non-empty string as the label of a group.
94
-
95
- Assuming `filter` returns `"group1"` and `"group2"` for some files, the return value will be an object:
96
-
97
- ```js
98
- {
99
- // default group, when filter callback returns non-string truthy value
100
- files: [ "foo" ],
101
- // other groups
102
- group1: [ "file1" ],
103
- group2: [ "file2" ]
104
- }
105
- ```
106
-
107
- ## Path Separator
108
-
109
- By default all paths generated always use forward slash `/` as separator, even on Windows.
110
-
111
- If you really want to force using Windows `\`, then you can pass in `options._path`:
112
-
113
- ```js
114
- const path = require("path");
115
- scanDir({ cwd: "test", _path: path });
116
- ```
117
-
118
- > Internally this is default to `require("path").posix`.
119
-
120
- If you only want to keep `\` in the dir you passed in, then you can just set `_path` to `path.posix`.
121
-
122
38
  # License
123
39
 
124
40
  Licensed under the [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0)
@@ -0,0 +1,126 @@
1
+ /// <reference types="node" />
2
+ import { Dirent, Stats } from "fs";
3
+ /**
4
+ * type of the 3rd argument for the filter callback
5
+ */
6
+ export declare type ExtrasData = {
7
+ /** name of the file being considered */
8
+ file: string;
9
+ /** path to the directory being processed that contains the file */
10
+ path: string;
11
+ /** result from fs.lstat or readdir */
12
+ stat: Dirent | Stats;
13
+ /** full path with cwd + path + file */
14
+ fullFile: string;
15
+ /** path to file without cwd: path + file */
16
+ dirFile: string;
17
+ /** extsion of the file, ie: `.js` */
18
+ ext: string;
19
+ /** file name without the extension */
20
+ noExt: string;
21
+ };
22
+ /**
23
+ * Info from the filter callback to indicate what to do next
24
+ */
25
+ export declare type FilterInfo = {
26
+ /** name of the group to add the file or directory (if grouping is enabled) */
27
+ group?: string;
28
+ /** if `true` then skip the file or directory, else add it */
29
+ skip?: boolean;
30
+ /** stop the scanning and return the result immediately. */
31
+ stop?: boolean;
32
+ /** if not `undefined`, then use this as the value to add to the output */
33
+ formatName?: string;
34
+ };
35
+ /**
36
+ * result from the filter callback
37
+ * - `false` or `undefined` - skip the file or directory
38
+ * - `true` - include the file in the default result
39
+ * - _string_ - name of the group to add the file or directory (if grouping is enabled)
40
+ * - _FilterInfo_ - see type {@link FilterInfo} for details
41
+ */
42
+ export declare type FilterResult = boolean | string | FilterInfo;
43
+ /**
44
+ * filter callback for file or directory entry
45
+ * @param file - name of the file/dir being considered
46
+ * @param path - path to the directory containing the file. Not the full path,
47
+ * just the relative path from CWD. It's empty string for the files in the
48
+ * first level directory.
49
+ * @param extras - extras data
50
+ */
51
+ export declare type FilterCallback = (file: string, path: string, extras: ExtrasData) => FilterResult;
52
+ /**
53
+ * Options for filterScanDir
54
+ */
55
+ export declare type Options = {
56
+ /** current working directory to start scanning */
57
+ cwd?: string;
58
+ /**
59
+ * prefix to add to the paths to scan. It's applied after `cwd`.
60
+ * - The `prefix` will be in the resulting paths. Useful if you want
61
+ * to scan a dir further down from `cwd` but you don't want to prepend
62
+ * CWD to the resulting paths.
63
+ */
64
+ prefix?: string;
65
+ /** prepend CWD to paths returned */
66
+ prependCwd?: boolean;
67
+ /** sort files from each dir */
68
+ sortFiles?: boolean;
69
+ /** include directories in result */
70
+ includeDir?: boolean;
71
+ /** enable grouping of files */
72
+ grouping?: boolean;
73
+ /** zero base max level of directories to recurse into. Default: `Infinity` */
74
+ maxLevel?: number;
75
+ /**
76
+ * use `fs.lstat` to get stat of each file, instead of `readir`'s `withFileTypes` option.
77
+ *
78
+ * - *Default*: `true` - for significant performance improvement, set this to `false`
79
+ *
80
+ */
81
+ fullStat?: boolean;
82
+ /**
83
+ * for async version only - numer of directories to process concurrently. *Default*: `50`
84
+ *
85
+ * - Set this to `0` or `1` to disable concurrent mode
86
+ *
87
+ * NOTE: setting this to a very large number, or `Infinity`, could potentially
88
+ * increase performance very significantly, however, there is a dimishing return
89
+ * and more memory usage, so the default is already fairly good.
90
+ */
91
+ concurrency?: number;
92
+ /** set to `true` to throw errors instead of ignoring them */
93
+ rethrowError?: boolean;
94
+ /** callback to filter files. */
95
+ filter?: FilterCallback;
96
+ /** callback to filter directories. */
97
+ filterDir?: FilterCallback;
98
+ /** array or string of extensions to ignore. ext must include `.`, ie: `".js"` */
99
+ ignoreExt?: string | string[];
100
+ /** array or string of extensions to include only, apply after `ignoreExt` */
101
+ filterExt?: string | string[];
102
+ /**
103
+ * path separator to use to join entries
104
+ *
105
+ * - *Default*: `path.posix.sep`
106
+ * - If you didn't specify this, then `cwd` is automatically converted to use `/`.
107
+ */
108
+ pathSep?: string;
109
+ };
110
+ /**
111
+ * The scanned result if grouping is enabled.
112
+ *
113
+ * - The default `files` group will contain all files not assigned a group
114
+ * - All other files with a group will be assigned to that field
115
+ */
116
+ export declare type GroupingResult = {
117
+ files: string[];
118
+ } & Record<string, string[]>;
119
+ /**
120
+ * async version of filter scan dir
121
+ * @param options
122
+ * @returns
123
+ */
124
+ export declare function filterScanDir(options?: string | Options): Promise<string[] | GroupingResult>;
125
+ /** sync version of filter scan dir */
126
+ export declare function filterScanDirSync(options?: string | Options): string[] | GroupingResult;
@@ -0,0 +1,357 @@
1
+ "use strict";
2
+ /* eslint-disable max-statements, complexity, comma-dangle, max-params, max-depth */
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.filterScanDirSync = exports.filterScanDir = void 0;
8
+ const fs_1 = __importDefault(require("fs"));
9
+ const path_1 = __importDefault(require("path"));
10
+ const util_1 = __importDefault(require("util"));
11
+ function makeExtrasData(file, fullFile, path, stat, options) {
12
+ const dirFile = options._join2(path, file);
13
+ const ix = file.lastIndexOf(".");
14
+ if (ix > 0) {
15
+ return {
16
+ file,
17
+ path,
18
+ stat,
19
+ fullFile,
20
+ dirFile,
21
+ ext: file.substring(ix),
22
+ noExt: file.substring(0, ix),
23
+ };
24
+ }
25
+ else {
26
+ return {
27
+ file,
28
+ path,
29
+ stat,
30
+ fullFile,
31
+ dirFile,
32
+ ext: "",
33
+ noExt: file,
34
+ };
35
+ }
36
+ }
37
+ /**
38
+ * Add a file entry to the result
39
+ *
40
+ * @param result
41
+ * @param options
42
+ * @param extras
43
+ */
44
+ function addResult(result, options, extras) {
45
+ const group = (options.grouping && typeof result === "string" ? result : result && result.group) || "files";
46
+ if (!options.result[group]) {
47
+ options.result[group] = [];
48
+ }
49
+ if (result.formatName !== undefined) {
50
+ options.result[group].push(result.formatName);
51
+ }
52
+ else {
53
+ options.result[group].push(options.prependCwd ? extras.fullFile : extras.dirFile);
54
+ }
55
+ }
56
+ /**
57
+ * Process a directory entry
58
+ *
59
+ * @param options
60
+ * @param extras
61
+ * @returns
62
+ */
63
+ function processDir(options, extras) {
64
+ const filterResult = options.filterDir
65
+ ? options.filterDir(extras.file, extras.path, extras)
66
+ : true;
67
+ if (filterResult) {
68
+ if (filterResult.skip !== true && options.includeDir) {
69
+ addResult(filterResult, options, extras);
70
+ }
71
+ return { stop: filterResult.stop, skip: filterResult.skip };
72
+ }
73
+ return { stop: false, skip: true };
74
+ }
75
+ /**
76
+ * process a file entry
77
+ *
78
+ * @param options
79
+ * @param extras
80
+ * @returns
81
+ */
82
+ function processFile(options, extras) {
83
+ if (options.ignoreExt.length > 0 && options.ignoreExt.indexOf(extras.ext) >= 0) {
84
+ return false;
85
+ }
86
+ if (options.filterExt.length > 0 && options.filterExt.indexOf(extras.ext) < 0) {
87
+ return false;
88
+ }
89
+ const filterResult = options.filter ? options.filter(extras.file, extras.path, extras) : true; // default to include
90
+ if (filterResult) {
91
+ if (filterResult.skip !== true) {
92
+ addResult(filterResult, options, extras);
93
+ }
94
+ return filterResult.stop;
95
+ }
96
+ return false;
97
+ }
98
+ /**
99
+ * get the result base on grouping enable flag
100
+ *
101
+ * @param options
102
+ * @returns
103
+ */
104
+ function getResult(options) {
105
+ return options.grouping
106
+ ? Object.assign({ files: [] }, options.result)
107
+ : options.result.files || [];
108
+ }
109
+ /**
110
+ * compare Dirent for sorting
111
+ *
112
+ * @param a
113
+ * @param b
114
+ * @returns
115
+ */
116
+ const direntCmp = (a, b) => {
117
+ /* istanbul ignore next */
118
+ if (a.name === b.name) {
119
+ /* istanbul ignore next */
120
+ return 0;
121
+ /* istanbul ignore next */
122
+ }
123
+ else if (a.name > b.name) {
124
+ /* istanbul ignore next */
125
+ return 1;
126
+ }
127
+ else {
128
+ /* istanbul ignore next */
129
+ return -1;
130
+ }
131
+ };
132
+ /**
133
+ * sync version of directory walk
134
+ *
135
+ * @param path
136
+ * @param options
137
+ * @param level
138
+ * @returns
139
+ */
140
+ function walkSync(path, options, level = 0) {
141
+ try {
142
+ const dir = options._join2(options.dir, path);
143
+ let files = fs_1.default.readdirSync(dir, options.readdirOpts);
144
+ if (options.sortFiles) {
145
+ if (options.fullStat) {
146
+ files = files.sort();
147
+ }
148
+ else {
149
+ files = files.sort(direntCmp);
150
+ }
151
+ }
152
+ const dirs = [];
153
+ let stop = false;
154
+ // process files first
155
+ for (let ix = 0; !stop && ix < files.length; ix++) {
156
+ const file = files[ix];
157
+ let extras;
158
+ if (options.fullStat) {
159
+ const fullFile = options._join2(dir, file);
160
+ const stat = fs_1.default.lstatSync(fullFile);
161
+ extras = makeExtrasData(file, fullFile, path, stat, options);
162
+ }
163
+ else {
164
+ const fullFile = options._join2(dir, file.name);
165
+ extras = makeExtrasData(file.name, fullFile, path, file, options);
166
+ }
167
+ if (extras.stat.isDirectory()) {
168
+ dirs.push(extras);
169
+ }
170
+ else {
171
+ stop = processFile(options, extras);
172
+ }
173
+ }
174
+ // now process dirs
175
+ if (!stop && dirs.length > 0) {
176
+ for (let ix = 0; ix < dirs.length; ix++) {
177
+ const extras = dirs[ix];
178
+ const flags = processDir(options, extras);
179
+ if (flags.stop) {
180
+ break;
181
+ }
182
+ if (!flags.skip && level < options.maxLevel) {
183
+ walkSync(extras.dirFile, options, level + 1);
184
+ }
185
+ }
186
+ }
187
+ }
188
+ catch (err) {
189
+ if (options.rethrowError) {
190
+ throw err;
191
+ }
192
+ }
193
+ return getResult(options);
194
+ }
195
+ const asyncReaddir = util_1.default.promisify(fs_1.default.readdir);
196
+ const asyncLStat = util_1.default.promisify(fs_1.default.lstat);
197
+ /**
198
+ * async version of dir walk
199
+ *
200
+ * @param path
201
+ * @param options
202
+ * @param level
203
+ * @returns
204
+ */
205
+ async function walk(path, options, level = 0) {
206
+ try {
207
+ const dir = options._join2(options.dir, path);
208
+ let files = await asyncReaddir(dir, options.readdirOpts);
209
+ if (options.sortFiles) {
210
+ if (options.fullStat) {
211
+ files = files.sort();
212
+ }
213
+ else {
214
+ files = files.sort(direntCmp);
215
+ }
216
+ }
217
+ const dirs = [];
218
+ let stop = false;
219
+ // process files first
220
+ for (let ix = 0; !stop && ix < files.length; ix++) {
221
+ const file = files[ix];
222
+ let extras;
223
+ if (options.fullStat) {
224
+ const fullFile = options._join2(dir, file);
225
+ const stat = await asyncLStat(fullFile);
226
+ extras = makeExtrasData(file, fullFile, path, stat, options);
227
+ }
228
+ else {
229
+ const fullFile = options._join2(dir, file.name);
230
+ extras = makeExtrasData(file.name, fullFile, path, file, options);
231
+ }
232
+ if (extras.stat.isDirectory()) {
233
+ dirs.push(extras);
234
+ }
235
+ else {
236
+ stop = processFile(options, extras);
237
+ }
238
+ }
239
+ // now process dirs
240
+ if (!stop && dirs.length > 0) {
241
+ let promises = [];
242
+ for (let ix = 0; ix < dirs.length; ix++) {
243
+ const extras = dirs[ix];
244
+ const flags = processDir(options, extras);
245
+ if (flags.stop) {
246
+ break;
247
+ }
248
+ if (!flags.skip && level < options.maxLevel) {
249
+ const walkP = walk(extras.dirFile, options, level + 1);
250
+ if (options.concurrency > 1) {
251
+ if (options._concurrentCount < options.concurrency) {
252
+ options._concurrentCount++;
253
+ promises.push(walkP);
254
+ }
255
+ else if (promises.length) {
256
+ await Promise.all(promises);
257
+ options._concurrentCount -= promises.length;
258
+ promises = [walkP];
259
+ options._concurrentCount++;
260
+ }
261
+ else {
262
+ await walkP;
263
+ }
264
+ }
265
+ else {
266
+ await walkP;
267
+ }
268
+ }
269
+ }
270
+ if (promises.length) {
271
+ await Promise.all(promises);
272
+ options._concurrentCount -= promises.length;
273
+ promises = [];
274
+ }
275
+ }
276
+ }
277
+ catch (err) {
278
+ if (options.rethrowError) {
279
+ throw err;
280
+ }
281
+ }
282
+ return getResult(options);
283
+ }
284
+ /**
285
+ * make a copy of the user's options with proper defaults
286
+ *
287
+ * @param opts
288
+ * @returns
289
+ */
290
+ function makeOptions(opts) {
291
+ const options = typeof opts === "string" ? { cwd: opts } : opts;
292
+ const sep = options.pathSep || path_1.default.posix.sep;
293
+ let cwd = options.cwd || options.dir || process.cwd();
294
+ if (!options.hasOwnProperty("pathSep") && cwd.includes("\\")) {
295
+ cwd = cwd.replace(/\\/g, "/");
296
+ }
297
+ const cleanExt = (ext) => {
298
+ if (!ext) {
299
+ return ext;
300
+ }
301
+ if (ext.startsWith("*.")) {
302
+ return ext.substring(1);
303
+ }
304
+ if (!ext.startsWith(".")) {
305
+ return `.${ext}`;
306
+ }
307
+ return ext;
308
+ };
309
+ // `includeRoot` was renamed to `prependCwd`, this avoid breaking code still using that
310
+ const prependCwd = (options.hasOwnProperty("includeRoot") ? options.includeRoot : options.prependCwd) ||
311
+ false;
312
+ // a simple and faster version of path.join that handles just 2
313
+ // arguments using posix separator
314
+ const _join2 = (a, b) => (a && b ? a + sep + b : a ? a : b); // eslint-disable-line
315
+ const opts2 = Object.assign({
316
+ _join2,
317
+ maxLevel: Infinity,
318
+ prefix: "",
319
+ prependCwd,
320
+ fullStat: true,
321
+ concurrency: 50,
322
+ readdirOpts: undefined,
323
+ }, options, {
324
+ dir: cwd,
325
+ result: {},
326
+ ignoreExt: []
327
+ .concat(options.ignoreExt)
328
+ .map(cleanExt)
329
+ .filter((x) => x),
330
+ filterExt: []
331
+ .concat(options.filterExt)
332
+ .map(cleanExt)
333
+ .filter((x) => x),
334
+ _concurrentCount: 0,
335
+ });
336
+ if (!opts2.fullStat) {
337
+ opts2.readdirOpts = { withFileTypes: true };
338
+ }
339
+ return opts2;
340
+ }
341
+ /**
342
+ * async version of filter scan dir
343
+ * @param options
344
+ * @returns
345
+ */
346
+ function filterScanDir(options = {}) {
347
+ const options2 = makeOptions(options);
348
+ return walk(options2.prefix, options2);
349
+ }
350
+ exports.filterScanDir = filterScanDir;
351
+ /** sync version of filter scan dir */
352
+ function filterScanDirSync(options = {}) {
353
+ const options2 = makeOptions(options);
354
+ return walkSync(options2.prefix, options2);
355
+ }
356
+ exports.filterScanDirSync = filterScanDirSync;
357
+ //# sourceMappingURL=filter-scan-dir.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"filter-scan-dir.js","sourceRoot":"","sources":["../src/filter-scan-dir.ts"],"names":[],"mappings":";AAAA,oFAAoF;;;;;;AAEpF,4CAAuC;AACvC,gDAAwB;AACxB,gDAAwB;AA2HxB,SAAS,cAAc,CACrB,IAAY,EACZ,QAAgB,EAChB,IAAY,EACZ,IAAoB,EACpB,OAAqB;IAErB,MAAM,OAAO,GAAW,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IACnD,MAAM,EAAE,GAAG,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;IAEjC,IAAI,EAAE,GAAG,CAAC,EAAE;QACV,OAAO;YACL,IAAI;YACJ,IAAI;YACJ,IAAI;YACJ,QAAQ;YACR,OAAO;YACP,GAAG,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC;YACvB,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC;SAC7B,CAAC;KACH;SAAM;QACL,OAAO;YACL,IAAI;YACJ,IAAI;YACJ,IAAI;YACJ,QAAQ;YACR,OAAO;YACP,GAAG,EAAE,EAAE;YACP,KAAK,EAAE,IAAI;SACZ,CAAC;KACH;AACH,CAAC;AAED;;;;;;GAMG;AACH,SAAS,SAAS,CAAC,MAAM,EAAE,OAAqB,EAAE,MAAkB;IAClE,MAAM,KAAK,GACT,CAAC,OAAO,CAAC,QAAQ,IAAI,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,IAAI,MAAM,CAAC,KAAK,CAAC,IAAI,OAAO,CAAC;IAEhG,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,C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B,EAAE;IAC1D,MAAM,QAAQ,GAAG,WAAW,CAAC,OAAO,CAAC,CAAC;IACtC,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;AACzC,CAAC;AAHD,sCAGC;AAED,sCAAsC;AACtC,SAAgB,iBAAiB,CAAC,UAA4B,EAAE;IAC9D,MAAM,QAAQ,GAAG,WAAW,CAAC,OAAO,CAAC,CAAC;IACtC,OAAO,QAAQ,CAAC,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;AAC7C,CAAC;AAHD,8CAGC"}
package/lib/index.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ export * from "../dist/filter-scan-dir.d";
2
+ import { filterScanDir } from "../dist/filter-scan-dir.d";
3
+
4
+ // @ts-ignore
5
+ export = filterScanDir;
package/lib/index.js CHANGED
@@ -1,260 +1,9 @@
1
- "use strict";
2
-
3
- /* eslint-disable max-statements, complexity, comma-dangle, max-params */
4
-
5
- const Fs = require("fs");
6
- const Path = require("path");
7
- const Util = require("util");
8
-
9
- function makeExtrasData(file, fullFile, path, stat, options) {
10
- const dirFile = options._path.join(path, file);
11
- const ix = file.lastIndexOf(".");
12
-
13
- if (ix > 0) {
14
- return {
15
- file,
16
- path,
17
- stat,
18
- fullFile,
19
- dirFile,
20
- ext: file.substr(ix),
21
- noExt: file.substring(0, ix),
22
- };
23
- } else {
24
- return {
25
- file,
26
- path,
27
- stat,
28
- fullFile,
29
- dirFile,
30
- ext: "",
31
- noExt: file,
32
- };
33
- }
34
- }
35
-
36
- function addResult(result, options, extras) {
37
- const group =
38
- (options.grouping && typeof result === "string"
39
- ? result
40
- : result && result.group) || "files";
41
-
42
- if (!options.result[group]) {
43
- options.result[group] = [];
44
- }
45
-
46
- if (result.formatName !== undefined) {
47
- options.result[group].push(result.formatName);
48
- } else {
49
- options.result[group].push(
50
- options.prependCwd ? extras.fullFile : extras.dirFile
51
- );
52
- }
53
- }
54
-
55
- function processDir(options, extras) {
56
- const filterResult = options.filterDir
57
- ? options.filterDir(extras.file, extras.path, extras)
58
- : true;
59
-
60
- if (filterResult) {
61
- if (filterResult.skip !== true && options.includeDir) {
62
- addResult(filterResult, options, extras);
63
- }
64
- return { stop: filterResult.stop, skip: filterResult.skip };
65
- }
66
-
67
- return { stop: false, skip: true };
68
- }
69
-
70
- function processFile(options, extras) {
71
- if (
72
- options.ignoreExt.length > 0 &&
73
- options.ignoreExt.indexOf(extras.ext) >= 0
74
- ) {
75
- return false;
76
- }
77
-
78
- if (
79
- options.filterExt.length > 0 &&
80
- options.filterExt.indexOf(extras.ext) < 0
81
- ) {
82
- return false;
83
- }
84
-
85
- const filterResult = options.filter
86
- ? options.filter(extras.file, extras.path, extras)
87
- : true; // default to include
88
-
89
- if (filterResult) {
90
- if (filterResult.skip !== true) {
91
- addResult(filterResult, options, extras);
92
- }
93
- return filterResult.stop;
94
- }
95
-
96
- return false;
97
- }
98
-
99
- function getResult(options) {
100
- return options.grouping
101
- ? Object.assign({ files: [] }, options.result)
102
- : options.result.files || [];
103
- }
104
-
105
- function walkSync(path, options, level = 0) {
106
- try {
107
- const dir = options._path.join(options.dir, path);
108
- let files = Fs.readdirSync(dir);
109
-
110
- if (options.sortFiles) {
111
- files = files.sort();
112
- }
113
-
114
- const dirs = [];
115
- let stop = false;
116
-
117
- // process files first
118
- for (let ix = 0; !stop && ix < files.length; ix++) {
119
- const file = files[ix];
120
- const fullFile = options._path.join(dir, file);
121
- const stat = Fs.lstatSync(fullFile);
122
-
123
- const extras = makeExtrasData(file, fullFile, path, stat, options);
124
-
125
- if (stat.isDirectory()) {
126
- dirs.push(extras);
127
- } else {
128
- stop = processFile(options, extras);
129
- }
130
- }
131
-
132
- // now process dirs
133
- if (!stop && dirs.length > 0) {
134
- for (let ix = 0; ix < dirs.length; ix++) {
135
- const extras = dirs[ix];
136
- const flags = processDir(options, extras);
137
- if (flags.stop) {
138
- break;
139
- }
140
- if (!flags.skip && level < options.maxLevel) {
141
- walkSync(extras.dirFile, options, level + 1);
142
- }
143
- }
144
- }
145
- } catch (err) {
146
- if (options.rethrowError) {
147
- throw err;
148
- }
149
- }
150
-
151
- return getResult(options);
152
- }
153
-
154
- const asyncReaddir = Util.promisify(Fs.readdir);
155
- const asyncLStat = Util.promisify(Fs.lstat);
156
-
157
- async function walk(path, options, level = 0) {
158
- try {
159
- const dir = options._path.join(options.dir, path);
160
- let files = await asyncReaddir(dir);
161
-
162
- if (options.sortFiles) {
163
- files = files.sort();
164
- }
165
-
166
- const dirs = [];
167
- let stop = false;
168
-
169
- // process files first
170
- for (let ix = 0; !stop && ix < files.length; ix++) {
171
- const file = files[ix];
172
- const fullFile = options._path.join(dir, file);
173
-
174
- const stat = await asyncLStat(fullFile);
175
- const extras = makeExtrasData(file, fullFile, path, stat, options);
176
- if (stat.isDirectory()) {
177
- dirs.push(extras);
178
- } else {
179
- stop = processFile(options, extras);
180
- }
181
- }
182
-
183
- // now process dirs
184
- if (!stop && dirs.length > 0) {
185
- for (let ix = 0; ix < dirs.length; ix++) {
186
- const extras = dirs[ix];
187
- const flags = processDir(options, extras);
188
- if (flags.stop) {
189
- break;
190
- }
191
- if (!flags.skip && level < options.maxLevel) {
192
- await walk(extras.dirFile, options, level + 1);
193
- }
194
- }
195
- }
196
- } catch (err) {
197
- if (options.rethrowError) {
198
- throw err;
199
- }
200
- }
201
-
202
- return getResult(options);
203
- }
204
-
205
- function makeOptions(options) {
206
- let cwd =
207
- (typeof options === "string" ? options : options.cwd || options.dir) ||
208
- process.cwd();
209
-
210
- if (!options._path && cwd.indexOf("\\") >= 0) {
211
- cwd = cwd.replace(/\\/g, "/");
212
- }
213
-
214
- const cleanExt = (ext) => {
215
- if (!ext) {
216
- return ext;
217
- }
218
- if (ext.startsWith("*.")) {
219
- return ext.substring(1);
220
- }
221
- if (!ext.startsWith(".")) {
222
- return `.${ext}`;
223
- }
224
- return ext;
225
- };
226
-
227
- const prependCwd =
228
- (options.hasOwnProperty("includeRoot")
229
- ? options.includeRoot
230
- : options.prependCwd) || false;
231
-
232
- return Object.assign(
233
- { _path: Path.posix, maxLevel: Infinity, prefix: "", prependCwd },
234
- options,
235
- {
236
- dir: cwd,
237
- result: {},
238
- ignoreExt: []
239
- .concat(options.ignoreExt)
240
- .map(cleanExt)
241
- .filter((x) => x),
242
- filterExt: []
243
- .concat(options.filterExt)
244
- .map(cleanExt)
245
- .filter((x) => x),
246
- }
247
- );
248
- }
249
-
250
- function filterScanDir(options) {
251
- const options2 = makeOptions(options);
252
- return walk(options2.prefix, options2);
253
- }
254
-
255
- filterScanDir.sync = function filterScanDirSync(options) {
256
- const options2 = makeOptions(options);
257
- return walkSync(options2.prefix, options2);
258
- };
259
-
260
- module.exports = filterScanDir;
1
+ const fsd = require("../dist/filter-scan-dir");
2
+
3
+ const asyncApi = (options) => fsd.filterScanDir(options);
4
+ Object.defineProperties(asyncApi, {
5
+ sync: { value: fsd.filterScanDirSync },
6
+ filterScanDir: { value: fsd.filterScanDir },
7
+ filterScanDirSync: { value: fsd.filterScanDirSync },
8
+ });
9
+ module.exports = asyncApi;
package/package.json CHANGED
@@ -1,16 +1,20 @@
1
1
  {
2
2
  "name": "filter-scan-dir",
3
- "version": "1.4.1",
3
+ "version": "1.5.0",
4
4
  "description": "Recursively scan and filter directory for a flat array of files",
5
5
  "main": "lib/index.js",
6
+ "types": "lib/index.d.ts",
6
7
  "scripts": {
7
- "test": "clap test-only",
8
+ "build": "rm -rf dist && tsc",
9
+ "test": "xrun -x build xarc/test-only",
8
10
  "lint": "clap lint",
9
11
  "coveralls": "cat coverage/lcov.info | coveralls",
10
- "coverage": "clap check",
11
- "ci:check": "clap -n --serial check coveralls",
12
+ "coverage": "xrun -x build xarc/test-cov",
13
+ "ci:check": "xrun --serial build check coveralls",
12
14
  "prepack": "publish-util-prepack",
13
- "postpack": "publish-util-postpack"
15
+ "postpack": "publish-util-postpack",
16
+ "prepublishOnly": "xrun --serial [[build, docs], xarc/check]",
17
+ "docs": "xrun xarc/docs && touch docs/.nojekyll"
14
18
  },
15
19
  "repository": {
16
20
  "type": "git",
@@ -20,6 +24,7 @@
20
24
  "registry": "https://registry.npmjs.com/"
21
25
  },
22
26
  "files": [
27
+ "dist",
23
28
  "lib"
24
29
  ],
25
30
  "keywords": [
@@ -31,7 +36,9 @@
31
36
  "dir",
32
37
  "directory",
33
38
  "readdir",
34
- "fs"
39
+ "fs",
40
+ "fast",
41
+ "glob"
35
42
  ],
36
43
  "author": "Joel Chen <joel123@gmail.com>",
37
44
  "license": "Apache-2.0",