<commit_before>var fs = require('fs');

var moment = require('moment');
var EventProxy = require('eventproxy').EventProxy;
var SourceInfo = require('./SourceInfo.js').SourceInfo;


exports.clocWithPath = function(rootpath) {
	console.log("ncloc start time is :" + moment().unix());
	var res = getAllFilesInfoSync(rootpath);

	var done = function(sourceInfos) {
			var totalBlankLines = 0;
			var totalInlineComments = 0;
			var total = 0;
			if (sourceInfos.length > 0) {
				for (var i = 0; i < sourceInfos.length; i++) {
					total += sourceInfos[i].total;
					totalBlankLines += sourceInfos[i].blankLines;
					totalInlineComments += sourceInfos[i].inlineComments;
				}
			}
			console.log("contain files:" + sourceInfos.length);
			console.log("total lines  :" + total);
			console.log("blank lines:" + totalBlankLines);
			console.log("inlineComments:" + totalInlineComments);

			console.log("ncloc end time is :" + moment().unix());
		}
	var proxy = new EventProxy();
	proxy.after('count_source_lines', res.length, done);

	getAllFiles(rootpath, proxy);


}


function getAllFilesInfoSync(root) {
	var res = [];
	var files = fs.readdirSync(root);
	files.forEach(function(file) {
		var pathname = root + '/' + file;
		var stat = fs.lstatSync(pathname);

		if (!stat.isDirectory()) {
			if (isSourceFile(pathname)) {
				res.push(pathname.replace(root, '.'));
			}
		} else {
			res = res.concat(getAllFilesInfoSync(pathname));
		}
	});
	return res
}


function getAllFiles(root, proxy) {

	fs.readdir(root, function(err, files) {
		files.forEach(function(file) {
			var pathname = root + '/' + file;
			var stat = fs.lstatSync(pathname);

			if (!stat.isDirectory()) {
				if (isSourceFile(pathname)) {
					clocWithFullPath(pathname, proxy);
				}
			} else {
				getAllFiles(pathname, proxy);
			}
		});
	});

}


function clocWithFullPath(filename, proxy) {

	var input = fs.createReadStream(filename);
	var remaining = '';
	var count = 0;
	var blankLines = 0;
	var inlineComments = 0;


	input.on('data', function(data) {
		remaining += data;
		var index = remaining.indexOf('\n');
		var last = 0;
		while (index > -1) {
			var line = remaining.substring(last, index);
			var trimedLine = line.trim();
			if (trimedLine == '') {
				blankLines++;
			}
			if (trimedLine.indexOf('//') > -1) {
				inlineComments++;
			}
			last = index + 1;
			count++;
			index = remaining.indexOf('\n', last);
		}
<commit_msg>去除异步二次遍历文件的过程。<commit_after>var fs = require('fs');

var moment = require('moment');
var EventProxy = require('eventproxy').EventProxy;
var SourceInfo = require('./SourceInfo.js').SourceInfo;



exports.clocWithPath = function(rootpath) {
	console.log("ncloc start time is :" + moment().unix());


	var res = getAllFilesInfoSync(rootpath);
	console.log("ncloc start time is :" + moment().unix());

	var done = function(sourceInfos) {
			var totalBlankLines = 0;
			var totalInlineComments = 0;
			var total = 0;
			if (sourceInfos.length > 0) {
				for (var i = 0; i < sourceInfos.length; i++) {
					total += sourceInfos[i].total;
					totalBlankLines += sourceInfos[i].blankLines;
					totalInlineComments += sourceInfos[i].inlineComments;
				}
			}
			console.log("contain files:" + sourceInfos.length);
			console.log("total lines  :" + total);
			console.log("blank lines:" + totalBlankLines);
			console.log("inlineComments:" + totalInlineComments);

			console.log("ncloc end time is :" + moment().unix());
		}
	var proxy = new EventProxy();

	proxy.after('count_source_lines', res.length, done);

	res.forEach(function(filepath){
		clocWithFullPath(filepath, proxy);
	});
	


}


function getAllFilesInfoSync(root) {
	var res = [];
	var files = fs.readdirSync(root);
	files.forEach(function(file) {
		var pathname = root + '/' + file;
		var stat = fs.lstatSync(pathname);

		if (!stat.isDirectory()) {
			if (isSourceFile(pathname)) {
				res.push(pathname);//pathname.replace(root, '.')
			}
		} else {
			res = res.concat(getAllFilesInfoSync(pathname));
		}
	});
	return res
}

function clocWithFullPath(filename, proxy) {

	var input = fs.createReadStream(filename);
	var remaining = '';
	var count = 0;
	var blankLines = 0;
	var inlineComments = 0;


	input.on('data', function(data) {
		remaining += data;
		var index = remaining.indexOf('\n');
		var last = 0;
		while (index > -1) {
			var line = remaining.substring(last, index);
			var trimedLine = line.trim();
			if (trimedLine == '') {
				blankLines++;
			}
			if (trimedLine.indexOf('//') > -1) {
				inlineComments++;
			}
			last = index + 1;
			count++;
			index = remaining.indexOf('\n', last);
		}

{'fraction_non_alphanumeric': 0.16162714814009135, 'fraction_numerical': 0.005220796171416141, 'mean_word_length': 3.7110655737704916, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '3118855', 'n_tokens_mistral': 1815, 'n_tokens_neox': 1573, 'n_words': 331}