ModuleParser

ModuleParser

new ModuleParser()

The ModuleParser is a utility class designed to loop through and iterate
on a directory and pull out of each .js file found, any classes or exports
that extend from GQLBase or a child of GQLBase.

Since:
  • 2.7.0
Source:

Classes

exports.ModuleParser

Methods

(static) ⌾⠀arrayToPattern(extensions, flags) → {RegExp}

The ModuleParser should only parse files that match the default or
supplied file extensions. The default list contains .js, .jsx, .ts
and .tsx; so JavaScript or TypeScript files and their JSX React
counterparts

Since the list is customizable for a usage, however, it makes sense
to have a function that will match what is supplied rather than
creating a constant expression to use instead.

Since:
  • 2.13.0
Source:
Parameters:
Name Type Description
extensions Array.<string>

an array of extensions to
convert to a regular expression that would pass for each

flags string

the value passed to a new RegExp denoting the
flags used in the pattern; defaults to 'i' for case insensitivity

Returns:
Type:
RegExp

a regular expression object matching the contents
of the array of extensions or the default extensions and that will
also match those values in a case insensitive manner

(static) ⌾⠀checkForPackageExtensions(toString) → (nullable) {Array.<string>}

Using the module read-pkg-up, finds the nearest package.json file
and checks to see if it has a .lattice.moduleParser.extensions' preference. If so, if the value is an array, that value is used, otherwise the value is wrapped in an array. If the optional parametertoStringistruethen.toString()` will be invoked on any non
Array values found; this behavior is the default

Since:
  • 2.13.0
Source:
Parameters:
Name Type Description
toString boolean

true if any non-array values should have
their .toString() method invoked before being wrapped in an Array;
defaults to true

Returns:
Type:
Array.<string>

null if no value is set for the property
lattice.ModuleParser.extensions in package.json or the value
of the setting if it is an array. Finally if the value is set but is
not an array, the specified value wrapped in an array is returned

⌾⠀findGQLBaseClasses(contents, gqlDefinitions) → {Set.<mixed>}

Given an object, typically the result of a require() or import
command, iterate over its contents and find any GQLBase derived
exports. Continually, and recursively, build this list of classes out
so that we can add them to a GQLExpressMiddleware.

Since:
  • 2.7.0
Source:
Parameters:
Name Type Description
contents Object

the object to parse for properties extending
from GQLBase

gqlDefinitions Array.<GQLBase>

the results, allowed as a second
parameter during recursion as a means to save state between calls

Returns:
Type:
Set.<mixed>

a unique set of values that are currently being
iterated over. Passed in as a third parameter to save state between calls
during recursion.

⌾⠀importClass(filePath) → {Object}

Given a file path, this method will attempt to import/require the
file in question and return the object it exported; whatever that
may be.

Since:
  • 2.7.0
Source:
Parameters:
Name Type Description
filePath string

a path to pass to require()

Returns:
Type:
Object

the object, or undefined, that was returned when
it was require()'ed.

(async) ⌾⠀parse() → {Promise.<Array.<GQLBase>>}

This method takes a instance of ModuleParser, initialized with a directory,
and walks its contents, importing files as they are found, and sorting
any exports that extend from GQLBase into an array of such classes
in a resolved promise.

Since:
  • 2.7.0
Source:
Returns:
Type:
Promise.<Array.<GQLBase>>

an array GQLBase classes, or an empty
array if none could be identified.

(async) ⌾⠀parseSync() → {Array.<GQLBase>}

This method takes a instance of ModuleParser, initialized with a directory,
and walks its contents, importing files as they are found, and sorting
any exports that extend from GQLBase into an array of such classes

Since:
  • 2.7.0
Source:
Returns:
Type:
Array.<GQLBase>

an array GQLBase classes, or an empty
array if none could be identified.

(async, inner) ⌾⠀walk(dir, filelist) → {Promise.<Array.<string>>}

Recursively walks a directory and returns an array of asbolute file paths
to the files under the specified directory.

Since:
  • 2.7.0
Source:
Parameters:
Name Type Description
dir string

string path to the top level directory to parse

filelist Array.<string>

an array of existing absolute file paths,
or if not parameter is supplied a default empty array will be used.

Returns:
Type:
Promise.<Array.<string>>

an array of existing absolute file paths
found under the supplied dir directory.

(async, inner) ⌾⠀walkSync(dir, filelist) → {Array.<string>}

Recursively walks a directory and returns an array of asbolute file paths
to the files under the specified directory. This version does this in a
synchronous fashion.

Since:
  • 2.7.0
Source:
Parameters:
Name Type Description
dir string

string path to the top level directory to parse

filelist Array.<string>

an array of existing absolute file paths,
or if not parameter is supplied a default empty array will be used.

Returns:
Type:
Array.<string>

an array of existing absolute file paths found
under the supplied dir directory.

(inner) ⎆⠀constructor(directory)

The constructor

Source:
Parameters:
Name Type Description
directory string

a string path to a directory containing the
various GQLBase extended classes that should be gathered.