Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Search Index

Generate a serialized lunr.js search index.

Usage

var pkgIndex = require( '@stdlib/_tools/search/pkg-index' );

pkgIndex( [options,] clbk )

Asynchronously generates a serialized lunr.js search index.

pkgIndex( clbk );

function clbk( error, idx ) {
    if ( error ) {
        throw error;
    }
    console.log( idx );
}

The function accepts the following options:

  • dir: root directory from which to search for packages. May be either an absolute file path or a path relative to the current working directory. Default: current working directory.
  • pattern: glob pattern used to find packages. Default: '**/package.json' (note: pattern must end with package.json).
  • ignore: list of glob patterns used to exclude matches.

To search for packages from an alternative directory, set the dir option.

var opts = {
    'dir': '/foo/bar/baz'
};

pkgIndex( opts, clbk );

function clbk( error, idx ) {
    if ( error ) {
        throw error;
    }
    console.log( idx );
}

To provide an alternative include filter, set the pattern option.

var opts = {
    'pattern': '**/foo/**/package.json'
};

pkgIndex( opts, clbk );

function clbk( error, idx ) {
    if ( error ) {
        throw error;
    }
    console.log( idx );
}

To exclude matches, set the ignore option.

var opts = {
    'ignore': [
        'node_modules/**',
        'build/**',
        'reports/**'
    ]
};

pkgIndex( opts, clbk );

function clbk( error, idx ) {
    if ( error ) {
        throw error;
    }
    console.log( idx );
}

Notes

  • If unable to generate a search index (e.g., if unable to resolve package README files), the function returns null.

Examples

var join = require( 'path' ).join;
var lunr = require( 'lunr' );
var rootDir = require( '@stdlib/_tools/utils/root-dir' );
var pkgIndex = require( '@stdlib/_tools/search/pkg-index' );

// Define a directory containing READMEs from which to create a search index:
var dir = join( rootDir(), 'lib', 'node_modules', '@stdlib', 'math', 'base', 'special' );

// Create a search index:
var opts = {
    'dir': dir
};
pkgIndex( opts, onIndex );

function onIndex( error, idx ) {
    var store;
    var res;
    if ( error ) {
        throw error;
    }
    if ( idx === null ) {
        console.error( 'Unable to create search index. Try modifying package search criteria and trying again.' );
        return;
    }
    // Load the serialized index into Lunr:
    store = lunr.Index.load( idx );

    // Perform a search:
    res = store.search( 'sine' );
    console.log( JSON.stringify( res, null, '  ' ) );
}

CLI

Usage

Usage: stdlib-pkg-index [options] [<dir>]

Options:

  -h,    --help                Print this message.
  -V,    --version             Print the package version.
         --pattern pattern     Inclusion glob pattern.
         --ignore pattern      Exclusion glob pattern.

Notes

  • If not provided a dir argument, the current working directory is the search directory.

  • To provide multiple exclusion glob patterns, set multiple --ignore option arguments.

    $ stdlib-pkg-index --ignore=node_modules/** --ignore=build/** --ignore=reports/**

Examples

$ stdlib-pkg-index . > search_index.json