Generate a serialized lunr.js search index.
var pkgIndex = require( '@stdlib/_tools/search/pkg-index' );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 withpackage.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 );
}- If unable to generate a search index (e.g., if unable to resolve package README files), the function returns
null.
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, ' ' ) );
}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.-
If not provided a
dirargument, the current working directory is the search directory. -
To provide multiple exclusion glob patterns, set multiple
--ignoreoption arguments.$ stdlib-pkg-index --ignore=node_modules/** --ignore=build/** --ignore=reports/**
$ stdlib-pkg-index . > search_index.json