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

Commit ebbf8ed

Browse files
committed
tiered configuration for embeddability on build
- configuration is embeddable - use OS conventions (via Electron) to store state be consistent: embedded config.json -> state.json
1 parent 221a07a commit ebbf8ed

File tree

7 files changed

+125
-44
lines changed

7 files changed

+125
-44
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
/dist
33
/config.json
44
/src/config.json
5+
/state.json
6+
/src/state.json
57
.tag*
68
*npm-debug.log
79
/.vagrant

src/main.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var settings = require('./settings.js');
77
var teams = require("./teams.js");
88
var tray = require("./tray.js");
99

10-
settings.load();
10+
settings.load(app.getAppPath(), app.getPath('userData'));
1111
chromeArgs.apply(settings);
1212
teams.listen();
1313

@@ -55,7 +55,7 @@ app.on('ready', function() {
5555
settings.set('window:width', bounds.width);
5656
settings.set('window:height', bounds.height);
5757
}
58-
settings.saveState();
58+
settings.saveState(app.getPath('userData'));
5959

6060
// Quit when the window is closed if not on OS X or if the tray icon is disabled.
6161
if (process.platform != 'darwin' && !tray.isEnabled()) {

src/settings.js

+15-17
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,16 @@ 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 getAppStatePath = function(appPath){
13+
return path.join(appPath, 'state.json');
1614
};
1715

18-
var getConfigPath = function(homedir){
19-
var settingsDir = getSettingsDir(homedir);
20-
return path.join(settingsDir, 'config.json');
16+
var getConfigPath = function(userDataPath){
17+
return path.join(userDataPath, 'config.json');
2118
};
2219

2320
var defaults = {
@@ -29,12 +26,14 @@ var defaults = {
2926
"showTrayIcon": false
3027
};
3128

32-
settings.load = function(homedir){
33-
var statePath = getStatePath(homedir);
34-
var configPath = getConfigPath(homedir);
29+
settings.load = function(appPath, userDataPath){
30+
var statePath = getStatePath(userDataPath);
31+
var appStatePath = getAppStatePath(appPath);
32+
var configPath = getConfigPath(userDataPath);
3533

3634
nconf.argv();
37-
nconf.file("state", statePath);
35+
nconf.file('state', statePath);
36+
nconf.file('appstate', appStatePath);
3837
nconf.file("config", configPath);
3938
nconf.defaults(defaults);
4039
};
@@ -54,11 +53,10 @@ settings.append = function(key, value){
5453
return settings._current;
5554
};
5655

57-
settings.saveState = function(homedir){
58-
var settingsDir = getSettingsDir(homedir);
59-
mkdirp(settingsDir);
56+
settings.saveState = function(userDataPath){
57+
mkdirp(userDataPath);
6058

61-
var statePath = getStatePath(homedir);
59+
var statePath = getStatePath(userDataPath);
6260
var state = {
6361
teams: nconf.get("teams"),
6462
window: nconf.get("window")

test/fake-app/state.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.
File renamed without changes.

test/settings-spec.js

+94-25
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,109 @@ 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);
27-
});
30+
it('has default values', function(){
31+
expect(settings.get('window:height')).to.eql(600);
32+
});
2833

29-
it('has default values', function(){
30-
expect(settings.get('window:height')).to.eql(600);
34+
it('appends config values', function(){
35+
settings.append('teams', 'http://localhost/team3');
36+
expect(settings.get('teams')).to.have.length(3);
37+
});
3138
});
3239

33-
it('appends config values', function(){
34-
settings.append('teams', 'http://localhost/team3');
35-
expect(settings.get('teams')).to.have.length(3);
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+
});
3673
});
3774

38-
it('reads non-state settings from `config.json`', function(){
39-
expect(settings.get('chrome-args')).to.have.property("some-arg-name", "some-arg-value");
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+
});
105+
106+
it('reads non-state settings from `config.json`', function(){
107+
expect(settings.get('chrome-args')).to.have.property("some-arg-name", "some-arg-value");
108+
});
40109
});
41110
});

0 commit comments

Comments
 (0)