All files / lib config.js

56% Statements 14/25
50% Branches 4/8
100% Functions 0/0
56% Lines 14/25
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  1x 1x   1x 1x         1x             1x               1x 1x                   1x         87x 86x     1x 1x               1x                      
'use strict';
const fs = require('fs');
const path = require('path');
 
const appFolder = 'todocmd';
const configFile = 'config.json';
 
// Set config options based on platform
let folder;
let symbols;
Iif (process.platform === 'win32') {
  folder = path.join(process.env.APPDATA, appFolder);
  symbols = {
    ok: '\u221A',
    nok: '\u00D7'
  };
}
else Iif (process.platform === 'darwin') {
  folder = path.join(process.env.HOME, 'Library', 'Preferences', appFolder);
  symbols = {
    ok: '✓',
    nok: '✖'
  };
}
else {
  folder = path.join(process.env.HOME, '.config', appFolder);
  symbols = {
    ok: '✓',
    nok: '✖'
  };
}
 
 
/*
 * MODULE EXPORTS
 */
module.exports = {
  _data: null,
  folder,
  symbols,
  get data() {
    if (this._data) {
      return this._data;
    }
 
    try {
      this._data = JSON.parse(fs.readFileSync(path.join(folder, configFile)));
    }
    catch (err) {
      fs.mkdirSync(folder);
      this._data = JSON.parse(fs.readFileSync(path.join(__dirname, 'default.json')));
      this.save();
    }
 
    return this._data;
  },
  save() {
    if (!this._data) {
      return false;
    }
 
    fs.writeFileSync(path.join(folder, configFile), JSON.stringify(this._data));
    return true;
  }
};