<commit_before>var glob = require("glob");
var readline = require('readline');
var fs = require('fs');
var os = require('os');
var path = require('path');
var jsmin = require('jsmin').jsmin;
var watch = require('watch');
var ini = require('ini');
var minify = require('html-minifier').minify;

var importRegex = /^import( +)?([\w\$.]+);/i;
var namespaceRegex = /^namespace( +)?([\w.]+);/i;
var firstVariableRegex = /^([\w]+)(.prototype)?(.[\w]+)( {0,}?=)/;
var templateRegex = /template( +)?\"([\w\$.\/]+)\"( +)?as( +)?([\w\$.\/]+);/i;

var filesToImport = [];
var namespacesToGenerate = [];
var namespacesGenerated = [];
var templatesReferences = [];

function inArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(haystack[i] == needle) return true;
    }
    return false;
}

function addFileImport(file) {
	if(!inArray(file, filesToImport)) {
		filesToImport.push(file);
	}
}

function namespacesAdd(namespace) {
	if(!inArray(namespace, namespacesToGenerate)) {
		namespacesToGenerate.push(namespace);
	}
}

function finishedReading(srcfolder, buildpath, project) {
  var buildContent = '';

  var cwd = process.cwd();
  var header = fs.readFileSync(cwd + '/' + project.header, "utf-8");
  header = header.replace('{{version}}',project.version);
  header = header.replace('{{revision}}',project.revision);
  buildContent += header + os.EOL;

  for (var i = filesToImport.length - 1; i >= 0; i--) {
  	var fileToLoad = filesToImport[i].replace(/\./g,'/').replace(/\$/g,'..') + '.js';
  	var fileToLoadX = (srcfolder+'/'+fileToLoad).replace('//','/');
  	console.log(fileToLoadX)
		if (fs.existsSync(fileToLoadX)) { 
		  var contents = fs.readFileSync(fileToLoadX).toString();
		  buildContent += contents;
		} else {
			buildContent += 'console.log("Could not import file: '+fileToLoad+'");' + os.EOL;
		}
  };

  for (var i = namespacesToGenerate.length - 1; i >= 0; i--) {
  	var spacesSplit = namespacesToGenerate[i].namespace.split('.'),
  			toCreate = [];
  	for (var j = 0; j < spacesSplit.length; j++) {
  		toCreate.push(spacesSplit[j]);
  		var joined = toCreate.join('.');
  		if(!inArray(joined, namespacesGenerated)) {
  			buildContent += 'if(typeof '+joined+'=="undefined") ' + joined + ' = {};' + os.EOL;
  			namespacesGenerated.push(joined);
  		}
  	};
  	buildContent += namespacesToGenerate[i].content;
  };

  for (var i = templatesReferences.length - 1; i >= 0; i--) {
    var ref = templatesReferences[i];
    buildContent += ref.variable + ' = document.getElementById("'+ project.templatesFolder + '/' + ref.id +'").innerHTML;' + os.EOL;
  };

  var minified = jsmin(buildContent);
  if(project.debug) {
    minified = buildContent;
  }

  fs.truncate(buildpath, 0, function(){
    fs.writeFile(buildpath,minified, function(err) {
        if(err) {
            return console.log(err);
        }

        filesToImport = [];
        namespacesToGenerate = [];
        namespacesGenerated = [];
        templatesReferences = [];
    }); 
  });

}

function readFileByLine(pathToFile, callback, endoffilecallback) {
  fs.readFile(pathToFile, function (err, data) {
    var bufferString = data.toString(); 
    var bufferStringSplit = bufferString.split('\n'); 
    for (var i = 0; i < bufferStringSplit.length; i++) {<commit_msg>fixes header not show on debug false<commit_after>var glob = require("glob");
var readline = require('readline');
var fs = require('fs');
var os = require('os');
var path = require('path');
var jsmin = require('jsmin').jsmin;
var watch = require('watch');
var ini = require('ini');
var minify = require('html-minifier').minify;

var importRegex = /^import( +)?([\w\$.]+);/i;
var namespaceRegex = /^namespace( +)?([\w.]+);/i;
var firstVariableRegex = /^([\w]+)(.prototype)?(.[\w]+)( {0,}?=)/;
var templateRegex = /template( +)?\"([\w\$.\/]+)\"( +)?as( +)?([\w\$.\/]+);/i;

var filesToImport = [];
var namespacesToGenerate = [];
var namespacesGenerated = [];
var templatesReferences = [];

function inArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(haystack[i] == needle) return true;
    }
    return false;
}

function addFileImport(file) {
	if(!inArray(file, filesToImport)) {
		filesToImport.push(file);
	}
}

function namespacesAdd(namespace) {
	if(!inArray(namespace, namespacesToGenerate)) {
		namespacesToGenerate.push(namespace);
	}
}

function finishedReading(srcfolder, buildpath, project) {
  var buildContent = '';

  var cwd = process.cwd();
  var header = fs.readFileSync(cwd + '/' + project.header, "utf-8");
  header = header.replace('{{version}}',project.version);
  header = header.replace('{{revision}}',project.revision);

  for (var i = filesToImport.length - 1; i >= 0; i--) {
  	var fileToLoad = filesToImport[i].replace(/\./g,'/').replace(/\$/g,'..') + '.js';
  	var fileToLoadX = (srcfolder+'/'+fileToLoad).replace('//','/');
  	console.log(fileToLoadX)
		if (fs.existsSync(fileToLoadX)) { 
		  var contents = fs.readFileSync(fileToLoadX).toString();
		  buildContent += contents;
		} else {
			buildContent += 'console.log("Could not import file: '+fileToLoad+'");' + os.EOL;
		}
  };

  for (var i = namespacesToGenerate.length - 1; i >= 0; i--) {
  	var spacesSplit = namespacesToGenerate[i].namespace.split('.'),
  			toCreate = [];
  	for (var j = 0; j < spacesSplit.length; j++) {
  		toCreate.push(spacesSplit[j]);
  		var joined = toCreate.join('.');
  		if(!inArray(joined, namespacesGenerated)) {
  			buildContent += 'if(typeof '+joined+'=="undefined") ' + joined + ' = {};' + os.EOL;
  			namespacesGenerated.push(joined);
  		}
  	};
  	buildContent += namespacesToGenerate[i].content;
  };

  for (var i = templatesReferences.length - 1; i >= 0; i--) {
    var ref = templatesReferences[i];
    buildContent += ref.variable + ' = document.getElementById("'+ project.templatesFolder + '/' + ref.id +'").innerHTML;' + os.EOL;
  };

  var minified = jsmin(buildContent);
  if(project.debug) {
    minified = buildContent;
  }

  minified += header + minified + os.EOL;

  fs.truncate(buildpath, 0, function(){
    fs.writeFile(buildpath,minified, function(err) {
        if(err) {
            return console.log(err);
        }

        filesToImport = [];
        namespacesToGenerate = [];
        namespacesGenerated = [];
        templatesReferences = [];
    }); 
  });

}

function readFileByLine(pathToFile, callback, endoffilecallback) {
  fs.readFile(pathToFile, function (err, data) {
    var bufferString = data.toString(); 
    var bufferStringSplit = bufferString.split('\n'); 
    for (var i = 0; i < bufferStringSplit.length; i++) {
{'fraction_non_alphanumeric': 0.1933884297520661, 'fraction_numerical': 0.0036063110443275734, 'mean_word_length': 3.687323943661972, 'pattern_counts': {'":': 0, '<': 9, '<?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': '6837727', 'n_tokens_mistral': 2338, 'n_tokens_neox': 2252, 'n_words': 453}