<commit_before>'use strict';

const process = require('process');
const JsonDB = require('node-json-db');

let instance = null;
let app = null;

/* To make the util runnable in both main and renderer process */
if (process.type === 'renderer') {
	app = require('electron').remote.app;
} else {
	app = require('electron').app;
}

class ConfigUtil {
	constructor() {
		if (instance) {
			return instance;
		} else {
			instance = this;
		}

		this.reloadDB();
		return instance;
	}

	getConfigItem(key, defaultValue = null) {
		this.reloadDB();
		const value = this.db.getData('/')[key];
		if (value === undefined) {
			this.setConfigItem(key, defaultValue);
			return defaultValue;
		} else {
			return value;
		}
	}

	setConfigItem(key, value) {
		this.db.push(`/${key}`, value, true);
		this.reloadDB();
	}

	removeConfigItem(key) {
		this.db.delete(`/${key}`);
		this.reloadDB();
	}

	reloadDB() {
		this.db = new JsonDB(app.getPath('userData') + '/settings.json', true, true);
	}
}

module.exports = new ConfigUtil();<commit_msg>settings util: delete settings.json file in case it is corrupted.<commit_after>'use strict';

const fs = require('fs');
const path = require('path');
const process = require('process');
const JsonDB = require('node-json-db');
const Console = require('../console');

const console = new Console({
	file: 'config-util.log'
});

let instance = null;
let dialog = null;
let app = null;

/* To make the util runnable in both main and renderer process */
if (process.type === 'renderer') {
	const remote = require('electron').remote;
	dialog = remote.dialog;
	app = remote.app;
} else {
	const electron = require('electron');
	dialog = electron.dialog;
	app = electron.app;
}

class ConfigUtil {
	constructor() {
		if (instance) {
			return instance;
		} else {
			instance = this;
		}

		this.reloadDB();
		return instance;
	}

	getConfigItem(key, defaultValue = null) {
		this.reloadDB();
		const value = this.db.getData('/')[key];
		if (value === undefined) {
			this.setConfigItem(key, defaultValue);
			return defaultValue;
		} else {
			return value;
		}
	}

	setConfigItem(key, value) {
		this.db.push(`/${key}`, value, true);
		this.reloadDB();
	}

	removeConfigItem(key) {
		this.db.delete(`/${key}`);
		this.reloadDB();
	}

	reloadDB() {
		const settingsJsonPath = path.join(app.getPath('userData'), '/settings.json');
		try {
			const file = fs.readFileSync(settingsJsonPath, 'utf8');
			JSON.parse(file);
		} catch (err) {
			if (fs.existsSync(settingsJsonPath)) {
				fs.unlinkSync(settingsJsonPath);
				dialog.showErrorBox(
					'Error saving settings',
					'We encountered error while saving current settings.'
				);
				console.error('Error while JSON parsing settings.json: ');
				console.error(err);
			}
		}
		this.db = new JsonDB(settingsJsonPath, true, true);
	}
}

module.exports = new ConfigUtil();
{'fraction_non_alphanumeric': 0.1616976499473869, 'fraction_numerical': 0.00035075412136092597, 'mean_word_length': 3.809443507588533, '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': '7302836', 'n_tokens_mistral': 1038, 'n_tokens_neox': 946, 'n_words': 231}