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

Commit 65a8e43

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 79aafb5 commit 65a8e43

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
@@ -6,7 +6,7 @@ var menu = require('./menu.js');
66
var settings = require('./settings.js');
77
var teams = require("./teams.js");
88

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

@@ -54,7 +54,7 @@ app.on('ready', function() {
5454
settings.set('window:width', bounds.width);
5555
settings.set('window:height', bounds.height);
5656
}
57-
settings.saveState();
57+
settings.saveState(app.getPath('userData'));
5858

5959
if (process.platform != 'darwin') { return; }
6060
if (quitting) { return; }

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 = {
@@ -28,12 +25,14 @@ var defaults = {
2825
"chrome-args": {}
2926
};
3027

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

3533
nconf.argv();
36-
nconf.file("state", statePath);
34+
nconf.file('state', statePath);
35+
nconf.file('appstate', appStatePath);
3736
nconf.file("config", configPath);
3837
nconf.defaults(defaults);
3938
};
@@ -53,11 +52,10 @@ settings.append = function(key, value){
5352
return settings._current;
5453
};
5554

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

60-
var statePath = getStatePath(homedir);
58+
var statePath = getStatePath(userDataPath);
6159
var state = {
6260
teams: nconf.get("teams"),
6361
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)