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

var tinyconf = {

    metas: {
        path: 'config.json'
    },

    conf: {},

    loadOrCreate: function(pathToFile, defaultConfig, callback) {

        var self = this;

        try {
            if(!fs.existsSync(pathToFile)) {
                // Load the file
                self.metas.path = 'pathToFile';
                self.conf = defaultConfig;
                self.saveSync();
                callback();
                return self;

            } else {

                fs.readFile(pathToFile, function(err, data) {
                    if(err) throw err;

                    // Check if json is valid
                    try {
                        JSON.parse(data);
                    }
                    catch(err) {
                        throw 'Error parsing JSON: ' + err;
                    }

                    self.metas.path = pathToFile;
                    self.conf = JSON.parse(data);
                    callback();
                    return self;
                })
            }
        }
        catch(err) {
            console.error(err);
        }
    },

    loadOrCreateSync: function(pathToFile, defaultConfig) {

        var self = this;

        try {
            if(!fs.existsSync(pathToFile)) {
                // Load the file
                self.metas.path = pathToFile;
                self.conf = defaultConfig;
                self.saveSync();
                return self;

            } else {

                var data = fs.readFileSync(pathToFile);

                // Check if json is valid
                try {
                    JSON.parse(data);
                }
                catch(err) {
                    throw 'Error parsing JSON: ' + err;
                }

                self.metas.path = pathToFile;
                self.conf = JSON.parse(data);
                return self;
            }
        }
        catch(err) {
            console.error(err);
        }
    },

    get: function(key) {

        return this.conf[key];
    },

    getAll: function() {

        return this.conf;
    },

    set: function(key, value) {

        this.conf[key] = value;
    },

    save: function(minify, callback) {

        minify = minify || false;
        var self = this;
        var output = minify ? JSON.stringify(self.conf) : JSON.stringify(self.conf, null, ' ');

        try {
            fs.writeFile(self.metas.path, output, function(err) {
                if(err) throw err;
                else callback();
            });
        }
        catch(err) {
            console.error(err);
        }
    },

    saveSync: function(minify) {

        minify = minify || false;
        var self = this;
        var output = minify ? JSON.stringify(self.conf) : JSON.stringify(self.conf, null, ' ');

        try {
            fs.writeFileSync(self.metas.path, output);
        }
        catch(err) {
            console.error(err);
        }
    }
}

module.exports = tinyconf;<commit_msg>Make it secure<commit_after>var fs = require('fs');

var teenyconf = function(configPath) {

    var _metas = {
        configPath: configPath || 'config.json'
    };

    var _conf = {};

    this.loadOrCreate = function(defaultConfig, callback) {

        var self = this;
        defaultConfig = defaultConfig || {};

        try {
            if(!fs.existsSync(_metas.configPath)) {
                // Load the file
                _conf = defaultConfig;
                self.saveSync();
                callback();

            } else {

                fs.readFile(_metas.configPath, function(err, data) {

                    if(err) throw err;

                    // Check if json is valid
                    try {
                        _conf = JSON.parse(data);
                        callback();
                    }
                    catch(err) {
                        throw 'Error reading ' + _metas.configPath + ': ' + err;
                    }
                });
            }
        }
        catch(err) {
            console.error(err);
        }
    },

    this.loadOrCreateSync = function(defaultConfig) {

        var self = this;
        defaultConfig = defaultConfig || {};

        try {
            if(!fs.existsSync(_metas.configPath)) {
                // Load the file
                _conf = defaultConfig;
                self.saveSync();

            } else {

                var data = fs.readFileSync(_metas.configPath);

                // Check if json is valid
                try {
                    _conf = JSON.parse(data);
                }
                catch(err) {
                    throw 'Error reading ' + _metas.configPath + ': ' + err;
                }
            }
        }
        catch(err) {
            console.error(err);
        }
    },

    this.get = function(key) {

        return _conf[key];
    },

    this.getAll = function() {

        return _conf;
    },

    this.set = function(key, value) {

        _conf[key] = value;
    },

    this.save = function(minify, callback) {

        minify = minify || false;
        var self = this;
        var output = minify ? JSON.stringify(_conf) : JSON.stringify(_conf, null, ' ');

        try {
            fs.writeFile(_metas.configPath, output, function(err) {
                if(err) throw err;
                else callback();
            });
        }
        catch(err) {
            console.error(err);
        }
    },

    this.saveSync = function(minify) {

        minify = minify || false;
        var self = this;
        var output = minify ? JSON.stringify(_conf) : JSON.stringify(_conf, null, ' ');

        try {
            fs.writeFileSync(_metas.configPath, output);
        }
        catch(err) {
            console.error(err);
        }
    }
}

module.exports = teenyconf;
{'fraction_non_alphanumeric': 0.11044520547945205, 'fraction_numerical': 0.0, 'mean_word_length': 1.1147719044170892, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1912225', 'n_tokens_mistral': 1647, 'n_tokens_neox': 1520, 'n_words': 311}