Skip to content

Commit c4e3681

Browse files
committed
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.
1 parent 1d40883 commit c4e3681

2 files changed

Lines changed: 33 additions & 7 deletions

File tree

app/public/js/common/configLocation.js

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,60 @@
11
const fs = require('fs-extra');
22
const 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

416
class 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
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"fs-extra": "^2.0.0",
5353
"jquery": "^3.1.1",
5454
"lodash": "^4.17.4",
55+
"mkdirp": "^0.5.1",
5556
"moment": "^2.17.1",
5657
"ng-dialog": "^1.0.0",
5758
"ng-infinite-scroll": "^1.3.0",

0 commit comments

Comments
 (0)