diff --git a/app/public/js/common/configLocation.js b/app/public/js/common/configLocation.js new file mode 100644 index 00000000..8dda7e44 --- /dev/null +++ b/app/public/js/common/configLocation.js @@ -0,0 +1,79 @@ +'use strict'; + +const fs = require('fs-extra'); +const userHome = require('user-home'); +const mkdirp = require('mkdirp'); + +const configuration = { + + createUserConfig(userConfigPath) { + mkdirp(userConfigPath, err => { + if (err) { + console.error(err); + } + }); + }, + + /** + * Get the configuration folder location + * + * @returns {string} The folder location of the config + */ + getUserConfig() { + let userConfigPath = null; + + /** Windows platform */ + if (process.platform === 'win32') { + userConfigPath = `${userHome}/.config/Soundnode`; + } + + + /** Linux platforms - XDG Standard */ + if (process.platform === 'linux') { + userConfigPath = `${userHome}/.config/Soundnode`; + } + + /** Mac os configuration location */ + if (process.platform === 'darwin') { + userConfigPath = `${userHome}/Library/Preferences/Soundnode`; + } + + // create user config in path + // if there is no userConfig path + if (!fs.statSync(userConfigPath).isDirectory()) { + this.createUserConfig() + } + + return userConfigPath; + }, + + /** + * Get the configuration path + * + * @returns {string} The file location of the config + */ + getPath() { + return `${this.getUserConfig()}/userConfig.json` + }, + + /** + * Get the config file + * + * @returns {Object} Parsed version of the saved file + */ + getConfigfile() { + return JSON.parse(fs.readFileSync(`${this.getPath()}`, 'utf-8')); + }, + + /** + * Ensure the config exists + * + * @returns {Boolean} True if the file exists + */ + containsConfig() { + return fs.existsSync(`${this.getPath()}`); + } + +} + +module.exports = configuration; diff --git a/app/public/js/system/guiConfig.js b/app/public/js/system/guiConfig.js index e46725fc..3be1f089 100644 --- a/app/public/js/system/guiConfig.js +++ b/app/public/js/system/guiConfig.js @@ -4,6 +4,7 @@ const { ipcRenderer } = require('electron'); const fs = require('fs-extra'); +const configuration = require('../common/configLocation'); let guiConfig = {}; @@ -28,10 +29,10 @@ guiConfig.maximize = function () { }; guiConfig.logOut = function () { - fs.removeSync(`${__dirname}/userConfig.json`); - this.destroy(); + fs.removeSync(configuration.getPath()); + guiConfig.destroy(); }; module.exports = { guiConfig: guiConfig -} \ No newline at end of file +} diff --git a/app/public/js/system/settings.js b/app/public/js/system/settings.js index 068b4c44..6732133c 100644 --- a/app/public/js/system/settings.js +++ b/app/public/js/system/settings.js @@ -1,8 +1,8 @@ "use strict"; const ua = require('universal-analytics'); -const fs = require('fs'); -const userConfig = JSON.parse(fs.readFileSync(`${__dirname}/userConfig.json`, 'utf-8')); +const configuration = require('../common/configLocation'); +const userConfig = configuration.getConfigfile(); // Set up some core settings window.settings = {}; @@ -17,4 +17,4 @@ window.settings.visitor = ua('UA-67310953-1'); window.scAccessToken = userConfig.accessToken; // set window clientId -window.scClientId = userConfig.clientId; \ No newline at end of file +window.scClientId = userConfig.clientId; diff --git a/main.js b/main.js index 68247f22..01a24077 100644 --- a/main.js +++ b/main.js @@ -9,12 +9,12 @@ const { Menu } = require('electron'); const windowStateKeeper = require('electron-window-state'); +const configuration = require('./app/public/js/common/configLocation'); // custom constants const clientId = '342b8a7af638944906dcdb46f9d56d98'; const redirectUri = 'http://sc-redirect.herokuapp.com/callback.html'; const SCconnect = `https://soundcloud.com/connect?&client_id=${clientId}&redirect_uri=${redirectUri}&response_type=token`; -const userConfigPath = `${__dirname}/app/public/js/system/userConfig.json`; let mainWindow; let authenticationWindow; @@ -24,9 +24,9 @@ app.on('ready', () => { }); function checkUserConfig() { - const userConfigExists = fs.existsSync(userConfigPath); + const containsConfig = configuration.containsConfig(); - if (userConfigExists) { + if (containsConfig) { initMainWindow(); } else { authenticateUser(); @@ -71,7 +71,7 @@ function authenticateUser() { } function setUserData(accessToken) { - fs.writeFileSync(userConfigPath, JSON.stringify({ + fs.writeFileSync(configuration.getPath(), JSON.stringify({ accessToken: accessToken, clientId: clientId }), 'utf-8'); @@ -263,4 +263,4 @@ function menuBar() { const menu = Menu.buildFromTemplate(template); Menu.setApplicationMenu(menu) -} \ No newline at end of file +} diff --git a/package.json b/package.json index 249289e6..8edd0e07 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "fs-extra": "^2.0.0", "jquery": "^3.1.1", "lodash": "^4.17.4", + "mkdirp": "^0.5.1", "moment": "^2.17.1", "ng-dialog": "^1.0.0", "ng-infinite-scroll": "^1.3.0", @@ -59,6 +60,7 @@ "react": "^15.4.2", "react-dom": "^15.4.2", "toastr": "^2.1.2", - "universal-analytics": "^0.4.8" + "universal-analytics": "^0.4.8", + "user-home": "^2.0.0" } }