From deabbd963c84de55160458a2cc6f8bf5e0d1ce3e Mon Sep 17 00:00:00 2001 From: Loic Nageleisen Date: Wed, 9 Dec 2015 15:24:48 +0100 Subject: [PATCH] tiered configuration for embeddability on build - configuration is embeddable - use OS conventions (via Electron) to store state --- src/main.js | 4 +- src/settings.js | 25 ++-- test/fake-app/config.json | 12 ++ .../.matterfront => fake-userdata}/state.json | 0 test/settings-spec.js | 113 ++++++++++++++---- 5 files changed, 117 insertions(+), 37 deletions(-) create mode 100644 test/fake-app/config.json rename test/{fake-home-dir/.matterfront => fake-userdata}/state.json (100%) diff --git a/src/main.js b/src/main.js index ca34690..9748e66 100644 --- a/src/main.js +++ b/src/main.js @@ -4,7 +4,7 @@ var menu = require('./menu.js'); var path = require('path'); var settings = require('./settings.js'); -settings.load(); +settings.load(app.getAppPath(), app.getPath('userData')); // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. @@ -62,7 +62,7 @@ app.on('ready', function() { settings.set('window:width', bounds.width); settings.set('window:height', bounds.height); } - settings.saveState(); + settings.saveState(app.getPath('userData')); if (process.platform != 'darwin') { return; } if (quitting) { return; } diff --git a/src/settings.js b/src/settings.js index 6027f41..52e4ef6 100644 --- a/src/settings.js +++ b/src/settings.js @@ -5,14 +5,12 @@ var path = require('path-extra'); var settings = {}; -var getSettingsDir = function(homedir){ - homedir = homedir || path.homedir(); - return path.join(homedir, '.matterfront'); +var getStatePath = function(userDataPath){ + return path.join(userDataPath, 'state.json'); }; -var getStatePath = function(homedir){ - var settingsDir = getSettingsDir(homedir); - return path.join(settingsDir, 'state.json'); +var getConfigPath = function(appPath){ + return path.join(appPath, 'config.json'); }; var defaults = { @@ -22,11 +20,13 @@ var defaults = { } }; -settings.load = function(homedir){ - var statePath = getStatePath(homedir); +settings.load = function(appPath, userDataPath){ + var statePath = getStatePath(userDataPath); + var configPath = getConfigPath(appPath); nconf.argv(); - nconf.file(statePath); + nconf.file('state', statePath); + nconf.file('config', configPath); nconf.defaults(defaults); }; @@ -45,11 +45,10 @@ settings.append = function(key, value){ return settings._current; }; -settings.saveState = function(homedir){ - var settingsDir = getSettingsDir(homedir); - mkdirp(settingsDir); +settings.saveState = function(userDataPath){ + mkdirp(userDataPath); - var statePath = getStatePath(homedir); + var statePath = getStatePath(userDataPath); var state = { teams: nconf.get("teams"), window: nconf.get("window") diff --git a/test/fake-app/config.json b/test/fake-app/config.json new file mode 100644 index 0000000..a1bc515 --- /dev/null +++ b/test/fake-app/config.json @@ -0,0 +1,12 @@ +{ + "teams": [{ + "name": "teamA", + "url": "http://some.server.com/teamA" + }, { + "name": "teamB", + "url": "http://some.server.com/teamB" + }], + "window": { + "height": 1024 + } +} diff --git a/test/fake-home-dir/.matterfront/state.json b/test/fake-userdata/state.json similarity index 100% rename from test/fake-home-dir/.matterfront/state.json rename to test/fake-userdata/state.json diff --git a/test/settings-spec.js b/test/settings-spec.js index b6d7720..b03ba2b 100644 --- a/test/settings-spec.js +++ b/test/settings-spec.js @@ -2,36 +2,105 @@ var path = require('path'); var settings = require('../src/settings.js'); describe('settings', function(){ + describe('without config but state', function(){ + before(function(){ + var fakeApp = path.join(__dirname, '.'); + var fakeUserData = path.join(__dirname, './fake-userdata'); + settings.load(fakeApp, fakeUserData); + }); - before(function(){ - var fakeHomeDir = path.join(__dirname, './fake-home-dir'); - settings.load(fakeHomeDir); - }); + it('reads command-line args', function(){ + //this arg is passed into the specs by mocha + expect(settings.get('reporter')).to.eql('spec'); + }); - it('reads command-line args', function(){ - //this arg is passed into the specs by mocha - expect(settings.get('reporter')).to.eql('spec'); - }); + it('reads array types', function(){ + expect(settings.get('teams')).to.have.length(2); + }); - it('reads array types', function(){ - expect(settings.get('teams')).to.have.length(2); - }); + it('reads nested objects', function(){ + expect(settings.get('window:width')).to.eql(800); + }); - it('reads nested objects', function(){ - expect(settings.get('window:width')).to.eql(800); - }); + it('sets config values', function(){ + settings.set('window:width', 1920); + expect(settings.get('window:width')).to.eql(1920); + }); - it('sets config values', function(){ - settings.set('window:width', 1920); - expect(settings.get('window:width')).to.eql(1920); + it('has default values', function(){ + expect(settings.get('window:height')).to.eql(600); + }); + + it('appends config values', function(){ + settings.append('teams', 'http://localhost/team3'); + expect(settings.get('teams')).to.have.length(3); + }); }); - it('has default values', function(){ - expect(settings.get('window:height')).to.eql(600); + describe('with config but no state', function(){ + before(function(){ + var fakeApp = path.join(__dirname, './fake-app'); + var fakeUserData = path.join(__dirname, './'); + settings.load(fakeApp, fakeUserData); + }); + + it('reads command-line args', function(){ + //this arg is passed into the specs by mocha + expect(settings.get('reporter')).to.eql('spec'); + }); + + it('reads array types', function(){ + expect(settings.get('teams')).to.have.length(2); + }); + + it('reads nested objects', function(){ + expect(settings.get('window:height')).to.eql(1024); + }); + + it('sets config values', function(){ + settings.set('window:height', 1920); + expect(settings.get('window:height')).to.eql(1920); + }); + + it('has default values', function(){ + expect(settings.get('window:width')).to.eql(1024); + }); + + it('appends config values', function(){ + settings.append('teams', 'http://localhost/team3'); + expect(settings.get('teams')).to.have.length(3); + }); }); - it('appends config values', function(){ - settings.append('teams', 'http://localhost/team3'); - expect(settings.get('teams')).to.have.length(3); + describe('with config and state', function(){ + before(function(){ + var fakeApp = path.join(__dirname, './fake-app'); + var fakeUserData = path.join(__dirname, './fake-userdata'); + settings.load(fakeApp, fakeUserData); + }); + + it('reads command-line args', function(){ + //this arg is passed into the specs by mocha + expect(settings.get('reporter')).to.eql('spec'); + }); + + it('reads array types', function(){ + expect(settings.get('teams')).to.have.length(2); + }); + + it('reads nested objects', function(){ + expect(settings.get('window:width')).to.eql(800); + expect(settings.get('window:height')).to.eql(1024); + }); + + it('sets config values', function(){ + settings.set('window:width', 1920); + expect(settings.get('window:width')).to.eql(1920); + }); + + it('appends config values', function(){ + settings.append('teams', 'http://localhost/team3'); + expect(settings.get('teams')).to.have.length(3); + }); }); });