Skip to content

Commit d908edc

Browse files
authored
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.
1 parent 9fe1019 commit d908edc

1 file changed

Lines changed: 61 additions & 53 deletions

File tree

Lines changed: 61 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,79 @@
1+
'use strict';
2+
13
const fs = require('fs-extra');
24
const userHome = require('user-home');
35
const mkdirp = require('mkdirp');
46

5-
class Configuration {
6-
7-
/**
8-
* Get the configuration folder location
9-
*
10-
* @returns {string} The folder location of the config
11-
*/
12-
static get location () {
13-
let userConfigPath = null;
7+
const configuration = {
148

15-
/** Windows platform */
16-
if (process.platform === 'win32') {
17-
userConfigPath = `${userHome}/.config/Soundnode`;
18-
}
9+
createUserConfig(userConfigPath) {
10+
mkdirp(userConfigPath, err => {
11+
if (err) {
12+
console.error(err);
13+
}
14+
});
15+
},
1916

17+
/**
18+
* Get the configuration folder location
19+
*
20+
* @returns {string} The folder location of the config
21+
*/
22+
getUserConfig() {
23+
let userConfigPath = null;
2024

21-
/** Linux platforms - XDG Standard */
22-
if (process.platform === 'linux') {
23-
userConfigPath = `${userHome}/.config/Soundnode`;
24-
}
25-
26-
/** Mac os configuration location */
27-
if (process.platform === 'darwin') {
28-
userConfigPath = `${userHome}/Library/Preferences/Soundnode`;
29-
}
25+
/** Windows platform */
26+
if (process.platform === 'win32') {
27+
userConfigPath = `${userHome}/.config/Soundnode`;
28+
}
3029

31-
if (!fs.statSync(userConfigPath).isDirectory()) {
32-
mkdirp(userConfigPath, err => {
33-
if (err) {
34-
console.error(err);
35-
}
36-
});
37-
}
3830

39-
return userConfigPath;
31+
/** Linux platforms - XDG Standard */
32+
if (process.platform === 'linux') {
33+
userConfigPath = `${userHome}/.config/Soundnode`;
4034
}
4135

42-
/**
43-
* Get the configuration path
44-
*
45-
* @returns {string} The file location of the config
46-
*/
47-
static get path () {
48-
return `${this.location}/userConfig.json`
36+
/** Mac os configuration location */
37+
if (process.platform === 'darwin') {
38+
userConfigPath = `${userHome}/Library/Preferences/Soundnode`;
4939
}
5040

51-
/**
52-
* Get the config file
53-
*
54-
* @returns {Object} Parsed version of the saved file
55-
*/
56-
static get file () {
57-
return JSON.parse(fs.readFileSync(`${this.path}`, 'utf-8'));
41+
// create user config in path
42+
// if there is no userConfig path
43+
if (!fs.statSync(userConfigPath).isDirectory()) {
44+
this.createUserConfig()
5845
}
5946

60-
/**
61-
* Ensure the config exists
62-
*
63-
* @returns {Boolean} True if the file exists
64-
*/
65-
static get configExists () {
66-
return fs.existsSync(`${this.path}`);
67-
}
47+
return userConfigPath;
48+
},
49+
50+
/**
51+
* Get the configuration path
52+
*
53+
* @returns {string} The file location of the config
54+
*/
55+
getPath() {
56+
return `${this.getUserConfig()}/userConfig.json`
57+
},
58+
59+
/**
60+
* Get the config file
61+
*
62+
* @returns {Object} Parsed version of the saved file
63+
*/
64+
getConfigfile() {
65+
return JSON.parse(fs.readFileSync(`${this.getPath()}`, 'utf-8'));
66+
},
67+
68+
/**
69+
* Ensure the config exists
70+
*
71+
* @returns {Boolean} True if the file exists
72+
*/
73+
containsConfig() {
74+
return fs.existsSync(`${this.getPath()}`);
75+
}
6876

6977
}
7078

71-
module.exports = Configuration;
79+
module.exports = configuration;

0 commit comments

Comments
 (0)