filter-scan-dir v1.5.5
filter-scan-dir
Recursively scan and filter directory for a flat array of files.
Install
npm install --save filter-scan-dir
Usage
const filterScanDir = require("filter-scan-dir");
// sync
const files = filterScanDir.sync({ cwd: "test" });
console.log(files);
// async
filterScanDir({ cwd: "test" }).then((files) => {
console.log(files);
});
console.log(await filterScanDir({ cwd: "test" }));
API
filterScanDir(options)
or filterScanDir(dir)
Sync version: filterScanDir.sync
options
:
name | description | default |
---|---|---|
cwd | current working directory to start scanning | process.cwd() |
prefix | prefix to add to the paths to scan | |
prependCwd | prepend CWD to paths returned | false |
sortFiles | sort files from each dir if true . | false |
filter | callback to filter files. it should return filter result. | |
ignoreExt | array or string of extensions to ignore. ext must include . , ie: ".js" | |
filterExt | array or string of extensions to include only, apply after ignoreExt . | |
filterDir | callback to filter directories. it should return filter result | |
includeDir | include directories in result if true | |
grouping | enable grouping if true | |
maxLevel | zero base max level of directories to recurse into | Infinity |
rethrowError | set to true to throw errors instead of ignoring them | false |
filterDir
and filter
callback signature:
function filter(file, path, extras) {}
params:
name | description |
---|---|
file | name of the file being considered |
path | path to directory being processed |
extras.stat | result of fs.stat on the file |
extras.dirFile | Path.join(path, file) |
extras.ext | extension of the file including . , ie: .js |
extras.noExt | file name without the extension |
extras.fullFile | Path.join(cwd, path, file) |
should return filter result:
false
- skip the file or directory- string - name of the group to add the file or directory (need to enable grouping)
- object -
{ group, skip, stop, formatName }
where:group
- name of the group to add the file or directory (need to enable grouping)skip
- iftrue
then skip the file or directory, else add it.stop
- stop the scanning and return the result immediately.formatName
- if notundefined
, then use this as the value to add to the output.
grouping
If grouping
is true, then the results will be grouped in an object.
The filter
and filterDir
callback can return a non-empty string as the label of a group.
Assuming filter
returns "group1"
and "group2"
for some files, the return value will be an object:
{
// default group, when filter callback returns non-string truthy value
files: [ "foo" ],
// other groups
group1: [ "file1" ],
group2: [ "file2" ]
}
Path Separator
By default all paths generated always use forward slash /
as separator, even on Windows.
If you really want to force using Windows \
, then you can pass in options._path
:
const path = require("path");
scanDir({ cwd: "test", _path: path });
Internally this is default to
require("path").posix
.
If you only want to keep \
in the dir you passed in, then you can just set _path
to path.posix
.
License
Licensed under the Apache License, Version 2.0
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago