Import and Require Paths

List import and require paths.

Usage

var ls = require( '@stdlib/_tools/modules/import-require-glob' );

ls( [options], clbk )

Reads files and lists import and require paths.

ls( onList );

function onList( error, results ) {
    if ( error ) {
        throw error;
    }
    console.dir( results );
    // => [{...},{...},...]
}

Each file is represented by an object with the following fields:

  • file: file name.
  • literals: an array of literal string import and require paths.
  • expressions: an array of import and require path expressions.

The function accepts the following options:

  • dir: root directory from which to search for files. May be either an absolute or relative directory path. Default: current working directory.
  • pattern: file glob pattern. Default: **/*.js.

By default, the function will search for files in the current working directory. To specify an alternative directory, set the dir option.

var join = require( 'path' ).join;

var opts = {
    'dir': join( __dirname, 'tests', 'fixtures' )
};

ls( opts, onList );

function onList( error, results ) {
    if ( error ) {
        throw error;
    }
    console.dir( results );
    // => [{"file":"...","literals":[...],"expressions":[...]}]
}

To specify a file glob pattern, set the pattern option.

var opts = {
    'pattern': '**/*.txt'
};

ls( opts, onList );

function onList( error, results ) {
    if ( error ) {
        throw error;
    }
    console.dir( results );
}

ls.sync( [options] )

Synchronously reads files and lists import and require paths.

var results = ls.sync();
// returns [{...},{...},...]

The function accepts the same options as ls() above.

Examples

var ls = require( '@stdlib/_tools/modules/import-require-glob' );

ls( onList );

function onList( error, results ) {
    if ( error ) {
        throw error;
    }
    console.dir( results );
}

CLI

Usage

Usage: import-require-glob [options] [<dirname>]

Options:

  -h,    --help                      Print this message.
  -V,    --version                   Print the package version.
         --pattern pattern           File glob pattern.

Notes

  • Results are printed to stdout as newline-delimited JSON (NDJSON).

Examples

$ DEBUG=* import-require-glob
{"file":"...","literals":[...],"expressions":[...]}
{"file":"...","literals":[...],"expressions":[...]}
...