<commit_before>import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import { ConfigOptions } from "./config";

export class ConfigLoader {

    private configFilePath = path.join(os.homedir(), "ueli.config.json");
    private defaultConfig: ConfigOptions;

    public constructor(defaultConfig: ConfigOptions, configFilePath?: string) {
        if (configFilePath !== undefined) {
            this.configFilePath = configFilePath;
        }
        this.defaultConfig = defaultConfig;
    }

    public loadConfigFromConfigFile(): ConfigOptions {
        try {
            const fileContent = fs.readFileSync(this.configFilePath, "utf-8");
            const parsed = JSON.parse(fileContent) as ConfigOptions;
            // Apply defaults if some settings are not set
            const mergedConfig = Object.assign(this.defaultConfig, parsed);
            return mergedConfig;
        } catch (err) {
            this.writeDefaultConfigToConfigFile();
            return this.defaultConfig;
        }
    }

    private writeDefaultConfigToConfigFile(): void {
        const stringifiedConfig = JSON.stringify(this.defaultConfig);
        fs.writeFileSync(this.configFilePath, stringifiedConfig, "utf-8");
    }
}<commit_msg>Stringify json pretty when writing config file<commit_after>import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import { ConfigOptions } from "./config";

export class ConfigLoader {

    private configFilePath = path.join(os.homedir(), "ueli.config.json");
    private defaultConfig: ConfigOptions;

    public constructor(defaultConfig: ConfigOptions, configFilePath?: string) {
        if (configFilePath !== undefined) {
            this.configFilePath = configFilePath;
        }
        this.defaultConfig = defaultConfig;
    }

    public loadConfigFromConfigFile(): ConfigOptions {
        try {
            const fileContent = fs.readFileSync(this.configFilePath, "utf-8");
            const parsed = JSON.parse(fileContent) as ConfigOptions;
            // Apply defaults if some settings are not set
            const mergedConfig = Object.assign(this.defaultConfig, parsed);
            return mergedConfig;
        } catch (err) {
            this.writeDefaultConfigToConfigFile();
            return this.defaultConfig;
        }
    }

    private writeDefaultConfigToConfigFile(): void {
        const stringifiedConfig = JSON.stringify(this.defaultConfig, null, 2);
        fs.writeFileSync(this.configFilePath, stringifiedConfig, "utf-8");
    }
}
{'fraction_non_alphanumeric': 0.09725490196078432, 'fraction_numerical': 0.00196078431372549, 'mean_word_length': 2.8245877061469264, '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': '203477', 'n_tokens_mistral': 647, 'n_tokens_neox': 636, 'n_words': 172}