Skip to content

Commit 87ca10a

Browse files
authored
Merge pull request #991 from jakejarrett/master
Utilize system specific config locations.
2 parents f260bef + 52c131d commit 87ca10a

5 files changed

Lines changed: 94 additions & 12 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
'use strict';
2+
3+
const fs = require('fs-extra');
4+
const userHome = require('user-home');
5+
const mkdirp = require('mkdirp');
6+
7+
const configuration = {
8+
9+
createUserConfig(userConfigPath) {
10+
mkdirp(userConfigPath, err => {
11+
if (err) {
12+
console.error(err);
13+
}
14+
});
15+
},
16+
17+
/**
18+
* Get the configuration folder location
19+
*
20+
* @returns {string} The folder location of the config
21+
*/
22+
getUserConfig() {
23+
let userConfigPath = null;
24+
25+
/** Windows platform */
26+
if (process.platform === 'win32') {
27+
userConfigPath = `${userHome}/.config/Soundnode`;
28+
}
29+
30+
31+
/** Linux platforms - XDG Standard */
32+
if (process.platform === 'linux') {
33+
userConfigPath = `${userHome}/.config/Soundnode`;
34+
}
35+
36+
/** Mac os configuration location */
37+
if (process.platform === 'darwin') {
38+
userConfigPath = `${userHome}/Library/Preferences/Soundnode`;
39+
}
40+
41+
// create user config in path
42+
// if there is no userConfig path
43+
if (!fs.statSync(userConfigPath).isDirectory()) {
44+
this.createUserConfig()
45+
}
46+
47+
return userConfigPath;
48+
},
49+
50+
/**
51+
* Get the configuration path
52+
*
53+
* @returns {string} The file location of the config
54+
*/
55+
getPath() {
56+
return `${this.getUserConfig()}/userConfig.json`
57+
},
58+
59+
/**
60+
* Get the config file
61+
*
62+
* @returns {Object} Parsed version of the saved file
63+
*/
64+
getConfigfile() {
65+
return JSON.parse(fs.readFileSync(`${this.getPath()}`, 'utf-8'));
66+
},
67+
68+
/**
69+
* Ensure the config exists
70+
*
71+
* @returns {Boolean} True if the file exists
72+
*/
73+
containsConfig() {
74+
return fs.existsSync(`${this.getPath()}`);
75+
}
76+
77+
}
78+
79+
module.exports = configuration;

app/public/js/system/guiConfig.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const {
44
ipcRenderer
55
} = require('electron');
66
const fs = require('fs-extra');
7+
const configuration = require('../common/configLocation');
78

89
let guiConfig = {};
910

@@ -28,10 +29,10 @@ guiConfig.maximize = function () {
2829
};
2930

3031
guiConfig.logOut = function () {
31-
fs.removeSync(`${__dirname}/userConfig.json`);
32-
this.destroy();
32+
fs.removeSync(configuration.getPath());
33+
guiConfig.destroy();
3334
};
3435

3536
module.exports = {
3637
guiConfig: guiConfig
37-
}
38+
}

app/public/js/system/settings.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"use strict";
22

33
const ua = require('universal-analytics');
4-
const fs = require('fs');
5-
const userConfig = JSON.parse(fs.readFileSync(`${__dirname}/userConfig.json`, 'utf-8'));
4+
const configuration = require('../common/configLocation');
5+
const userConfig = configuration.getConfigfile();
66

77
// Set up some core settings
88
window.settings = {};
@@ -17,4 +17,4 @@ window.settings.visitor = ua('UA-67310953-1');
1717
window.scAccessToken = userConfig.accessToken;
1818

1919
// set window clientId
20-
window.scClientId = userConfig.clientId;
20+
window.scClientId = userConfig.clientId;

main.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ const {
99
Menu
1010
} = require('electron');
1111
const windowStateKeeper = require('electron-window-state');
12+
const configuration = require('./app/public/js/common/configLocation');
1213

1314
// custom constants
1415
const clientId = '342b8a7af638944906dcdb46f9d56d98';
1516
const redirectUri = 'http://sc-redirect.herokuapp.com/callback.html';
1617
const SCconnect = `https://soundcloud.com/connect?&client_id=${clientId}&redirect_uri=${redirectUri}&response_type=token`;
17-
const userConfigPath = `${__dirname}/app/public/js/system/userConfig.json`;
1818

1919
let mainWindow;
2020
let authenticationWindow;
@@ -24,9 +24,9 @@ app.on('ready', () => {
2424
});
2525

2626
function checkUserConfig() {
27-
const userConfigExists = fs.existsSync(userConfigPath);
27+
const containsConfig = configuration.containsConfig();
2828

29-
if (userConfigExists) {
29+
if (containsConfig) {
3030
initMainWindow();
3131
} else {
3232
authenticateUser();
@@ -71,7 +71,7 @@ function authenticateUser() {
7171
}
7272

7373
function setUserData(accessToken) {
74-
fs.writeFileSync(userConfigPath, JSON.stringify({
74+
fs.writeFileSync(configuration.getPath(), JSON.stringify({
7575
accessToken: accessToken,
7676
clientId: clientId
7777
}), 'utf-8');
@@ -263,4 +263,4 @@ function menuBar() {
263263

264264
const menu = Menu.buildFromTemplate(template);
265265
Menu.setApplicationMenu(menu)
266-
}
266+
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,15 @@
5151
"fs-extra": "^3.0.1",
5252
"jquery": "^3.1.1",
5353
"lodash": "^4.17.4",
54+
"mkdirp": "^0.5.1",
5455
"moment": "^2.17.1",
5556
"ng-dialog": "^1.0.0",
5657
"ng-infinite-scroll": "^1.3.0",
5758
"normalize.css": "^7.0.0",
5859
"react": "^15.5.4",
5960
"react-dom": "^15.5.4",
6061
"toastr": "^2.1.2",
61-
"universal-analytics": "^0.4.8"
62+
"universal-analytics": "^0.4.8",
63+
"user-home": "^2.0.0"
6264
}
6365
}

0 commit comments

Comments
 (0)