Skip to content
This repository has been archived by the owner on Mar 7, 2018. It is now read-only.

Commit

Permalink
tiered configuration for embeddability on build
Browse files Browse the repository at this point in the history
- configuration is embeddable
- use OS conventions (via Electron) to store state
  • Loading branch information
lloeki committed Dec 9, 2015
1 parent c280139 commit deabbd9
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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; }
Expand Down
25 changes: 12 additions & 13 deletions src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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);
};

Expand All @@ -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")
Expand Down
12 changes: 12 additions & 0 deletions test/fake-app/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"teams": [{
"name": "teamA",
"url": "http://some.server.com/teamA"
}, {
"name": "teamB",
"url": "http://some.server.com/teamB"
}],
"window": {
"height": 1024
}
}
File renamed without changes.
113 changes: 91 additions & 22 deletions test/settings-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

0 comments on commit deabbd9

Please sign in to comment.