filter-scan-dir 1.3.0 → 1.5.1

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/README.md CHANGED
@@ -1,7 +1,5 @@
1
1
  # filter-scan-dir
2
2
 
3
- [![dependency status][deps-svg]][deps-url]
4
- [![dev dependency status][dev-deps-svg]][dev-deps-url]
5
3
  [![License][license-image]][license-url]
6
4
  [![build][build-image]][build-url]
7
5
  [![coverage][coverage-image]][coverage-url]
@@ -12,6 +10,11 @@
12
10
 
13
11
  Recursively scan and filter directory for a flat array of files.
14
12
 
13
+ - Supports super fast concurrent mode in async version.
14
+
15
+ - **[API Docs]**
16
+ - **[Github]**
17
+
15
18
  # Install
16
19
 
17
20
  ```bash
@@ -20,104 +23,17 @@ npm install --save filter-scan-dir
20
23
 
21
24
  # Usage
22
25
 
23
- ```js
24
- const filterScanDir = require("filter-scan-dir");
26
+ ```ts
27
+ import { filterScanDir, filterScanDirSync } from "filter-scan-dir";
25
28
 
26
29
  // sync
27
- const files = filterScanDir.sync({ cwd: "test" });
28
- console.log(files);
30
+ console.log(filterScanDirSync({ cwd: "test" }));
29
31
 
30
32
  // async
31
- filterScanDir({ cwd: "test" }).then((files) => {
32
- console.log(files);
33
- });
34
-
35
33
  console.log(await filterScanDir({ cwd: "test" }));
36
34
  ```
37
35
 
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`.
36
+ - **[API Docs]**
121
37
 
122
38
  # License
123
39
 
@@ -138,3 +54,5 @@ Licensed under the [Apache License, Version 2.0](https://www.apache.org/licenses
138
54
  [downloads-image]: https://img.shields.io/npm/dm/filter-scan-dir.svg
139
55
  [downloads-url]: https://npm-stat.com/charts.html?package=filter-scan-dir
140
56
  [npm-badge-png]: https://nodei.co/npm/filter-scan-dir.png?downloads=true&stars=true
57
+ [api docs]: https://jchip.github.io/filter-scan-dir/modules.html#filterScanDir
58
+ [github]: https://github.com/jchip/filter-scan-dir
@@ -0,0 +1,10 @@
1
+ import { Dirent } from "fs";
2
+ /**
3
+ * compare Dirent for sorting
4
+ *
5
+ * @param a
6
+ * @param b
7
+ * @returns
8
+ * @ignore
9
+ */
10
+ export declare const direntCmp: (a: Dirent, b: Dirent) => number;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.direntCmp = void 0;
4
+ /* @ignore */
5
+ /**
6
+ * compare Dirent for sorting
7
+ *
8
+ * @param a
9
+ * @param b
10
+ * @returns
11
+ * @ignore
12
+ */
13
+ const direntCmp = (a, b) => {
14
+ if (a.name === b.name) {
15
+ return 0;
16
+ }
17
+ else if (a.name > b.name) {
18
+ return 1;
19
+ }
20
+ else {
21
+ return -1;
22
+ }
23
+ };
24
+ exports.direntCmp = direntCmp;
25
+ //# sourceMappingURL=dirent-cmp.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dirent-cmp.js","sourceRoot":"","sources":["../src/dirent-cmp.ts"],"names":[],"mappings":";;;AAEA,aAAa;AAEb;;;;;;;GAOG;AACI,MAAM,SAAS,GAAG,CAAC,CAAS,EAAE,CAAS,EAAU,EAAE;IACxD,IAAI,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,IAAI,EAAE;QACrB,OAAO,CAAC,CAAC;KACV;SAAM,IAAI,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,IAAI,EAAE;QAC1B,OAAO,CAAC,CAAC;KACV;SAAM;QACL,OAAO,CAAC,CAAC,CAAC;KACX;AACH,CAAC,CAAC;AARW,QAAA,SAAS,aAQpB"}
@@ -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;
package/dist/index.js ADDED
@@ -0,0 +1,335 @@
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
+ const dirent_cmp_1 = require("./dirent-cmp");
12
+ function makeExtrasData(file, fullFile, path, stat, options) {
13
+ const dirFile = options._join2(path, file);
14
+ const ix = file.lastIndexOf(".");
15
+ if (ix > 0) {
16
+ return {
17
+ file,
18
+ path,
19
+ stat,
20
+ fullFile,
21
+ dirFile,
22
+ ext: file.substring(ix),
23
+ noExt: file.substring(0, ix),
24
+ };
25
+ }
26
+ else {
27
+ return {
28
+ file,
29
+ path,
30
+ stat,
31
+ fullFile,
32
+ dirFile,
33
+ ext: "",
34
+ noExt: file,
35
+ };
36
+ }
37
+ }
38
+ /**
39
+ * Add a file entry to the result
40
+ *
41
+ * @param result
42
+ * @param options
43
+ * @param extras
44
+ */
45
+ function addResult(result, options, extras) {
46
+ const group = (options.grouping && typeof result === "string" ? result : result && result.group) || "files";
47
+ if (!options.result[group]) {
48
+ options.result[group] = [];
49
+ }
50
+ if (result.formatName !== undefined) {
51
+ options.result[group].push(result.formatName);
52
+ }
53
+ else {
54
+ options.result[group].push(options.prependCwd ? extras.fullFile : extras.dirFile);
55
+ }
56
+ }
57
+ /**
58
+ * Process a directory entry
59
+ *
60
+ * @param options
61
+ * @param extras
62
+ * @returns
63
+ */
64
+ function processDir(options, extras) {
65
+ const filterResult = options.filterDir
66
+ ? options.filterDir(extras.file, extras.path, extras)
67
+ : true;
68
+ if (filterResult) {
69
+ if (filterResult.skip !== true && options.includeDir) {
70
+ addResult(filterResult, options, extras);
71
+ }
72
+ return { stop: filterResult.stop, skip: filterResult.skip };
73
+ }
74
+ return { stop: false, skip: true };
75
+ }
76
+ /**
77
+ * process a file entry
78
+ *
79
+ * @param options
80
+ * @param extras
81
+ * @returns
82
+ */
83
+ function processFile(options, extras) {
84
+ if (options.ignoreExt.length > 0 && options.ignoreExt.indexOf(extras.ext) >= 0) {
85
+ return false;
86
+ }
87
+ if (options.filterExt.length > 0 && options.filterExt.indexOf(extras.ext) < 0) {
88
+ return false;
89
+ }
90
+ const filterResult = options.filter ? options.filter(extras.file, extras.path, extras) : true; // default to include
91
+ if (filterResult) {
92
+ if (filterResult.skip !== true) {
93
+ addResult(filterResult, options, extras);
94
+ }
95
+ return filterResult.stop;
96
+ }
97
+ return false;
98
+ }
99
+ /**
100
+ * get the result base on grouping enable flag
101
+ *
102
+ * @param options
103
+ * @returns
104
+ */
105
+ function getResult(options) {
106
+ return options.grouping
107
+ ? Object.assign({ files: [] }, options.result)
108
+ : options.result.files || [];
109
+ }
110
+ /**
111
+ * sync version of directory walk
112
+ *
113
+ * @param path
114
+ * @param options
115
+ * @param level
116
+ * @returns
117
+ */
118
+ function walkSync(path, options, level = 0) {
119
+ try {
120
+ const dir = options._join2(options.dir, path);
121
+ let files = fs_1.default.readdirSync(dir, options.readdirOpts);
122
+ if (options.sortFiles) {
123
+ if (options.fullStat) {
124
+ files = files.sort();
125
+ }
126
+ else {
127
+ files = files.sort(dirent_cmp_1.direntCmp);
128
+ }
129
+ }
130
+ const dirs = [];
131
+ let stop = false;
132
+ // process files first
133
+ for (let ix = 0; !stop && ix < files.length; ix++) {
134
+ const file = files[ix];
135
+ let extras;
136
+ if (options.fullStat) {
137
+ const fullFile = options._join2(dir, file);
138
+ const stat = fs_1.default.lstatSync(fullFile);
139
+ extras = makeExtrasData(file, fullFile, path, stat, options);
140
+ }
141
+ else {
142
+ const fullFile = options._join2(dir, file.name);
143
+ extras = makeExtrasData(file.name, fullFile, path, file, options);
144
+ }
145
+ if (extras.stat.isDirectory()) {
146
+ dirs.push(extras);
147
+ }
148
+ else {
149
+ stop = processFile(options, extras);
150
+ }
151
+ }
152
+ // now process dirs
153
+ if (!stop && dirs.length > 0) {
154
+ for (let ix = 0; ix < dirs.length; ix++) {
155
+ const extras = dirs[ix];
156
+ const flags = processDir(options, extras);
157
+ if (flags.stop) {
158
+ break;
159
+ }
160
+ if (!flags.skip && level < options.maxLevel) {
161
+ walkSync(extras.dirFile, options, level + 1);
162
+ }
163
+ }
164
+ }
165
+ }
166
+ catch (err) {
167
+ if (options.rethrowError) {
168
+ throw err;
169
+ }
170
+ }
171
+ return getResult(options);
172
+ }
173
+ const asyncReaddir = util_1.default.promisify(fs_1.default.readdir);
174
+ const asyncLStat = util_1.default.promisify(fs_1.default.lstat);
175
+ /**
176
+ * async version of dir walk
177
+ *
178
+ * @param path
179
+ * @param options
180
+ * @param level
181
+ * @returns
182
+ */
183
+ async function walk(path, options, level = 0) {
184
+ try {
185
+ const dir = options._join2(options.dir, path);
186
+ let files = await asyncReaddir(dir, options.readdirOpts);
187
+ if (options.sortFiles) {
188
+ if (options.fullStat) {
189
+ files = files.sort();
190
+ }
191
+ else {
192
+ files = files.sort(dirent_cmp_1.direntCmp);
193
+ }
194
+ }
195
+ const dirs = [];
196
+ let stop = false;
197
+ // process files first
198
+ for (let ix = 0; !stop && ix < files.length; ix++) {
199
+ const file = files[ix];
200
+ let extras;
201
+ if (options.fullStat) {
202
+ const fullFile = options._join2(dir, file);
203
+ const stat = await asyncLStat(fullFile);
204
+ extras = makeExtrasData(file, fullFile, path, stat, options);
205
+ }
206
+ else {
207
+ const fullFile = options._join2(dir, file.name);
208
+ extras = makeExtrasData(file.name, fullFile, path, file, options);
209
+ }
210
+ if (extras.stat.isDirectory()) {
211
+ dirs.push(extras);
212
+ }
213
+ else {
214
+ stop = processFile(options, extras);
215
+ }
216
+ }
217
+ // now process dirs
218
+ if (!stop && dirs.length > 0) {
219
+ let promises = [];
220
+ for (let ix = 0; ix < dirs.length; ix++) {
221
+ const extras = dirs[ix];
222
+ const flags = processDir(options, extras);
223
+ if (flags.stop) {
224
+ break;
225
+ }
226
+ if (!flags.skip && level < options.maxLevel) {
227
+ const walkP = walk(extras.dirFile, options, level + 1);
228
+ if (options.concurrency > 1) {
229
+ if (options._concurrentCount < options.concurrency) {
230
+ options._concurrentCount++;
231
+ promises.push(walkP);
232
+ }
233
+ else if (promises.length) {
234
+ await Promise.all(promises);
235
+ options._concurrentCount -= promises.length;
236
+ promises = [walkP];
237
+ options._concurrentCount++;
238
+ }
239
+ else {
240
+ await walkP;
241
+ }
242
+ }
243
+ else {
244
+ await walkP;
245
+ }
246
+ }
247
+ }
248
+ if (promises.length) {
249
+ await Promise.all(promises);
250
+ options._concurrentCount -= promises.length;
251
+ promises = [];
252
+ }
253
+ }
254
+ }
255
+ catch (err) {
256
+ if (options.rethrowError) {
257
+ throw err;
258
+ }
259
+ }
260
+ return getResult(options);
261
+ }
262
+ /**
263
+ * make a copy of the user's options with proper defaults
264
+ *
265
+ * @param opts
266
+ * @returns
267
+ */
268
+ function makeOptions(opts) {
269
+ const options = typeof opts === "string" ? { cwd: opts } : opts;
270
+ const sep = options.pathSep || path_1.default.posix.sep;
271
+ let cwd = options.cwd || options.dir || process.cwd();
272
+ if (!options.hasOwnProperty("pathSep") && cwd.includes("\\")) {
273
+ cwd = cwd.replace(/\\/g, "/");
274
+ }
275
+ const cleanExt = (ext) => {
276
+ if (!ext) {
277
+ return ext;
278
+ }
279
+ if (ext.startsWith("*.")) {
280
+ return ext.substring(1);
281
+ }
282
+ if (!ext.startsWith(".")) {
283
+ return `.${ext}`;
284
+ }
285
+ return ext;
286
+ };
287
+ // `includeRoot` was renamed to `prependCwd`, this avoid breaking code still using that
288
+ const prependCwd = (options.hasOwnProperty("includeRoot") ? options.includeRoot : options.prependCwd) ||
289
+ false;
290
+ // a simple and faster version of path.join that handles just 2
291
+ // arguments using posix separator
292
+ const _join2 = (a, b) => (a && b ? a + sep + b : a || b);
293
+ const opts2 = Object.assign({
294
+ _join2,
295
+ maxLevel: Infinity,
296
+ prefix: "",
297
+ prependCwd,
298
+ fullStat: true,
299
+ concurrency: 50,
300
+ readdirOpts: undefined,
301
+ }, options, {
302
+ dir: cwd,
303
+ result: {},
304
+ ignoreExt: []
305
+ .concat(options.ignoreExt)
306
+ .map(cleanExt)
307
+ .filter((x) => x),
308
+ filterExt: []
309
+ .concat(options.filterExt)
310
+ .map(cleanExt)
311
+ .filter((x) => x),
312
+ _concurrentCount: 0,
313
+ });
314
+ if (!opts2.fullStat) {
315
+ opts2.readdirOpts = { withFileTypes: true };
316
+ }
317
+ return opts2;
318
+ }
319
+ /**
320
+ * async version of filter scan dir
321
+ * @param options
322
+ * @returns
323
+ */
324
+ function filterScanDir(options = {}) {
325
+ const options2 = makeOptions(options);
326
+ return walk(options2.prefix, options2);
327
+ }
328
+ exports.filterScanDir = filterScanDir;
329
+ /** sync version of filter scan dir */
330
+ function filterScanDirSync(options = {}) {
331
+ const options2 = makeOptions(options);
332
+ return walkSync(options2.prefix, options2);
333
+ }
334
+ exports.filterScanDirSync = filterScanDirSync;
335
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,oFAAoF;;;;;;AAEpF,4CAAuC;AACvC,gDAAwB;AACxB,gDAAwB;AACxB,6CAAyC;AA2HzC,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;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,CAAC,QAAQ,CAAC;aACb,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;QACnB,gBAAgB,EAAE,CAAC;KACpB,CACF,CAAC;IAEF,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE;QACnB,KAAK,CAAC,WAAW,GAAG,EAAE,aAAa,EAAE,IAAI,EAAE,CAAC;KAC7C;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;;;GAIG;AACH,SAAgB,aAAa,CAAC,UAA4B,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,219 +1,11 @@
1
1
  "use strict";
2
2
 
3
- /* eslint-disable max-statements, complexity, comma-dangle */
4
-
5
- const Fs = require("fs");
6
- const Path = require("path");
7
- const Util = require("util");
8
-
9
- function processFile(file, options, extras) {
10
- let dirFile = options._path.join(extras.path, file);
11
-
12
- const ix = file.lastIndexOf(".");
13
-
14
- if (ix > 0) {
15
- extras.ext = file.substr(ix);
16
- extras.noExt = file.substring(0, ix);
17
- } else {
18
- extras.ext = "";
19
- extras.noExt = file;
20
- }
21
-
22
- extras.dirFile = dirFile;
23
-
24
- const add = (result) => {
25
- const group =
26
- (options.grouping && typeof result === "string"
27
- ? result
28
- : result && result.group) || "files";
29
-
30
- if (!options.result[group]) {
31
- options.result[group] = [];
32
- }
33
-
34
- if (result.formatName !== undefined) {
35
- options.result[group].push(result.formatName);
36
- } else {
37
- options.result[group].push(
38
- options.prependCwd ? extras.fullFile : dirFile
39
- );
40
- }
41
- };
42
-
43
- let filterResult;
44
-
45
- if (extras.stat.isDirectory()) {
46
- if (options.filterDir) {
47
- filterResult = options.filterDir(file, extras.path, extras);
48
- } else {
49
- filterResult = true;
50
- }
51
-
52
- if (filterResult && filterResult.skip !== true) {
53
- if (options.includeDir) {
54
- add(filterResult);
55
- }
56
- } else {
57
- dirFile = undefined;
58
- }
59
- } else {
60
- if (
61
- options.ignoreExt.length > 0 &&
62
- options.ignoreExt.indexOf(extras.ext) >= 0
63
- ) {
64
- return false;
65
- }
66
- if (
67
- options.filterExt.length > 0 &&
68
- options.filterExt.indexOf(extras.ext) < 0
69
- ) {
70
- return false;
71
- }
72
-
73
- if (options.filter) {
74
- filterResult = options.filter(file, extras.path, extras);
75
- } else {
76
- filterResult = true;
77
- }
78
-
79
- if (filterResult && filterResult.skip !== true) {
80
- add(filterResult);
81
- }
82
-
83
- dirFile = undefined;
84
- }
85
-
86
- return { dirFile, stop: filterResult && filterResult.stop };
87
- }
88
-
89
- function getResult(options) {
90
- return options.grouping
91
- ? Object.assign({ files: [] }, options.result)
92
- : options.result.files || [];
93
- }
94
-
95
- function walkSync(path, options, level = 0) {
96
- try {
97
- const dir = options._path.join(options.dir, path);
98
- let files = Fs.readdirSync(dir);
99
-
100
- if (options.sortFiles) {
101
- files = files.sort();
102
- }
103
-
104
- for (const file of files) {
105
- const fullFile = options._path.join(dir, file);
106
- const stat = Fs.statSync(fullFile);
107
- const result = processFile(file, options, { path, stat, fullFile });
108
-
109
- if (result) {
110
- if (result.stop) {
111
- break;
112
- }
113
-
114
- if (result.dirFile && level < options.maxLevel) {
115
- walkSync(result.dirFile, options, level + 1);
116
- }
117
- }
118
- }
119
- } catch (err) {
120
- if (options.rethrowError) {
121
- throw err;
122
- }
123
- }
124
-
125
- return getResult(options);
126
- }
127
-
128
- const asyncReaddir = Util.promisify(Fs.readdir);
129
- const asyncStat = Util.promisify(Fs.stat);
130
-
131
- async function walk(path, options, level = 0) {
132
- try {
133
- const dir = options._path.join(options.dir, path);
134
- let files = await asyncReaddir(dir);
135
-
136
- if (options.sortFiles) {
137
- files = files.sort();
138
- }
139
-
140
- for (const file of files) {
141
- const fullFile = options._path.join(dir, file);
142
- const stat = await asyncStat(fullFile);
143
- const result = processFile(file, options, { path, stat, fullFile });
144
-
145
- if (result) {
146
- if (result.stop) {
147
- break;
148
- }
149
-
150
- if (result.dirFile && level < options.maxLevel) {
151
- await walk(result.dirFile, options, level + 1);
152
- }
153
- }
154
- }
155
- } catch (err) {
156
- if (options.rethrowError) {
157
- throw err;
158
- }
159
- }
160
-
161
- return getResult(options);
162
- }
163
-
164
- function makeOptions(options) {
165
- let cwd =
166
- (typeof options === "string" ? options : options.cwd || options.dir) ||
167
- process.cwd();
168
-
169
- if (!options._path && cwd.indexOf("\\") >= 0) {
170
- cwd = cwd.replace(/\\/g, "/");
171
- }
172
-
173
- const cleanExt = (ext) => {
174
- if (!ext) {
175
- return ext;
176
- }
177
- if (ext.startsWith("*.")) {
178
- return ext.substring(1);
179
- }
180
- if (!ext.startsWith(".")) {
181
- return `.${ext}`;
182
- }
183
- return ext;
184
- };
185
-
186
- const prependCwd =
187
- (options.hasOwnProperty("includeRoot")
188
- ? options.includeRoot
189
- : options.prependCwd) || false;
190
-
191
- return Object.assign(
192
- { _path: Path.posix, maxLevel: Infinity, prefix: "", prependCwd },
193
- options,
194
- {
195
- dir: cwd,
196
- result: {},
197
- ignoreExt: []
198
- .concat(options.ignoreExt)
199
- .map(cleanExt)
200
- .filter((x) => x),
201
- filterExt: []
202
- .concat(options.filterExt)
203
- .map(cleanExt)
204
- .filter((x) => x),
205
- }
206
- );
207
- }
208
-
209
- function filterScanDir(options) {
210
- const options2 = makeOptions(options);
211
- return walk(options2.prefix, options2);
212
- }
213
-
214
- filterScanDir.sync = function filterScanDirSync(options) {
215
- const options2 = makeOptions(options);
216
- return walkSync(options2.prefix, options2);
217
- };
218
-
219
- module.exports = filterScanDir;
3
+ const fsd = require("../dist");
4
+
5
+ const asyncApi = (options) => fsd.filterScanDir(options);
6
+ Object.defineProperties(asyncApi, {
7
+ sync: { value: fsd.filterScanDirSync },
8
+ filterScanDir: { value: fsd.filterScanDir },
9
+ filterScanDirSync: { value: fsd.filterScanDirSync },
10
+ });
11
+ module.exports = asyncApi;
package/package.json CHANGED
@@ -1,16 +1,20 @@
1
1
  {
2
2
  "name": "filter-scan-dir",
3
- "version": "1.3.0",
3
+ "version": "1.5.1",
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
- "lint": "clap lint",
8
+ "build": "rm -rf dist && tsc",
9
+ "test": "xrun -x build xarc/test-only",
10
+ "lint": "xrun xarc/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",
package/CHANGELOG.md DELETED
@@ -1,6 +0,0 @@
1
- # v1.3.0
2
-
3
- - `sortFiles` option
4
- - support filter callbacks returning `result.formatName`
5
- - CI
6
- - readme badges