Skip to content
Snippets Groups Projects
Select Git revision
  • eaf6bb41cdd668b52a835aa984ad7c02410ae86f
  • main default protected
  • jw_sonar
  • v6.0.0 protected
  • interactive-mode-preference
  • bedran_exercise-list
  • add_route_user
  • Jw_sonar_backup
  • exercise_list_filter
  • assignment_filter
  • add_route_assignments
  • move-to-esm-only
  • 6.0.0-dev
  • Pre-alpha
  • 5.0.0
  • Latest
  • 4.2.0
  • 4.1.1
  • 4.1.0
  • 4.0.1
  • 4.0.0
  • 3.5.0
  • 3.4.2
  • 3.4.1
  • 3.3.0
  • 3.2.3
  • 3.2.2
  • 3.2.0
  • 3.1.2
  • 3.1.1
  • 3.1.0
  • 3.0.1
32 results

CompletionFishCommand.ts

Blame
  • LocalConfigFile.ts 1.57 KiB
    import * as fs from 'fs';
    import JSON5   from 'json5';
    
    
    class LocalConfigFile {
        private readonly folder: string;
        private readonly filename: string;
    
        constructor(folder: string, filename: string) {
            this.folder = folder;
            this.filename = filename;
    
            this.loadConfig();
        }
    
        private get configPath(): string {
            return `${ this.folder }/${ this.filename }`;
        }
    
        private _config: { [key: string]: unknown } = {};
    
        loadConfig() {
            if ( !fs.existsSync(this.configPath) ) {
                fs.mkdirSync(this.folder, { recursive: true });
    
                fs.writeFileSync(this.configPath, JSON5.stringify({}));
            }
    
            try {
                this._config = JSON5.parse(fs.readFileSync(this.configPath).toString());
            } catch ( error ) {
                console.log(error);
            }
        }
    
        getParam(key: string): unknown {
            const value = key in this._config ? this._config[key] : null;
            if ( value === null ) {
                return null;
            } else if ( typeof value === 'object' ) {
                return { ...value };
            } else {
                return value;
            }
        }
    
        setParam(key: string, value: unknown): void {
            const previousValue = this.getParam(key);
            if ( JSON5.stringify(previousValue) === JSON5.stringify(value) ) {
                return;
            }
    
            this._config[key] = value;
    
            try {
                fs.writeFileSync(this.configPath, JSON5.stringify(this._config, null, 4));
            } catch ( error ) {
                console.log(error);
            }
        }
    }
    
    
    export default LocalConfigFile;