From b9d2cf869fe9eee73d7e4787c517594dfc45d3e6 Mon Sep 17 00:00:00 2001 From: Jake Jarrett Date: Tue, 6 Jun 2017 20:42:29 +1000 Subject: [PATCH 01/10] Utilize system specific config locations. Linux configs utilize the XDG Specification `~/.config/soundnode/` Mac os utilizes what i could gather as `~/Library/Preferences/Soundnode` Not sure about the mac os location though. --- app/public/js/system/guiConfig.js | 15 ++++++++++++++- app/public/js/system/settings.js | 14 +++++++++++++- main.js | 15 ++++++++++++++- package.json | 3 ++- 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/app/public/js/system/guiConfig.js b/app/public/js/system/guiConfig.js index e46725fc..8211064d 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 userHome = require('user-home'); let guiConfig = {}; @@ -28,7 +29,19 @@ guiConfig.maximize = function () { }; guiConfig.logOut = function () { - fs.removeSync(`${__dirname}/userConfig.json`); + let _userConfigPath = `${__dirname}/app/public/js/system/userConfig.json`; // Windows specific for now + + /** Linux platforms - XDG Standard */ + if (process.platform === 'linux') { + _userConfigPath = `${userHome}/.config/Soundnode/userConfig.json`; + } + + /** Mac os configuration location */ + if (process.platform === 'darwin') { + _userConfigPath = `${userHome}/Library/Preferences/Soundnode/userConfig.json`; + } + + fs.removeSync(`${_userConfigPath}`); this.destroy(); }; diff --git a/app/public/js/system/settings.js b/app/public/js/system/settings.js index 068b4c44..321aa2cf 100644 --- a/app/public/js/system/settings.js +++ b/app/public/js/system/settings.js @@ -2,7 +2,19 @@ const ua = require('universal-analytics'); const fs = require('fs'); -const userConfig = JSON.parse(fs.readFileSync(`${__dirname}/userConfig.json`, 'utf-8')); +const userHome = require('user-home'); + +let _userConfigPath = `${__dirname}/app/public/js/system/userConfig.json`; // Windows specific for now +/** Linux platforms - XDG Standard */ +if (process.platform === 'linux') { + _userConfigPath = `${userHome}/.config/Soundnode/userConfig.json`; +} + +/** Mac os configuration location */ +if (process.platform === 'darwin') { + _userConfigPath = `${userHome}/Library/Preferences/Soundnode/userConfig.json`; +} +const userConfig = JSON.parse(fs.readFileSync(`${_userConfigPath}`, 'utf-8')); // Set up some core settings window.settings = {}; diff --git a/main.js b/main.js index 68247f22..c77dea44 100644 --- a/main.js +++ b/main.js @@ -9,12 +9,25 @@ const { Menu } = require('electron'); const windowStateKeeper = require('electron-window-state'); +const userHome = require('user-home'); // 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 _userConfigPath = `${__dirname}/app/public/js/system/userConfig.json`; // Windows specific for now + +/** Linux platforms - XDG Standard */ +if (process.platform === 'linux') { + _userConfigPath = `${userHome}/.config/Soundnode/userConfig.json`; +} + +/** Mac os configuration location */ +if (process.platform === 'darwin') { + _userConfigPath = `${userHome}/Library/Preferences/Soundnode/userConfig.json`; +} + +const userConfigPath = _userConfigPath.slice(0); // Just to make sure it doesn't keep reference to the mutable string. let mainWindow; let authenticationWindow; diff --git a/package.json b/package.json index 249289e6..02a1d04a 100644 --- a/package.json +++ b/package.json @@ -59,6 +59,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" } } From 1d4088313d36a29039db391bc778c7424025d4dd Mon Sep 17 00:00:00 2001 From: Jake Jarrett Date: Tue, 13 Jun 2017 19:15:53 +1000 Subject: [PATCH 02/10] Setup config location as a ES6 class w/ static getters --- app/public/js/common/configLocation.js | 53 ++++++++++++++++++++++++++ app/public/js/system/guiConfig.js | 15 +------- app/public/js/system/settings.js | 15 +------- main.js | 19 ++------- 4 files changed, 60 insertions(+), 42 deletions(-) create mode 100644 app/public/js/common/configLocation.js diff --git a/app/public/js/common/configLocation.js b/app/public/js/common/configLocation.js new file mode 100644 index 00000000..b1a0f234 --- /dev/null +++ b/app/public/js/common/configLocation.js @@ -0,0 +1,53 @@ +const fs = require('fs-extra'); +const userHome = require('user-home'); + +class Configuration { + + /** + * Get the configuration path + * + * @returns {string} The location of the config + */ + static get path () { + let userConfigPath = null; + + /** Linux platforms - XDG Standard */ + if (process.platform === 'win32') { + userConfigPath = `${userHome}/.config/Soundnode/userConfig.json`; + } + + + /** Linux platforms - XDG Standard */ + if (process.platform === 'linux') { + userConfigPath = `${userHome}/.config/Soundnode/userConfig.json`; + } + + /** Mac os configuration location */ + if (process.platform === 'darwin') { + userConfigPath = `${userHome}/Library/Preferences/Soundnode/userConfig.json`; + } + + return userConfigPath; + } + + /** + * Get the config file + * + * @returns {Object} Parsed version of the saved file + */ + static get file () { + return JSON.parse(fs.readFileSync(`${this.path}`, 'utf-8')); + } + + /** + * Ensure the config exists + * + * @returns {Boolean} True if the file exists + */ + static get configExists () { + return fs.existsSync(this.path); + } + +} + +module.exports = Configuration; \ No newline at end of file diff --git a/app/public/js/system/guiConfig.js b/app/public/js/system/guiConfig.js index 8211064d..08a877a9 100644 --- a/app/public/js/system/guiConfig.js +++ b/app/public/js/system/guiConfig.js @@ -5,6 +5,7 @@ const { } = require('electron'); const fs = require('fs-extra'); const userHome = require('user-home'); +const Configuration = require('../common/configLocation'); let guiConfig = {}; @@ -29,19 +30,7 @@ guiConfig.maximize = function () { }; guiConfig.logOut = function () { - let _userConfigPath = `${__dirname}/app/public/js/system/userConfig.json`; // Windows specific for now - - /** Linux platforms - XDG Standard */ - if (process.platform === 'linux') { - _userConfigPath = `${userHome}/.config/Soundnode/userConfig.json`; - } - - /** Mac os configuration location */ - if (process.platform === 'darwin') { - _userConfigPath = `${userHome}/Library/Preferences/Soundnode/userConfig.json`; - } - - fs.removeSync(`${_userConfigPath}`); + fs.removeSync(Configuration.path); this.destroy(); }; diff --git a/app/public/js/system/settings.js b/app/public/js/system/settings.js index 321aa2cf..d1ce31a8 100644 --- a/app/public/js/system/settings.js +++ b/app/public/js/system/settings.js @@ -2,19 +2,8 @@ const ua = require('universal-analytics'); const fs = require('fs'); -const userHome = require('user-home'); - -let _userConfigPath = `${__dirname}/app/public/js/system/userConfig.json`; // Windows specific for now -/** Linux platforms - XDG Standard */ -if (process.platform === 'linux') { - _userConfigPath = `${userHome}/.config/Soundnode/userConfig.json`; -} - -/** Mac os configuration location */ -if (process.platform === 'darwin') { - _userConfigPath = `${userHome}/Library/Preferences/Soundnode/userConfig.json`; -} -const userConfig = JSON.parse(fs.readFileSync(`${_userConfigPath}`, 'utf-8')); +const Configuration = require('../common/configLocation'); +const userConfig = Configuration.file; // Set up some core settings window.settings = {}; diff --git a/main.js b/main.js index c77dea44..fa5ff05d 100644 --- a/main.js +++ b/main.js @@ -9,25 +9,12 @@ const { Menu } = require('electron'); const windowStateKeeper = require('electron-window-state'); -const userHome = require('user-home'); +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`; -let _userConfigPath = `${__dirname}/app/public/js/system/userConfig.json`; // Windows specific for now - -/** Linux platforms - XDG Standard */ -if (process.platform === 'linux') { - _userConfigPath = `${userHome}/.config/Soundnode/userConfig.json`; -} - -/** Mac os configuration location */ -if (process.platform === 'darwin') { - _userConfigPath = `${userHome}/Library/Preferences/Soundnode/userConfig.json`; -} - -const userConfigPath = _userConfigPath.slice(0); // Just to make sure it doesn't keep reference to the mutable string. let mainWindow; let authenticationWindow; @@ -37,7 +24,7 @@ app.on('ready', () => { }); function checkUserConfig() { - const userConfigExists = fs.existsSync(userConfigPath); + const userConfigExists = Configuration.configExists; if (userConfigExists) { initMainWindow(); @@ -84,7 +71,7 @@ function authenticateUser() { } function setUserData(accessToken) { - fs.writeFileSync(userConfigPath, JSON.stringify({ + fs.writeFileSync(Configuration.path, JSON.stringify({ accessToken: accessToken, clientId: clientId }), 'utf-8'); From c4e3681cacb0f0d79b958577b3e93df72bcf8b09 Mon Sep 17 00:00:00 2001 From: Jake Jarrett Date: Sat, 17 Jun 2017 09:44:10 +1000 Subject: [PATCH 03/10] Ensure the folder exists before trying to modify its children This adds a private function (Possibly could abstract it out to a class, but it's not used anywhere else) to simply determine if a given location is a folder & if it exists. --- app/public/js/common/configLocation.js | 39 +++++++++++++++++++++----- package.json | 1 + 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/app/public/js/common/configLocation.js b/app/public/js/common/configLocation.js index b1a0f234..adbf5844 100644 --- a/app/public/js/common/configLocation.js +++ b/app/public/js/common/configLocation.js @@ -1,35 +1,60 @@ const fs = require('fs-extra'); const userHome = require('user-home'); +const mkdirp = require('mkdirp'); + +/** + * Check if a given folder exists before trying to access any of it's children + * + * @param {String} folder The folder we're checking + */ +const folderExists = folder => { + let exists = false; + fs.stat(folder, (err, stats) => { if (stats.isDirectory()) exists = true; }); + return exists; +}; class Configuration { /** - * Get the configuration path + * Get the configuration folder location * - * @returns {string} The location of the config + * @returns {string} The folder location of the config */ - static get path () { + static get location () { let userConfigPath = null; /** Linux platforms - XDG Standard */ if (process.platform === 'win32') { - userConfigPath = `${userHome}/.config/Soundnode/userConfig.json`; + userConfigPath = `${userHome}/.config/Soundnode`; } /** Linux platforms - XDG Standard */ if (process.platform === 'linux') { - userConfigPath = `${userHome}/.config/Soundnode/userConfig.json`; + userConfigPath = `${userHome}/.config/Soundnode`; } /** Mac os configuration location */ if (process.platform === 'darwin') { - userConfigPath = `${userHome}/Library/Preferences/Soundnode/userConfig.json`; + userConfigPath = `${userHome}/Library/Preferences/Soundnode`; + } + + if (!folderExists(userConfigPath)) { + mkdirp(userConfigPath, err => { if (err) console.error(err); }); } return userConfigPath; } + /** + * Get the configuration path + * + * @returns {string} The file location of the config + */ + static get path () { + return `${this.location}/userConfig.json` + } + /** * Get the config file * @@ -45,7 +70,7 @@ class Configuration { * @returns {Boolean} True if the file exists */ static get configExists () { - return fs.existsSync(this.path); + return fs.existsSync(`${this.path}`); } } diff --git a/package.json b/package.json index 02a1d04a..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", From dba55343bc337b9f6d892993d79680a0ab9e9970 Mon Sep 17 00:00:00 2001 From: Jake Jarrett Date: Sat, 15 Jul 2017 08:30:11 +1000 Subject: [PATCH 04/10] Code formatting & utilise syncronous version of stat --- app/public/js/common/configLocation.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/public/js/common/configLocation.js b/app/public/js/common/configLocation.js index adbf5844..9b54bb64 100644 --- a/app/public/js/common/configLocation.js +++ b/app/public/js/common/configLocation.js @@ -9,7 +9,12 @@ const mkdirp = require('mkdirp'); */ const folderExists = folder => { let exists = false; - fs.stat(folder, (err, stats) => { if (stats.isDirectory()) exists = true; }); + fs.statSync(folder, (err, stats) => { + if (stats.isDirectory()) { + exists = true; + } + }); + return exists; }; @@ -40,7 +45,11 @@ class Configuration { } if (!folderExists(userConfigPath)) { - mkdirp(userConfigPath, err => { if (err) console.error(err); }); + mkdirp(userConfigPath, err => { + if (err) { + console.error(err); + } + }); } return userConfigPath; From 3c9af63fce467a6d8dd1bda24463f5aa1a93051a Mon Sep 17 00:00:00 2001 From: Jake Jarrett Date: Sat, 15 Jul 2017 08:30:51 +1000 Subject: [PATCH 05/10] Make windows comment the correct platform --- app/public/js/common/configLocation.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/public/js/common/configLocation.js b/app/public/js/common/configLocation.js index 9b54bb64..749334f6 100644 --- a/app/public/js/common/configLocation.js +++ b/app/public/js/common/configLocation.js @@ -28,7 +28,7 @@ class Configuration { static get location () { let userConfigPath = null; - /** Linux platforms - XDG Standard */ + /** Windows platform */ if (process.platform === 'win32') { userConfigPath = `${userHome}/.config/Soundnode`; } From 9fe1019ec69d0ee0923ac84f749ecf387f95662a Mon Sep 17 00:00:00 2001 From: Michael Lancaster Date: Wed, 19 Jul 2017 11:51:41 -0700 Subject: [PATCH 06/10] remove folderExist func and use sync fs instead --- app/public/js/common/configLocation.js | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/app/public/js/common/configLocation.js b/app/public/js/common/configLocation.js index 749334f6..93a8da1e 100644 --- a/app/public/js/common/configLocation.js +++ b/app/public/js/common/configLocation.js @@ -2,22 +2,6 @@ const fs = require('fs-extra'); const userHome = require('user-home'); const mkdirp = require('mkdirp'); -/** - * Check if a given folder exists before trying to access any of it's children - * - * @param {String} folder The folder we're checking - */ -const folderExists = folder => { - let exists = false; - fs.statSync(folder, (err, stats) => { - if (stats.isDirectory()) { - exists = true; - } - }); - - return exists; -}; - class Configuration { /** @@ -44,7 +28,7 @@ class Configuration { userConfigPath = `${userHome}/Library/Preferences/Soundnode`; } - if (!folderExists(userConfigPath)) { + if (!fs.statSync(userConfigPath).isDirectory()) { mkdirp(userConfigPath, err => { if (err) { console.error(err); @@ -84,4 +68,4 @@ class Configuration { } -module.exports = Configuration; \ No newline at end of file +module.exports = Configuration; From d908edc1a4d0d663b59d4ec607110a7b9aece059 Mon Sep 17 00:00:00 2001 From: Michael Lancaster Date: Wed, 19 Jul 2017 14:38:10 -0700 Subject: [PATCH 07/10] Move Configuration to a object and methods Instead of using class we will use a obj that contain the get methods needed that way making simpler when importing in other files. --- app/public/js/common/configLocation.js | 114 +++++++++++++------------ 1 file changed, 61 insertions(+), 53 deletions(-) diff --git a/app/public/js/common/configLocation.js b/app/public/js/common/configLocation.js index 93a8da1e..8dda7e44 100644 --- a/app/public/js/common/configLocation.js +++ b/app/public/js/common/configLocation.js @@ -1,71 +1,79 @@ +'use strict'; + const fs = require('fs-extra'); const userHome = require('user-home'); const mkdirp = require('mkdirp'); -class Configuration { - - /** - * Get the configuration folder location - * - * @returns {string} The folder location of the config - */ - static get location () { - let userConfigPath = null; +const configuration = { - /** Windows platform */ - if (process.platform === 'win32') { - userConfigPath = `${userHome}/.config/Soundnode`; - } + 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; - /** 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`; - } + /** Windows platform */ + if (process.platform === 'win32') { + userConfigPath = `${userHome}/.config/Soundnode`; + } - if (!fs.statSync(userConfigPath).isDirectory()) { - mkdirp(userConfigPath, err => { - if (err) { - console.error(err); - } - }); - } - return userConfigPath; + /** Linux platforms - XDG Standard */ + if (process.platform === 'linux') { + userConfigPath = `${userHome}/.config/Soundnode`; } - /** - * Get the configuration path - * - * @returns {string} The file location of the config - */ - static get path () { - return `${this.location}/userConfig.json` + /** Mac os configuration location */ + if (process.platform === 'darwin') { + userConfigPath = `${userHome}/Library/Preferences/Soundnode`; } - /** - * Get the config file - * - * @returns {Object} Parsed version of the saved file - */ - static get file () { - return JSON.parse(fs.readFileSync(`${this.path}`, 'utf-8')); + // create user config in path + // if there is no userConfig path + if (!fs.statSync(userConfigPath).isDirectory()) { + this.createUserConfig() } - /** - * Ensure the config exists - * - * @returns {Boolean} True if the file exists - */ - static get configExists () { - return fs.existsSync(`${this.path}`); - } + 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; +module.exports = configuration; From 1fa1494faf7002ff7ea0b2512555be5644fc83be Mon Sep 17 00:00:00 2001 From: Michael Lancaster Date: Wed, 19 Jul 2017 14:38:48 -0700 Subject: [PATCH 08/10] refactor based on the configLocation changes --- app/public/js/system/guiConfig.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/app/public/js/system/guiConfig.js b/app/public/js/system/guiConfig.js index 08a877a9..3be1f089 100644 --- a/app/public/js/system/guiConfig.js +++ b/app/public/js/system/guiConfig.js @@ -4,8 +4,7 @@ const { ipcRenderer } = require('electron'); const fs = require('fs-extra'); -const userHome = require('user-home'); -const Configuration = require('../common/configLocation'); +const configuration = require('../common/configLocation'); let guiConfig = {}; @@ -30,10 +29,10 @@ guiConfig.maximize = function () { }; guiConfig.logOut = function () { - fs.removeSync(Configuration.path); - this.destroy(); + fs.removeSync(configuration.getPath()); + guiConfig.destroy(); }; module.exports = { guiConfig: guiConfig -} \ No newline at end of file +} From 9ff65fe8d5bcae30b569f067d5b6c730b1802393 Mon Sep 17 00:00:00 2001 From: Michael Lancaster Date: Wed, 19 Jul 2017 14:39:22 -0700 Subject: [PATCH 09/10] refactor based on the configLocation changes --- app/public/js/system/settings.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/public/js/system/settings.js b/app/public/js/system/settings.js index d1ce31a8..6732133c 100644 --- a/app/public/js/system/settings.js +++ b/app/public/js/system/settings.js @@ -1,9 +1,8 @@ "use strict"; const ua = require('universal-analytics'); -const fs = require('fs'); -const Configuration = require('../common/configLocation'); -const userConfig = Configuration.file; +const configuration = require('../common/configLocation'); +const userConfig = configuration.getConfigfile(); // Set up some core settings window.settings = {}; @@ -18,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; From 52c131d4165b3c028d9ae30c82dcc7d77a88c272 Mon Sep 17 00:00:00 2001 From: Michael Lancaster Date: Wed, 19 Jul 2017 14:40:10 -0700 Subject: [PATCH 10/10] refactor based on the configLocation changes --- main.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/main.js b/main.js index fa5ff05d..01a24077 100644 --- a/main.js +++ b/main.js @@ -9,7 +9,7 @@ const { Menu } = require('electron'); const windowStateKeeper = require('electron-window-state'); -const Configuration = require('./app/public/js/common/configLocation'); +const configuration = require('./app/public/js/common/configLocation'); // custom constants const clientId = '342b8a7af638944906dcdb46f9d56d98'; @@ -24,9 +24,9 @@ app.on('ready', () => { }); function checkUserConfig() { - const userConfigExists = Configuration.configExists; + const containsConfig = configuration.containsConfig(); - if (userConfigExists) { + if (containsConfig) { initMainWindow(); } else { authenticateUser(); @@ -71,7 +71,7 @@ function authenticateUser() { } function setUserData(accessToken) { - fs.writeFileSync(Configuration.path, 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 +}