1.5.5 • Published 2 years ago

filter-scan-dir v1.5.5

Weekly downloads
360
License
Apache-2.0
Repository
github
Last release
2 years ago

filter-scan-dir

dependency status dev dependency status License build coverage

Downloads

npm badge

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:

namedescriptiondefault
cwdcurrent working directory to start scanningprocess.cwd()
prefixprefix to add to the paths to scan
prependCwdprepend CWD to paths returnedfalse
sortFilessort files from each dir if true.false
filtercallback to filter files. it should return filter result.
ignoreExtarray or string of extensions to ignore. ext must include ., ie: ".js"
filterExtarray or string of extensions to include only, apply after ignoreExt.
filterDircallback to filter directories. it should return filter result
includeDirinclude directories in result if true
groupingenable grouping if true
maxLevelzero base max level of directories to recurse intoInfinity
rethrowErrorset to true to throw errors instead of ignoring themfalse

filterDir and filter callback signature:

function filter(file, path, extras) {}

params:

namedescription
filename of the file being considered
pathpath to directory being processed
extras.statresult of fs.stat on the file
extras.dirFilePath.join(path, file)
extras.extextension of the file including ., ie: .js
extras.noExtfile name without the extension
extras.fullFilePath.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 - if true then skip the file or directory, else add it.
    • stop - stop the scanning and return the result immediately.
    • formatName - if not undefined, 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