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

Commit f42ed42

Browse files
committed
tiered configuration for embeddability on build
- configuration is embeddable - use OS conventions (via Electron) to store state
1 parent 9740d9d commit f42ed42

File tree

5 files changed

+117
-37
lines changed

5 files changed

+117
-37
lines changed

src/main.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var menu = require('./menu.js');
44
var path = require('path');
55
var settings = require('./settings.js');
66

7-
settings.load();
7+
settings.load(app.getAppPath(), app.getPath('userData'));
88

99
// Keep a global reference of the window object, if you don't, the window will
1010
// be closed automatically when the JavaScript object is garbage collected.
@@ -70,7 +70,7 @@ app.on('ready', function() {
7070
settings.set('window:width', bounds.width);
7171
settings.set('window:height', bounds.height);
7272
}
73-
settings.saveState();
73+
settings.saveState(app.getPath('userData'));
7474

7575
if (process.platform != 'darwin') { return; }
7676
if (quitting) { return; }

src/settings.js

+12-13
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ var path = require('path-extra');
55

66
var settings = {};
77

8-
var getSettingsDir = function(homedir){
9-
homedir = homedir || path.homedir();
10-
return path.join(homedir, '.matterfront');
8+
var getStatePath = function(userDataPath){
9+
return path.join(userDataPath, 'state.json');
1110
};
1211

13-
var getStatePath = function(homedir){
14-
var settingsDir = getSettingsDir(homedir);
15-
return path.join(settingsDir, 'state.json');
12+
var getConfigPath = function(appPath){
13+
return path.join(appPath, 'config.json');
1614
};
1715

1816
var defaults = {
@@ -22,11 +20,13 @@ var defaults = {
2220
}
2321
};
2422

25-
settings.load = function(homedir){
26-
var statePath = getStatePath(homedir);
23+
settings.load = function(appPath, userDataPath){
24+
var statePath = getStatePath(userDataPath);
25+
var configPath = getConfigPath(appPath);
2726

2827
nconf.argv();
29-
nconf.file(statePath);
28+
nconf.file('state', statePath);
29+
nconf.file('config', configPath);
3030
nconf.defaults(defaults);
3131
};
3232

@@ -45,11 +45,10 @@ settings.append = function(key, value){
4545
return settings._current;
4646
};
4747

48-
settings.saveState = function(homedir){
49-
var settingsDir = getSettingsDir(homedir);
50-
mkdirp(settingsDir);
48+
settings.saveState = function(userDataPath){
49+
mkdirp(userDataPath);
5150

52-
var statePath = getStatePath(homedir);
51+
var statePath = getStatePath(userDataPath);
5352
var state = {
5453
teams: nconf.get("teams"),
5554
window: nconf.get("window")

test/fake-app/config.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"teams": [{
3+
"name": "teamA",
4+
"url": "http://some.server.com/teamA"
5+
}, {
6+
"name": "teamB",
7+
"url": "http://some.server.com/teamB"
8+
}],
9+
"window": {
10+
"height": 1024
11+
}
12+
}
File renamed without changes.

test/settings-spec.js

+91-22
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,105 @@ var path = require('path');
22
var settings = require('../src/settings.js');
33

44
describe('settings', function(){
5+
describe('without config but state', function(){
6+
before(function(){
7+
var fakeApp = path.join(__dirname, '.');
8+
var fakeUserData = path.join(__dirname, './fake-userdata');
9+
settings.load(fakeApp, fakeUserData);
10+
});
511

6-
before(function(){
7-
var fakeHomeDir = path.join(__dirname, './fake-home-dir');
8-
settings.load(fakeHomeDir);
9-
});
12+
it('reads command-line args', function(){
13+
//this arg is passed into the specs by mocha
14+
expect(settings.get('reporter')).to.eql('spec');
15+
});
1016

11-
it('reads command-line args', function(){
12-
//this arg is passed into the specs by mocha
13-
expect(settings.get('reporter')).to.eql('spec');
14-
});
17+
it('reads array types', function(){
18+
expect(settings.get('teams')).to.have.length(2);
19+
});
1520

16-
it('reads array types', function(){
17-
expect(settings.get('teams')).to.have.length(2);
18-
});
21+
it('reads nested objects', function(){
22+
expect(settings.get('window:width')).to.eql(800);
23+
});
1924

20-
it('reads nested objects', function(){
21-
expect(settings.get('window:width')).to.eql(800);
22-
});
25+
it('sets config values', function(){
26+
settings.set('window:width', 1920);
27+
expect(settings.get('window:width')).to.eql(1920);
28+
});
2329

24-
it('sets config values', function(){
25-
settings.set('window:width', 1920);
26-
expect(settings.get('window:width')).to.eql(1920);
30+
it('has default values', function(){
31+
expect(settings.get('window:height')).to.eql(600);
32+
});
33+
34+
it('appends config values', function(){
35+
settings.append('teams', 'http://localhost/team3');
36+
expect(settings.get('teams')).to.have.length(3);
37+
});
2738
});
2839

29-
it('has default values', function(){
30-
expect(settings.get('window:height')).to.eql(600);
40+
describe('with config but no state', function(){
41+
before(function(){
42+
var fakeApp = path.join(__dirname, './fake-app');
43+
var fakeUserData = path.join(__dirname, './');
44+
settings.load(fakeApp, fakeUserData);
45+
});
46+
47+
it('reads command-line args', function(){
48+
//this arg is passed into the specs by mocha
49+
expect(settings.get('reporter')).to.eql('spec');
50+
});
51+
52+
it('reads array types', function(){
53+
expect(settings.get('teams')).to.have.length(2);
54+
});
55+
56+
it('reads nested objects', function(){
57+
expect(settings.get('window:height')).to.eql(1024);
58+
});
59+
60+
it('sets config values', function(){
61+
settings.set('window:height', 1920);
62+
expect(settings.get('window:height')).to.eql(1920);
63+
});
64+
65+
it('has default values', function(){
66+
expect(settings.get('window:width')).to.eql(1024);
67+
});
68+
69+
it('appends config values', function(){
70+
settings.append('teams', 'http://localhost/team3');
71+
expect(settings.get('teams')).to.have.length(3);
72+
});
3173
});
3274

33-
it('appends config values', function(){
34-
settings.append('teams', 'http://localhost/team3');
35-
expect(settings.get('teams')).to.have.length(3);
75+
describe('with config and state', function(){
76+
before(function(){
77+
var fakeApp = path.join(__dirname, './fake-app');
78+
var fakeUserData = path.join(__dirname, './fake-userdata');
79+
settings.load(fakeApp, fakeUserData);
80+
});
81+
82+
it('reads command-line args', function(){
83+
//this arg is passed into the specs by mocha
84+
expect(settings.get('reporter')).to.eql('spec');
85+
});
86+
87+
it('reads array types', function(){
88+
expect(settings.get('teams')).to.have.length(2);
89+
});
90+
91+
it('reads nested objects', function(){
92+
expect(settings.get('window:width')).to.eql(800);
93+
expect(settings.get('window:height')).to.eql(1024);
94+
});
95+
96+
it('sets config values', function(){
97+
settings.set('window:width', 1920);
98+
expect(settings.get('window:width')).to.eql(1920);
99+
});
100+
101+
it('appends config values', function(){
102+
settings.append('teams', 'http://localhost/team3');
103+
expect(settings.get('teams')).to.have.length(3);
104+
});
36105
});
37106
});

0 commit comments

Comments
 (0)