11const fs = require ( 'fs-extra' ) ;
22const userHome = require ( 'user-home' ) ;
3+ const mkdirp = require ( 'mkdirp' ) ;
4+
5+ /**
6+ * Check if a given folder exists before trying to access any of it's children
7+ *
8+ * @param {String } folder The folder we're checking
9+ */
10+ const folderExists = folder => {
11+ let exists = false ;
12+ fs . stat ( folder , ( err , stats ) => { if ( stats . isDirectory ( ) ) exists = true ; } ) ;
13+ return exists ;
14+ } ;
315
416class Configuration {
517
618 /**
7- * Get the configuration path
19+ * Get the configuration folder location
820 *
9- * @returns {string } The location of the config
21+ * @returns {string } The folder location of the config
1022 */
11- static get path ( ) {
23+ static get location ( ) {
1224 let userConfigPath = null ;
1325
1426 /** Linux platforms - XDG Standard */
1527 if ( process . platform === 'win32' ) {
16- userConfigPath = `${ userHome } /.config/Soundnode/userConfig.json ` ;
28+ userConfigPath = `${ userHome } /.config/Soundnode` ;
1729 }
1830
1931
2032 /** Linux platforms - XDG Standard */
2133 if ( process . platform === 'linux' ) {
22- userConfigPath = `${ userHome } /.config/Soundnode/userConfig.json ` ;
34+ userConfigPath = `${ userHome } /.config/Soundnode` ;
2335 }
2436
2537 /** Mac os configuration location */
2638 if ( process . platform === 'darwin' ) {
27- userConfigPath = `${ userHome } /Library/Preferences/Soundnode/userConfig.json` ;
39+ userConfigPath = `${ userHome } /Library/Preferences/Soundnode` ;
40+ }
41+
42+ if ( ! folderExists ( userConfigPath ) ) {
43+ mkdirp ( userConfigPath , err => { if ( err ) console . error ( err ) ; } ) ;
2844 }
2945
3046 return userConfigPath ;
3147 }
3248
49+ /**
50+ * Get the configuration path
51+ *
52+ * @returns {string } The file location of the config
53+ */
54+ static get path ( ) {
55+ return `${ this . location } /userConfig.json`
56+ }
57+
3358 /**
3459 * Get the config file
3560 *
@@ -45,7 +70,7 @@ class Configuration {
4570 * @returns {Boolean } True if the file exists
4671 */
4772 static get configExists ( ) {
48- return fs . existsSync ( this . path ) ;
73+ return fs . existsSync ( ` ${ this . path } ` ) ;
4974 }
5075
5176}
0 commit comments