2.0.0 • Published 4 years ago

@jsenv/file-collector v2.0.0

Weekly downloads
8
License
MIT
Repository
github
Last release
4 years ago

File collector

github package npm package github ci codecov coverage

Collect subset of files from a directory.

Table of contents

Presentation

This github repository corresponds to @jsenv/file-collector package published on github and npm package registries.

@jsenv/file-collector can be used to recursively collect files inside a directory. You can collect only a subset of files using basic pattern matching.

collectFiles example

collectFiles is an async function collectings a subset of files inside a directory

Implemented in src/collectFiles.js and could be used as shown below.

import { collectFiles } from "@jsenv/file-collector"

const files = await collectFiles({
  directoryUrl: "file:///Users/you/directory",
  specifierMetaMap: {
    "./**/*.js": {
      whatever: 42,
    },
  },
  predicate: (meta) => {
    return meta.whatever === 42
  },
})

collectFiles parameters

directoryPath

directoryUrl is a string leading to a directory on your filesystem.

This parameter is required, an example value could be:

"file:///Users/you/directory"

Values like "/Users/you/directory" or "C:\\Users\\you\\directory" are also accepted.

specifierMetaMap

specifierMetaMap is an object associating specifier that looks like file paths to arbitrary objects called meta.

This parameter is required, an example value could be:

{
  "./**/*.js": {
    whatever: 42,
  }
}

predicate

predicate is a function responsible to decide if the file will be collected.

This parameter is required, an example value could be:

(meta) => meta.whatever === 42

collectFiles return value

collectFiles return value is called matchingFileResultArray

matchingFileResultArray

matchingFileResultArray is an array of matchingFileResult representing collected files.

See below an example of matchingFileResultArray:

[
  {
    relativeUrl: 'directory/file.js',
    meta: { whatever: 42 },
    lstat: {} // Object returned by node.js lstat on that file,
    operationResult: undefined, // not documented, ignore this
  }
]

Installation

If you have never installed a jsenv package, read Installing a jsenv package before going further.

This documentation is up-to-date with a specific version so prefer any of the following commands

npm install --save-dev @jsenv/file-collector@2.0.0
yarn add --dev @jsenv/file-collector@2.0.0