# all files / src/models/ project-settings.js

100% Statements 49/49
100% Branches 18/18
100% Functions 17/17
100% Lines 22/22
2 statements, 5 branches Ignored

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    671×
    1×
    1×
    1×

    1×

    10×
    10×
    10×

    10×
    7×

    3×
    3×

    6×

    4×

    28×

    12×

    6×

    2×

    2×

    1×

    1×

     import path from 'path';
    import { copySync } from 'fs-extra';
    import jf from 'jsonfile';
    import { pwd } from 'shelljs';

    import { fileExists } from '../util/fs';

    /*
      Look into using Yam for finding settings so it will get the first
      .reduxrc it finds and use that for project settings just like how
      eslintrc and ember-cli works
    */

    export default class ProjectSettings {
      constructor(relativePath) {
        this.relativePath = relativePath || '../../templates/.reduxrc';
        this.loadSettings();
      }

      loadSettings() {
        if (this.settingsExist()) {
          this.settings = jf.readFileSync(this.settingsPath());
        } else {
          this.buildFromTemplate();
          this.settings = jf.readFileSync(this.settingsPath());
        }
      }

      templatePath() {
        return path.join(
          path.dirname(module.id), this.relativePath
        );
      }

      buildFromTemplate() {
        copySync(this.templatePath(), this.settingsPath());
      }

      settingsPath() {
        return path.join(pwd(), '.reduxrc');
      }

      settingsExist() {
        return fileExists(this.settingsPath());
      }

      getSetting(key) {
        return this.settings[key];
      }

      getAllSettings() {
        return this.settings;
      }

      setSetting(key, val) {
        this.settings[key] = val;
      }

      setAllSettings(json) {
        this.settings = json;
      }

      save() {
        jf.writeFileSync(this.settingsPath(), this.settings);
      }
    }