All files config.js

50% Statements 9/18
0% Branches 0/2
50% Functions 3/6
50% Lines 9/18
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          1x               1x 1x 1x 1x   1x 1x                                               1x 1x          
import debug from 'debug';
import fs from 'fs-promise';
import os from 'os';
import path from 'path';
 
const d = debug('electron-forge:runtime-config');
 
/*
 * Let's be real: sharing config across spawned processes must be easier than
 * this...
 */
class BasicConfigStore {
  constructor() {
    this._store = {};
    this._dir = path.resolve(os.tmpdir(), 'electron-forge');
    this._path = path.resolve(this._dir, '.runtime.config');
    fs.mkdirsSync(this._dir);
 
    process.on('exit', () => {
      this.reset();
    });
  }
 
  get(key) {
    this._load();
    d('fetching key', key);
    return this._store[key];
  }
 
  set(key, value) {
    this._load();
    this._store[key] = value;
    d('setting key:', key, 'to value:', value);
    fs.writeFileSync(this._path, JSON.stringify(this._store));
  }
 
  _load() {
    if (fs.existsSync(this._path)) {
      this._store = JSON.parse(fs.readFileSync(this._path, 'utf8'));
    }
  }
 
  reset() {
    this._store = {};
    fs.writeFileSync(this._path, JSON.stringify(this._store));
  }
}
 
export default new BasicConfigStore();