filter-scan-dir 1.1.1 → 1.4.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/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ # v1.3.0
2
+
3
+ - `sortFiles` option
4
+ - support filter callbacks returning `result.formatName`
5
+ - CI
6
+ - readme badges
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2021 Joel Chen
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md CHANGED
@@ -1,5 +1,15 @@
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
+ [![License][license-image]][license-url]
6
+ [![build][build-image]][build-url]
7
+ [![coverage][coverage-image]][coverage-url]
8
+
9
+ [![Downloads][downloads-image]][downloads-url]
10
+
11
+ [![npm badge][npm-badge-png]][package-url]
12
+
3
13
  Recursively scan and filter directory for a flat array of files.
4
14
 
5
15
  # Install
@@ -14,15 +24,15 @@ npm install --save filter-scan-dir
14
24
  const filterScanDir = require("filter-scan-dir");
15
25
 
16
26
  // sync
17
- const files = filterScanDir.sync({ dir: "test" });
27
+ const files = filterScanDir.sync({ cwd: "test" });
18
28
  console.log(files);
19
29
 
20
30
  // async
21
- filterScanDir({ dir: "test" }).then(files => {
31
+ filterScanDir({ cwd: "test" }).then((files) => {
22
32
  console.log(files);
23
33
  });
24
34
 
25
- console.log(await filterScanDir({ dir: "test" }));
35
+ console.log(await filterScanDir({ cwd: "test" }));
26
36
  ```
27
37
 
28
38
  ## API
@@ -33,17 +43,20 @@ Sync version: `filterScanDir.sync`
33
43
 
34
44
  `options`:
35
45
 
36
- | name | description | default |
37
- | ------------- | -------------------------------------------------------------------------- | ---------- |
38
- | `dir` | root directory to start scanning | |
39
- | `includeRoot` | add the root dir to the result | |
40
- | `filter` | callback to filter files. it should return filter result. | |
41
- | `ignoreExt` | array or string of extensions to ignore. ext must include `.`, ie: `".js"` | |
42
- | `filterExt` | array or string of extensions to include only, apply after `ignoreExt`. | |
43
- | `filterDir` | callback to filter directories. it should return filter result | |
44
- | `includeDir` | include directories in result if `true` | |
45
- | `grouping` | enable [grouping](#grouping) if `true` | |
46
- | `maxLevel` | zero base max level of directories to recurse into | `Infinity` |
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` |
47
60
 
48
61
  `filterDir` and `filter` callback signature:
49
62
 
@@ -61,16 +74,17 @@ params:
61
74
  | `extras.dirFile` | `Path.join(path, file)` |
62
75
  | `extras.ext` | extension of the file including `.`, ie: `.js` |
63
76
  | `extras.noExt` | file name without the extension |
64
- | `extras.fullFile` | `Path.join(rootDir, path, file)` |
77
+ | `extras.fullFile` | `Path.join(cwd, path, file)` |
65
78
 
66
79
  should return filter result:
67
80
 
68
81
  - `false` - skip the file or directory
69
82
  - _string_ - name of the group to add the file or directory (need to enable [grouping](#grouping))
70
- - _object_ - `{ group, skip, stop }` where:
83
+ - _object_ - `{ group, skip, stop, formatName }` where:
71
84
  - `group` - name of the group to add the file or directory (need to enable [grouping](#grouping))
72
85
  - `skip` - if `true` then skip the file or directory, else add it.
73
- - `stop` - stop the scanning and return the result immediately
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.
74
88
 
75
89
  ## grouping
76
90
 
@@ -98,7 +112,7 @@ If you really want to force using Windows `\`, then you can pass in `options._pa
98
112
 
99
113
  ```js
100
114
  const path = require("path");
101
- scanDir({ dir: "test", _path: path });
115
+ scanDir({ cwd: "test", _path: path });
102
116
  ```
103
117
 
104
118
  > Internally this is default to `require("path").posix`.
@@ -108,3 +122,19 @@ If you only want to keep `\` in the dir you passed in, then you can just set `_p
108
122
  # License
109
123
 
110
124
  Licensed under the [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0)
125
+
126
+ [npm-version-svg]: https://badge.fury.io/js/filter-scan-dir.svg
127
+ [package-url]: https://npmjs.com/package/filter-scan-dir
128
+ [deps-svg]: https://david-dm.org/filter-scan-dir.svg
129
+ [deps-url]: https://david-dm.org/filter-scan-dir
130
+ [dev-deps-svg]: https://david-dm.org/filter-scan-dir/dev-status.svg
131
+ [dev-deps-url]: https://david-dm.org/filter-scan-dir#info=devDependencies
132
+ [license-image]: https://img.shields.io/npm/l/filter-scan-dir.svg
133
+ [license-url]: LICENSE
134
+ [build-image]: https://github.com/jchip/filter-scan-dir/actions/workflows/node.js.yml/badge.svg
135
+ [build-url]: https://github.com/jchip/filter-scan-dir/actions/workflows/node.js.yml
136
+ [coverage-image]: https://coveralls.io/repos/github/jchip/filter-scan-dir/badge.svg?branch=main
137
+ [coverage-url]: https://coveralls.io/github/jchip/filter-scan-dir?branch=main
138
+ [downloads-image]: https://img.shields.io/npm/dm/filter-scan-dir.svg
139
+ [downloads-url]: https://npm-stat.com/charts.html?package=filter-scan-dir
140
+ [npm-badge-png]: https://nodei.co/npm/filter-scan-dir.png?downloads=true&stars=true
package/lib/index.js CHANGED
@@ -1,83 +1,99 @@
1
1
  "use strict";
2
2
 
3
- /* eslint-disable max-statements, complexity */
3
+ /* eslint-disable max-statements, complexity, comma-dangle, max-params */
4
4
 
5
5
  const Fs = require("fs");
6
6
  const Path = require("path");
7
7
  const Util = require("util");
8
8
 
9
- function process(file, options, extras) {
10
- let dirFile = options._path.join(extras.path, file);
11
-
9
+ function makeExtrasData(file, fullFile, path, stat, options) {
10
+ const dirFile = options._path.join(path, file);
12
11
  const ix = file.lastIndexOf(".");
13
12
 
14
13
  if (ix > 0) {
15
- extras.ext = file.substr(ix);
16
- extras.noExt = file.substring(0, ix);
14
+ return {
15
+ file,
16
+ path,
17
+ stat,
18
+ fullFile,
19
+ dirFile,
20
+ ext: file.substr(ix),
21
+ noExt: file.substring(0, ix),
22
+ };
17
23
  } else {
18
- extras.ext = "";
19
- extras.noExt = file;
24
+ return {
25
+ file,
26
+ path,
27
+ stat,
28
+ fullFile,
29
+ dirFile,
30
+ ext: "",
31
+ noExt: file,
32
+ };
20
33
  }
34
+ }
21
35
 
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";
36
+ function addResult(result, options, extras) {
37
+ const group =
38
+ (options.grouping && typeof result === "string"
39
+ ? result
40
+ : result && result.group) || "files";
29
41
 
30
- if (!options.result[group]) {
31
- options.result[group] = [];
32
- }
42
+ if (!options.result[group]) {
43
+ options.result[group] = [];
44
+ }
33
45
 
34
- options.result[group].push(options.includeRoot ? extras.fullFile : dirFile);
35
- };
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
+ }
36
54
 
37
- let filterResult;
55
+ function processDir(options, extras) {
56
+ const filterResult = options.filterDir
57
+ ? options.filterDir(extras.file, extras.path, extras)
58
+ : true;
38
59
 
39
- if (extras.stat.isDirectory()) {
40
- if (options.filterDir) {
41
- filterResult = options.filterDir(file, extras.path, extras);
42
- } else {
43
- filterResult = true;
60
+ if (filterResult) {
61
+ if (filterResult.skip !== true && options.includeDir) {
62
+ addResult(filterResult, options, extras);
44
63
  }
64
+ return { stop: filterResult.stop, skip: filterResult.skip };
65
+ }
45
66
 
46
- if (filterResult && filterResult.skip !== true) {
47
- if (options.includeDir) {
48
- add(filterResult);
49
- }
50
- } else {
51
- dirFile = undefined;
52
- }
53
- } else {
54
- if (
55
- options.ignoreExt.length > 0 &&
56
- options.ignoreExt.indexOf(extras.ext) >= 0
57
- ) {
58
- return false;
59
- }
60
- if (
61
- options.filterExt.length > 0 &&
62
- options.filterExt.indexOf(extras.ext) < 0
63
- ) {
64
- return false;
65
- }
67
+ return { stop: false, skip: true };
68
+ }
66
69
 
67
- if (options.filter) {
68
- filterResult = options.filter(file, extras.path, extras);
69
- } else {
70
- filterResult = true;
71
- }
70
+ function processFile(options, extras) {
71
+ if (
72
+ options.ignoreExt.length > 0 &&
73
+ options.ignoreExt.indexOf(extras.ext) >= 0
74
+ ) {
75
+ return false;
76
+ }
72
77
 
73
- if (filterResult && filterResult.skip !== true) {
74
- add(filterResult);
75
- }
78
+ if (
79
+ options.filterExt.length > 0 &&
80
+ options.filterExt.indexOf(extras.ext) < 0
81
+ ) {
82
+ return false;
83
+ }
76
84
 
77
- dirFile = undefined;
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;
78
94
  }
79
95
 
80
- return { dirFile, stop: filterResult && filterResult.stop };
96
+ return false;
81
97
  }
82
98
 
83
99
  function getResult(options) {
@@ -89,68 +105,113 @@ function getResult(options) {
89
105
  function walkSync(path, options, level = 0) {
90
106
  try {
91
107
  const dir = options._path.join(options.dir, path);
92
- const files = Fs.readdirSync(dir);
108
+ let files = Fs.readdirSync(dir);
109
+
110
+ if (options.sortFiles) {
111
+ files = files.sort();
112
+ }
113
+
114
+ const dirs = [];
115
+ let stop = false;
93
116
 
94
- for (const file of files) {
117
+ // process files first
118
+ for (let ix = 0; !stop && ix < files.length; ix++) {
119
+ const file = files[ix];
95
120
  const fullFile = options._path.join(dir, file);
96
- const stat = Fs.statSync(fullFile);
97
- const result = process(file, options, { path, stat, fullFile });
121
+ const stat = Fs.lstatSync(fullFile);
98
122
 
99
- if (result) {
100
- if (result.stop) {
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) {
101
138
  break;
102
139
  }
103
-
104
- if (result.dirFile && level < options.maxLevel) {
105
- walkSync(result.dirFile, options, level + 1);
140
+ if (!flags.skip && level < options.maxLevel) {
141
+ walkSync(extras.dirFile, options, level + 1);
106
142
  }
107
143
  }
108
144
  }
109
145
  } catch (err) {
110
- // console.log(err);
146
+ if (options.rethrowError) {
147
+ throw err;
148
+ }
111
149
  }
112
150
 
113
151
  return getResult(options);
114
152
  }
115
153
 
116
154
  const asyncReaddir = Util.promisify(Fs.readdir);
117
- const asyncStat = Util.promisify(Fs.stat);
155
+ const asyncLStat = Util.promisify(Fs.lstat);
118
156
 
119
157
  async function walk(path, options, level = 0) {
120
158
  try {
121
159
  const dir = options._path.join(options.dir, path);
122
- const files = await asyncReaddir(dir);
160
+ let files = await asyncReaddir(dir);
161
+
162
+ if (options.sortFiles) {
163
+ files = files.sort();
164
+ }
123
165
 
124
- for (const file of files) {
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];
125
172
  const fullFile = options._path.join(dir, file);
126
- const stat = await asyncStat(fullFile);
127
- const result = process(file, options, { path, stat, fullFile });
128
173
 
129
- if (result) {
130
- if (result.stop) {
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) {
131
189
  break;
132
190
  }
133
-
134
- if (result.dirFile && level < options.maxLevel) {
135
- await walk(result.dirFile, options, level + 1);
191
+ if (!flags.skip && level < options.maxLevel) {
192
+ await walk(extras.dirFile, options, level + 1);
136
193
  }
137
194
  }
138
195
  }
139
196
  } catch (err) {
140
- // console.log(err);
197
+ if (options.rethrowError) {
198
+ throw err;
199
+ }
141
200
  }
142
201
 
143
202
  return getResult(options);
144
203
  }
145
204
 
146
205
  function makeOptions(options) {
147
- let dir = typeof options === "string" ? options : options.dir;
206
+ let cwd =
207
+ (typeof options === "string" ? options : options.cwd || options.dir) ||
208
+ process.cwd();
148
209
 
149
- if (!options._path && dir.indexOf("\\") >= 0) {
150
- dir = dir.replace(/\\/g, "/");
210
+ if (!options._path && cwd.indexOf("\\") >= 0) {
211
+ cwd = cwd.replace(/\\/g, "/");
151
212
  }
152
213
 
153
- const cleanExt = ext => {
214
+ const cleanExt = (ext) => {
154
215
  if (!ext) {
155
216
  return ext;
156
217
  }
@@ -163,26 +224,37 @@ function makeOptions(options) {
163
224
  return ext;
164
225
  };
165
226
 
166
- return Object.assign({ _path: Path.posix, maxLevel: Infinity }, options, {
167
- dir,
168
- result: {},
169
- ignoreExt: []
170
- .concat(options.ignoreExt)
171
- .map(cleanExt)
172
- .filter(x => x),
173
- filterExt: []
174
- .concat(options.filterExt)
175
- .map(cleanExt)
176
- .filter(x => x)
177
- });
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
+ );
178
248
  }
179
249
 
180
250
  function filterScanDir(options) {
181
- return walk("", makeOptions(options));
251
+ const options2 = makeOptions(options);
252
+ return walk(options2.prefix, options2);
182
253
  }
183
254
 
184
255
  filterScanDir.sync = function filterScanDirSync(options) {
185
- return walkSync("", makeOptions(options));
256
+ const options2 = makeOptions(options);
257
+ return walkSync(options2.prefix, options2);
186
258
  };
187
259
 
188
260
  module.exports = filterScanDir;
package/package.json CHANGED
@@ -1,11 +1,15 @@
1
1
  {
2
2
  "name": "filter-scan-dir",
3
- "version": "1.1.1",
3
+ "version": "1.4.1",
4
4
  "description": "Recursively scan and filter directory for a flat array of files",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
7
7
  "test": "clap test-only",
8
+ "lint": "clap lint",
9
+ "coveralls": "cat coverage/lcov.info | coveralls",
8
10
  "coverage": "clap check",
11
+ "ci:check": "clap -n --serial check coveralls",
12
+ "prepack": "publish-util-prepack",
9
13
  "postpack": "publish-util-postpack"
10
14
  },
11
15
  "repository": {