<commit_before>function generateId() {
    var id = new Date().getTime();
    return id;
}

function addProject(projName, projVersion, iconPath, projDir) {
    var id = generateId();
    var projectObj = {};
    projectObj.id = id;
    projectObj.projDir = projDir;
    projectObj.projName = projName;

    if (localStorage["projects"]) {
        // retrieve exsiting projects to appending a new project
        var projects = JSON.parse(localStorage["projects"]);        
        projects.push(projectObj);      
        localStorage["projects"] = JSON.stringify(projects);   
        console.log(JSON.stringify(projects));    
    } else {
        var myProjects = new Array();
        myProjects.push(projectObj);        
        localStorage["projects"] = JSON.stringify(myProjects);
        console.log(JSON.stringify(myProjects));
    }

    // render newly added project to GUI & set it as the active widget
    addProjectWidget(id, projName, projVersion, iconPath, projDir);
    setActiveWidget(id, projDir);
}

function removeMissingProjects(i) {
    // removes a project from localStorage if the project is not found on the file system
    var projects = JSON.parse(localStorage["projects"]);
    var projDir = projects[i].projDir;
    var filename = projDir + "/www/config.xml";
    
    fs.readFile(filename, 'utf8', function(err, data) {
        if (err) {
            projects.splice(i, 1);
            localStorage["projects"] = JSON.stringify(projects);
        }
    });       
}

function getProjects() {
    if (localStorage["projects"]) {
        var projects = JSON.parse(localStorage["projects"]);
        var index = projects.length;
                
        for (var i=0;i<index;i++) {
            removeMissingProjects(i);            
        }

        var sortedProjects = projects.sort(sortByProperty("projName"));
        localStorage["projects"] = JSON.stringify(sortedProjects);
                
        setTimeout(renderProjects, 1000);          

    }  
}

function renderProjects() {
    var projects = JSON.parse(localStorage["projects"]);
    var index = projects.length;
    
    for (var i=0;i<index;i++) {
        var id = projects[i].id;
        var projDir = projects[i].projDir;
        getProjectConfig(id, projDir, i); 
    }
    
    console.log(JSON.stringify(projects));    
}
<commit_msg>[#176] found a better way to check if projDir is missing from file system, need to work on removing missing projects from localStorage<commit_after>function generateId() {
    var id = new Date().getTime();
    return id;
}

function addProject(projName, projVersion, iconPath, projDir) {
    var id = generateId();
    var projectObj = {};
    projectObj.id = id;
    projectObj.projDir = projDir;
    projectObj.projName = projName;

    if (localStorage["projects"]) {
        // retrieve exsiting projects to appending a new project
        var projects = JSON.parse(localStorage["projects"]);        
        projects.push(projectObj);      
        localStorage["projects"] = JSON.stringify(projects);   
        console.log(JSON.stringify(projects));    
    } else {
        var myProjects = new Array();
        myProjects.push(projectObj);        
        localStorage["projects"] = JSON.stringify(myProjects);
        console.log(JSON.stringify(myProjects));
    }

    // render newly added project to GUI & set it as the active widget
    addProjectWidget(id, projName, projVersion, iconPath, projDir);
    setActiveWidget(id, projDir);
}

function getProjects() {
    if (localStorage["projects"]) {

        var projects = JSON.parse(localStorage["projects"]);
        var index = projects.length;
        var missing = [];
        
        console.log(JSON.stringify(projects));
        
        global.jQuery.each(projects, function(idx, project) {
            var projDir = project.projDir;
            var id = project.id;
                       
            fs.exists(projDir, function(exists) {
                if (exists) {
                    console.log(projDir);
                    getProjectConfig(id, projDir, idx);
                } else {
                    // project folder not found...must remove from 
                    missing.push(id);
                }               
            });            
        });
    }  
}

function renderProjects() {
    var projects = JSON.parse(localStorage["projects"]);
    var index = projects.length;
    
    for (var i=0;i<index;i++) {
        var id = projects[i].id;
        var projDir = projects[i].projDir;
        getProjectConfig(id, projDir, i); 
    }
    
    console.log(JSON.stringify(projects));    
}

{'fraction_non_alphanumeric': 0.116414686825054, 'fraction_numerical': 0.002591792656587473, 'mean_word_length': 2.242997198879552, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 1}, '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': '1966374', 'n_tokens_mistral': 1328, 'n_tokens_neox': 1285, 'n_words': 298}