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

be consistent: embedded config.json -> state.json
  • Loading branch information
lloeki committed Feb 22, 2016
1 parent 534426c commit e77336f
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 44 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
/dist
/config.json
/src/config.json
/state.json
/src/state.json
.tag*
*npm-debug.log
/.vagrant
Expand Down
4 changes: 2 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var settings = require('./settings.js');
var teams = require("./teams.js");
var tray = require("./tray.js");

settings.load();
settings.load(app.getAppPath(), app.getPath('userData'));
chromeArgs.apply(settings);
teams.listen();

Expand Down Expand Up @@ -55,7 +55,7 @@ app.on('ready', function() {
settings.set('window:width', bounds.width);
settings.set('window:height', bounds.height);
}
settings.saveState();
settings.saveState(app.getPath('userData'));

// Quit when the window is closed if not on OS X or if the tray icon is disabled.
if (process.platform != 'darwin' && !tray.isEnabled()) {
Expand Down
32 changes: 15 additions & 17 deletions src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@ 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 getAppStatePath = function(appPath){
return path.join(appPath, 'state.json');
};

var getConfigPath = function(homedir){
var settingsDir = getSettingsDir(homedir);
return path.join(settingsDir, 'config.json');
var getConfigPath = function(userDataPath){
return path.join(userDataPath, 'config.json');
};

var defaults = {
Expand All @@ -29,12 +26,14 @@ var defaults = {
"showTrayIcon": false
};

settings.load = function(homedir){
var statePath = getStatePath(homedir);
var configPath = getConfigPath(homedir);
settings.load = function(appPath, userDataPath){
var statePath = getStatePath(userDataPath);
var appStatePath = getAppStatePath(appPath);
var configPath = getConfigPath(userDataPath);

nconf.argv();
nconf.file("state", statePath);
nconf.file('state', statePath);
nconf.file('appstate', appStatePath);
nconf.file("config", configPath);
nconf.defaults(defaults);
};
Expand All @@ -54,11 +53,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/state.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.
119 changes: 94 additions & 25 deletions test/settings-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,109 @@ 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('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('appends config values', function(){
settings.append('teams', 'http://localhost/team3');
expect(settings.get('teams')).to.have.length(3);
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('reads non-state settings from `config.json`', function(){
expect(settings.get('chrome-args')).to.have.property("some-arg-name", "some-arg-value");
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);
});

it('reads non-state settings from `config.json`', function(){
expect(settings.get('chrome-args')).to.have.property("some-arg-name", "some-arg-value");
});
});
});

0 comments on commit e77336f

Please sign in to comment.