text: /**
  * @license Apache-2.0
  *
  * Copyright (c) 2018 The Stdlib Authors.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  *    http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
  
  'use strict';
  
  // MODULES //
  
  var path = require( 'path' );
  var logger = require( 'debug' );
  var readFileList = require( '@stdlib/fs/read-file-list' );
  var isFunction = require( '@stdlib/assert/is-function' );
  var copy = require( '@stdlib/utils/copy' );
  var cwd = require( '@stdlib/process/cwd' );
  var findReadmes = require( '@stdlib/_tools/pkgs/readmes' );
  var createIndex = require( './create.js' );
  var defaults = require( './defaults.json' );
  var validate = require( './validate.js' );
  
  
  // VARIABLES //
  
  var debug = logger( 'search:pkg-index:main' );
  var FOPTS = {
  	'encoding': 'utf-8'
  };
  
  
  // MAIN //
  
  /**
  * Creates a serialized search index for all packages.
  *
  * @param {Options} [options] - function options
  * @param {string} [options.dir] - root directory at which to start indexing
  * @param {string} [options.pattern='**\/package.json'] - glob pattern
  * @param {StringArray} [options.ignore] - glob pattern(s) to exclude matches
  * @param {Callback} clbk - callback function
  * @throws {Error} must provide valid options
  * @throws {TypeError} options argument must be an object
  * @throws {TypeError} callback argument must be a function
  *
  * @example
  * var resolve = require( 'path' ).resolve;
  *
  * var opts = {
  *     'dir': resolve( __dirname, '..' )
  * };
  * pkgIndex( opts, clbk );
  *
  * function clbk( error, idx ) {
  *     if ( error ) {
  *         console.error( error.message );
  *         return;
  *     }
  *     console.log( idx );
  * }
  */
  function pkgIndex() {
  	var options;
  	var opts;
  	var clbk;
  	var err;
  
  	opts = copy( defaults );
  	if ( arguments.length < 2 ) {
  		clbk = arguments[ 0 ];
  	} else {
  		options = arguments[ 0 ];
  		clbk = arguments[ 1 ];
  		err = validate( opts, options );
  		if ( err ) {
  			throw err;
  		}
  	}
  	if ( !isFunction( clbk ) ) {
  		throw new TypeError( 'invalid argument. Callback argument must be a function. Value: `' + clbk + '`.' );
  	}
  	if ( opts.dir ) {
  		opts.dir = path.resolve( cwd(), opts.dir );
  	} else {
  		opts.dir = cwd();
  	}
  	debug( 'Searching for READMEs...' );
  	findReadmes( opts, onReadmes );
  
  	/**
  	* Callback invoked after finding package READMEs.
  	*
  	* @private
  	* @param {(Error|null)} error - error object
  	* @param {StringArray} files - list of file paths
  	* @returns {void}
  	*/
  	function onReadmes( error, files ) {
  		if ( error ) {
  			debug( 'Encountered an error when searching for READMEs. Error: %s', error.message );
  			return done( error );
  		}
  		debug( 'Found %d READMEs.', files.length );
  		if ( files.length === 0 ) {
  			return done( null, null );
  		}
  		debug( 'Files: %s', JSON.stringify( files ) );
  		debug( 'Reading file contents...' );
  		readFileList( files, FOPTS, onFiles );
  	}
  
  	/**
  	* Callback invoked after retrieving file contents.
  	*
  	* @private
  	* @param {(Error|null)} error - error object
  	* @param {ObjectArray} files - list of files and their contents
  	* @returns {void}
  	*/
  	function onFiles( error, files ) {
  		var idx;
  		if ( error ) {
  			debug( 'Encountered an error when reading README file contents. Error: %s', error.message );
  			return done( error );
  		}
  		debug( 'Creating search index.' );
  		idx = createIndex( files );
  		debug( 'Successfully created search index.' );
  		done( null, idx );
  	}
  
  	/**
  	* Callback invoked upon completion.
  	*
  	* @private
  	* @param {(Error|null)} error - error object
  	* @param {(Object|null)} idx - serialized search index
  	* @returns {void}
  	*/
  	function done( error, idx ) {
  		if ( error ) {
  			return clbk( error );
  		}
  		clbk( null, idx );
  	}
  }
  
  
  // EXPORTS //
  
  module.exports = pkgIndex;
  