<commit_before>// modules
var fs = require('fs');
var remote = require('electron').remote;
var dialog = remote.dialog;

// variables
var editors = [];
var editorNames = ['Javascript', 'HTML', 'Vertex', 'Fragment', 'VertexPost', 'FragmentPost'];
var editorModes = ['javascript', 'html', 'glsl', 'glsl', 'glsl', 'glsl'];
var editorTheme = ['monokai', 'monokai', 'vibrant_ink', 'vibrant_ink', 'vibrant_ink', 'vibrant_ink'];
var editorThemeLight = ['tomorrow', 'tomorrow', 'tomorrow', 'tomorrow', 'tomorrow', 'tomorrow'];
var editorThemeDarken = true;
var activeSource = -1;
var sourceArray = [];
var popupTime = 0;
var listAddEvent = [];

// const var
var TARGET_FILE_NAME = [
    'javascript.js',
    'html.html',
    'vs.vert',
    'fs.frag',
    'vsp.vert',
    'fsp.frag',
    'info.json'
];

// initial
window.onload = function(){
    var e, s, t;
    editorInitialize();
    win = window;
    win.addEventListener('keydown', keydown, false);
    for(var i = 0, l = editorNames.length; i < l; i++){
        bid('tab' + editorNames[i]).addEventListener('click', tabSelecter, false);
    }
    win.addEventListener('click', function(eve){showPopup(false);}, false);
    e = bid('iconMenu');
    e.addEventListener('click', function(eve){showPopup(true);}, false);
    e = bid('iconList');
    e.addEventListener('click', loadDirectory, false);
    e = bid('hiddenDefault');
    e.addEventListener('click', loadDirectory, false);
    e = bid('iconPlay');
    e.addEventListener('click', init, false);
    e = bid('autoSave');
    e.addEventListener('click', function(eve){
        var e = bid('inputAutoSave');
        e.checked = !e.checked;
    }, false);

    run = false;

    // drag and drop
    document.ondragover = document.ondrop = function(eve){
        eve.preventDefault();
        return false;
    };
    document.addEventListener('dragleave', function(eve){return false;}, false);
    document.addEventListener('dragend', function(eve){return false;}, false);
    document.addEventListener('dragover', function(eve){return false;}, false);
    document.addEventListener('drop', function(eve){
        eve.preventDefault();
        var target = eve.dataTransfer.items;
        if(target.length !== 1){
            alert('please drop single template directory');
            return false;
        }
        var entry = null;
        if(target[0].getAsEntry){
            entry = target[0].getAsEntry();
        }else{
            entry = target[0].webkitGetAsEntry();
        }
        if(!entry || !entry.isDirectory){
            alert('invalid drop file');
            return false;
        }
        var path = eve.dataTransfer.files[0].path;
        loadDirectory(path);
        return false;
    }, false);
};

function addTempleteList(list){
    var i, j, e, f, a, s, t;
    e = bid('hidden');
    a = e.childNodes;
    if(listAddEvent.length > 0){
        for(i = 0, j = listAddEvent.length; i < j; ++i){
            listAddEvent[i].target.removeEventListener('click', listAddEvent[i].function, false);
        }
    }
    listAddEvent = [];
    for(i = 0, j = a.length; i < j; ++i){
        if(a[i].nodeType === 1 && a[i].id !== 'hiddenDefault'){
            e.removeChild(a[i]);
            a[i] = null;
            --i;
            --j;
        }
    }
    f = bid('hiddenDefault')
    if(!list || !list.hasOwnProperty('length') || list.length === 0){
        f.style.display = 'block';
        return;
    }else{
        f.style.display = 'none';
    }
    for(i = 0, j = list.length; i < j; ++i){
        s = zeroPadding(i + 1, 3);
        t = '';
        if(list[i].hasOwnProperty('info.json') && list[i]['info.json'].hasOwnProperty('title')){
            s += ': ' + list[i]['info.json'].title;
            t = list[i]['info.json'].description;
        }
        f = document.createElement('div');
        f.id = i + '_' + templateHash(loadTargetDirectory);
        f.className = 'list';
        f.textContent = s;
        f.title = t;
        listAddEvent.push({
            target: f,
            function: function(eve){
                var e = eve.currentTarget;
                var i = parseInt(e.id.match(/\d+/), 10);
                editorAddSource(i);
            }
        });
        f.addEventListener('click', listAddEvent[i].function, false);
        e.appendChild(f);
    }
}

function loadDirectory(path){
    var i, j, list, projectRoot, separator;
    var currentWindow = remote.getCurrentWindow();
    if(path && Object.prototype.toString.call(path) === '[object String]'){
        loaddir(path);
    }else{
        remote.dialog.showOpenDialog(currentWindow, {
            defaultPath: remote.app.getAppPath('exe'),
            properties: ['openDirectory']
        }, loaddir);
    }

    function loaddir(dir){
        var e;
        var path = '';
        if(Object.prototype.toString.call(dir) === '[object String]'){
            path = dir;
        }else{
            if(dir && dir.length > 0){
                path = dir[0];
            }else{
                console.warn('invalid directory');
                return;
            }
        }
        // directory check
        fs.readdir(path, function(err, files){
            if(err || !files || !files.hasOwnProperty('length') || files.length === 0){
                console.warn('error: readdire');
                return
            }
            separator = process.platform === 'darwin' ? '/' : '\\';
            projectRoot = path + separator;
            list = [];
            files.filter(function(file){
                return fs.existsSync(projectRoot + file) && fs.statSync(projectRoot + file).isDirectory();
            }).forEach(function(file){
                list.push({
                    index: parseInt(file, 10),
                    indexString: file,
                    targetDirectory: projectRoot + file
                });
            });

            // map for template/
            loadTargetDirectory = path;<commit_msg>fix check directory<commit_after>// modules
var fs = require('fs');
var remote = require('electron').remote;
var dialog = remote.dialog;

// variables
var editors = [];
var editorNames = ['Javascript', 'HTML', 'Vertex', 'Fragment', 'VertexPost', 'FragmentPost'];
var editorModes = ['javascript', 'html', 'glsl', 'glsl', 'glsl', 'glsl'];
var editorTheme = ['monokai', 'monokai', 'vibrant_ink', 'vibrant_ink', 'vibrant_ink', 'vibrant_ink'];
var editorThemeLight = ['tomorrow', 'tomorrow', 'tomorrow', 'tomorrow', 'tomorrow', 'tomorrow'];
var editorThemeDarken = true;
var activeSource = -1;
var sourceArray = [];
var popupTime = 0;
var listAddEvent = [];

// const var
var TARGET_FILE_NAME = [
    'javascript.js',
    'html.html',
    'vs.vert',
    'fs.frag',
    'vsp.vert',
    'fsp.frag',
    'info.json'
];

// initial
window.onload = function(){
    var e, s, t;
    editorInitialize();
    win = window;
    win.addEventListener('keydown', keydown, false);
    for(var i = 0, l = editorNames.length; i < l; i++){
        bid('tab' + editorNames[i]).addEventListener('click', tabSelecter, false);
    }
    win.addEventListener('click', function(eve){showPopup(false);}, false);
    e = bid('iconMenu');
    e.addEventListener('click', function(eve){showPopup(true);}, false);
    e = bid('iconList');
    e.addEventListener('click', loadDirectory, false);
    e = bid('hiddenDefault');
    e.addEventListener('click', loadDirectory, false);
    e = bid('iconPlay');
    e.addEventListener('click', init, false);
    e = bid('autoSave');
    e.addEventListener('click', function(eve){
        var e = bid('inputAutoSave');
        e.checked = !e.checked;
    }, false);

    run = false;

    // drag and drop
    document.ondragover = document.ondrop = function(eve){
        eve.preventDefault();
        return false;
    };
    document.addEventListener('dragleave', function(eve){return false;}, false);
    document.addEventListener('dragend', function(eve){return false;}, false);
    document.addEventListener('dragover', function(eve){return false;}, false);
    document.addEventListener('drop', function(eve){
        eve.preventDefault();
        var target = eve.dataTransfer.items;
        if(target.length !== 1){
            alert('please drop single template directory');
            return false;
        }
        var entry = null;
        if(target[0].getAsEntry){
            entry = target[0].getAsEntry();
        }else{
            entry = target[0].webkitGetAsEntry();
        }
        if(!entry || !entry.isDirectory){
            alert('invalid drop file');
            return false;
        }
        var path = eve.dataTransfer.files[0].path;
        loadDirectory(path);
        return false;
    }, false);
};

function addTempleteList(list){
    var i, j, e, f, a, s, t;
    e = bid('hidden');
    a = e.childNodes;
    if(listAddEvent.length > 0){
        for(i = 0, j = listAddEvent.length; i < j; ++i){
            listAddEvent[i].target.removeEventListener('click', listAddEvent[i].function, false);
        }
    }
    listAddEvent = [];
    for(i = 0, j = a.length; i < j; ++i){
        if(a[i].nodeType === 1 && a[i].id !== 'hiddenDefault'){
            e.removeChild(a[i]);
            a[i] = null;
            --i;
            --j;
        }
    }
    f = bid('hiddenDefault')
    if(!list || !list.hasOwnProperty('length') || list.length === 0){
        f.style.display = 'block';
        return;
    }else{
        f.style.display = 'none';
    }
    for(i = 0, j = list.length; i < j; ++i){
        s = zeroPadding(i + 1, 3);
        t = '';
        if(list[i].hasOwnProperty('info.json') && list[i]['info.json'].hasOwnProperty('title')){
            s += ': ' + list[i]['info.json'].title;
            t = list[i]['info.json'].description;
        }
        f = document.createElement('div');
        f.id = i + '_' + templateHash(loadTargetDirectory);
        f.className = 'list';
        f.textContent = s;
        f.title = t;
        listAddEvent.push({
            target: f,
            function: function(eve){
                var e = eve.currentTarget;
                var i = parseInt(e.id.match(/\d+/), 10);
                editorAddSource(i);
            }
        });
        f.addEventListener('click', listAddEvent[i].function, false);
        e.appendChild(f);
    }
}

function loadDirectory(path){
    var i, j, list, projectRoot, separator;
    var currentWindow = remote.getCurrentWindow();
    if(path && Object.prototype.toString.call(path) === '[object String]'){
        loaddir(path);
    }else{
        remote.dialog.showOpenDialog(currentWindow, {
            defaultPath: remote.app.getAppPath('exe'),
            properties: ['openDirectory']
        }, loaddir);
    }

    function loaddir(dir){
        var e;
        var path = '';
        if(Object.prototype.toString.call(dir) === '[object String]'){
            path = dir;
        }else{
            if(dir && dir.length > 0){
                path = dir[0];
            }else{
                console.warn('invalid directory');
                return;
            }
        }
        // directory check
        fs.readdir(path, function(err, files){
            if(err || !files || !files.hasOwnProperty('length') || files.length === 0){
                console.warn('error: readdire');
                return
            }
            separator = process.platform === 'darwin' ? '/' : '\\';
            projectRoot = path + separator;
            list = [];
            files.filter(function(file){
                return fs.existsSync(projectRoot + file) && fs.statSync(projectRoot + file).isDirectory();
            }).forEach(function(file){
                if(file && !file.match(/\D/) && !isNaN(parseInt(file, 10))){
                    list.push({
                        index: parseInt(file, 10),
                        indexString: file,
                        targetDirectory: projectRoot + file
                    });
                }
            });

            // map for template/
            loadTargetDirectory = path;
{'fraction_non_alphanumeric': 0.16310760282509348, 'fraction_numerical': 0.003988367262152057, 'mean_word_length': 2.152435830277632, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, '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': '2585252', 'n_tokens_mistral': 3596, 'n_tokens_neox': 3499, 'n_words': 755}