<commit_before>/**
 * Copyright (c) 2014, CKSource - <NAME>. All rights reserved.
 * Licensed under the terms of the MIT License (see LICENSE.md).
 */

'use strict';

var minimatch = require( 'minimatch' ),
	fs = require( 'graceful-fs' ),
	path = require( 'path' );

/**
 * Reads directory recursively
 * @param  {String}   dir               Directory to be read
 * @param  {Object}   [options]         Configuration options
 * @param  {String}   [options.ignore]  Ignore file pattern, supports globstar matching
 * @param  {Boolean}  [options.stats]   Return file objects containing file statistics
 * @param  {Function} callback          Function called when done or error occured
 */
function gerard( dir, options, callback ) {
	var results = [],
		count = 0;

	if ( typeof options == 'function' ) {
		callback = options;
		options = {};
	}

	function decreaseCounter() {
		count--;

		if ( !count ) {
			callback( null, results );
		}
	}

	fs.readdir( dir, function( err, files ) {
		if ( err ) {
			return callback( err );
		}

		if ( !( count = files.length ) ) {
			return callback( null, results );
		}

		files.forEach( function( file ) {
			var name = file;

			file = path.join( dir, file );

			if ( options.ignore && minimatch( file, options.ignore ) ) {
				return decreaseCounter();
			}

			fs.lstat( file, function( err, stats ) {
				if ( err ) {
					return callback( err );
				}

				if ( stats.isDirectory() ) {
					gerard( file, options, function( err, files ) {
						if ( err ) {
							return callback( err );
						}

						results = results.concat( files );

						decreaseCounter();
					} );
				} else {
					if ( options.stats ) {
						results.push( {
							name: name,
							dir: dir,
							path: file,
							stats: stats
						} );
					} else {
						results.push( file );
					}

					decreaseCounter();
				}
			} );
		} );
	} );<commit_msg>Resolved #2 - added stopOnErrors option.<commit_after>/**
 * Copyright (c) 2014, CKSource - <NAME>. All rights reserved.
 * Licensed under the terms of the MIT License (see LICENSE.md).
 */

'use strict';

var minimatch = require( 'minimatch' ),
	fs = require( 'graceful-fs' ),
	path = require( 'path' );

/**
 * Reads directory recursively
 * @param  {String}   dir                     Directory to be read
 * @param  {Object}   [options]               Configuration options
 * @param  {String}   [options.ignore]        Ignore file pattern, supports globstar matching
 * @param  {Boolean}  [options.stats]         Return file objects containing file statistics
 * @param  {Boolean}  [options.stopOnErrors]  Stop reading the directory on first error
 * @param  {Function} callback                Function called when done or error occured
 */
function gerard( dir, options, callback ) {
	var results = [],
		count = 0;

	if ( arguments.length < 2 ) {
		return callback( new Error( 'Invalid number arguments for Gerard specified.' ) );
	}

	if ( typeof options == 'function' ) {
		callback = options;
		options = {};
	}

	options.stopOnErrors = ( options.stopOnErrors === undefined ) ? true : options.stopOnErrors;

	function decreaseCounter() {
		count--;

		if ( !count ) {
			callback( null, results );
		}
	}

	fs.readdir( dir, function( err, files ) {
		if ( err && options.stopOnErrors ) {
			return callback( err );
		}

		if ( !files || !( count = files.length ) ) {
			return callback( null, results );
		}

		files.forEach( function( file ) {
			var name = file;

			file = path.join( dir, file );

			if ( options.ignore && minimatch( file, options.ignore ) ) {
				return decreaseCounter();
			}

			fs.lstat( file, function( err, stats ) {
				if ( err && options.stopOnErrors ) {
					return callback( err );
				}

				if ( err ) {
					return decreaseCounter();
				}

				if ( stats.isDirectory() ) {
					gerard( file, options, function( err, files ) {
						if ( err && options.stopOnErrors ) {
							return callback( err );
						}

						if ( !err ) {
							results = results.concat( files );
						}

						decreaseCounter();
					} );
				} else {
					if ( options.stats ) {
						results.push( {
							name: name,
							dir: dir,
							path: file,
							stats: stats
						} );
					} else {
						results.push( file );
					}

					decreaseCounter();
				}
			} );
		} );
	} );
{'fraction_non_alphanumeric': 0.13417190775681342, 'fraction_numerical': 0.002795248078266946, 'mean_word_length': 2.3209590100541377, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '578290', 'n_tokens_mistral': 1645, 'n_tokens_neox': 1294, 'n_words': 392}