Skip to content

Commit 1d40883

Browse files
committed
Setup config location as a ES6 class w/ static getters
1 parent b9d2cf8 commit 1d40883

4 files changed

Lines changed: 60 additions & 42 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const fs = require('fs-extra');
2+
const userHome = require('user-home');
3+
4+
class Configuration {
5+
6+
/**
7+
* Get the configuration path
8+
*
9+
* @returns {string} The location of the config
10+
*/
11+
static get path () {
12+
let userConfigPath = null;
13+
14+
/** Linux platforms - XDG Standard */
15+
if (process.platform === 'win32') {
16+
userConfigPath = `${userHome}/.config/Soundnode/userConfig.json`;
17+
}
18+
19+
20+
/** Linux platforms - XDG Standard */
21+
if (process.platform === 'linux') {
22+
userConfigPath = `${userHome}/.config/Soundnode/userConfig.json`;
23+
}
24+
25+
/** Mac os configuration location */
26+
if (process.platform === 'darwin') {
27+
userConfigPath = `${userHome}/Library/Preferences/Soundnode/userConfig.json`;
28+
}
29+
30+
return userConfigPath;
31+
}
32+
33+
/**
34+
* Get the config file
35+
*
36+
* @returns {Object} Parsed version of the saved file
37+
*/
38+
static get file () {
39+
return JSON.parse(fs.readFileSync(`${this.path}`, 'utf-8'));
40+
}
41+
42+
/**
43+
* Ensure the config exists
44+
*
45+
* @returns {Boolean} True if the file exists
46+
*/
47+
static get configExists () {
48+
return fs.existsSync(this.path);
49+
}
50+
51+
}
52+
53+
module.exports = Configuration;

app/public/js/system/guiConfig.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const {
55
} = require('electron');
66
const fs = require('fs-extra');
77
const userHome = require('user-home');
8+
const Configuration = require('../common/configLocation');
89

910
let guiConfig = {};
1011

@@ -29,19 +30,7 @@ guiConfig.maximize = function () {
2930
};
3031

3132
guiConfig.logOut = function () {
32-
let _userConfigPath = `${__dirname}/app/public/js/system/userConfig.json`; // Windows specific for now
33-
34-
/** Linux platforms - XDG Standard */
35-
if (process.platform === 'linux') {
36-
_userConfigPath = `${userHome}/.config/Soundnode/userConfig.json`;
37-
}
38-
39-
/** Mac os configuration location */
40-
if (process.platform === 'darwin') {
41-
_userConfigPath = `${userHome}/Library/Preferences/Soundnode/userConfig.json`;
42-
}
43-
44-
fs.removeSync(`${_userConfigPath}`);
33+
fs.removeSync(Configuration.path);
4534
this.destroy();
4635
};
4736

app/public/js/system/settings.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,8 @@
22

33
const ua = require('universal-analytics');
44
const fs = require('fs');
5-
const userHome = require('user-home');
6-
7-
let _userConfigPath = `${__dirname}/app/public/js/system/userConfig.json`; // Windows specific for now
8-
/** Linux platforms - XDG Standard */
9-
if (process.platform === 'linux') {
10-
_userConfigPath = `${userHome}/.config/Soundnode/userConfig.json`;
11-
}
12-
13-
/** Mac os configuration location */
14-
if (process.platform === 'darwin') {
15-
_userConfigPath = `${userHome}/Library/Preferences/Soundnode/userConfig.json`;
16-
}
17-
const userConfig = JSON.parse(fs.readFileSync(`${_userConfigPath}`, 'utf-8'));
5+
const Configuration = require('../common/configLocation');
6+
const userConfig = Configuration.file;
187

198
// Set up some core settings
209
window.settings = {};

main.js

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,12 @@ const {
99
Menu
1010
} = require('electron');
1111
const windowStateKeeper = require('electron-window-state');
12-
const userHome = require('user-home');
12+
const Configuration = require('./app/public/js/common/configLocation');
1313

1414
// custom constants
1515
const clientId = '342b8a7af638944906dcdb46f9d56d98';
1616
const redirectUri = 'http://sc-redirect.herokuapp.com/callback.html';
1717
const SCconnect = `https://soundcloud.com/connect?&client_id=${clientId}&redirect_uri=${redirectUri}&response_type=token`;
18-
let _userConfigPath = `${__dirname}/app/public/js/system/userConfig.json`; // Windows specific for now
19-
20-
/** Linux platforms - XDG Standard */
21-
if (process.platform === 'linux') {
22-
_userConfigPath = `${userHome}/.config/Soundnode/userConfig.json`;
23-
}
24-
25-
/** Mac os configuration location */
26-
if (process.platform === 'darwin') {
27-
_userConfigPath = `${userHome}/Library/Preferences/Soundnode/userConfig.json`;
28-
}
29-
30-
const userConfigPath = _userConfigPath.slice(0); // Just to make sure it doesn't keep reference to the mutable string.
3118

3219
let mainWindow;
3320
let authenticationWindow;
@@ -37,7 +24,7 @@ app.on('ready', () => {
3724
});
3825

3926
function checkUserConfig() {
40-
const userConfigExists = fs.existsSync(userConfigPath);
27+
const userConfigExists = Configuration.configExists;
4128

4229
if (userConfigExists) {
4330
initMainWindow();
@@ -84,7 +71,7 @@ function authenticateUser() {
8471
}
8572

8673
function setUserData(accessToken) {
87-
fs.writeFileSync(userConfigPath, JSON.stringify({
74+
fs.writeFileSync(Configuration.path, JSON.stringify({
8875
accessToken: accessToken,
8976
clientId: clientId
9077
}), 'utf-8');

0 commit comments

Comments
 (0)