|
| 1 | +'use strict'; |
| 2 | + |
1 | 3 | const fs = require('fs-extra'); |
2 | 4 | const userHome = require('user-home'); |
3 | 5 | const mkdirp = require('mkdirp'); |
4 | 6 |
|
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 = { |
14 | 8 |
|
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 | + }, |
19 | 16 |
|
| 17 | + /** |
| 18 | + * Get the configuration folder location |
| 19 | + * |
| 20 | + * @returns {string} The folder location of the config |
| 21 | + */ |
| 22 | + getUserConfig() { |
| 23 | + let userConfigPath = null; |
20 | 24 |
|
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 | + } |
30 | 29 |
|
31 | | - if (!fs.statSync(userConfigPath).isDirectory()) { |
32 | | - mkdirp(userConfigPath, err => { |
33 | | - if (err) { |
34 | | - console.error(err); |
35 | | - } |
36 | | - }); |
37 | | - } |
38 | 30 |
|
39 | | - return userConfigPath; |
| 31 | + /** Linux platforms - XDG Standard */ |
| 32 | + if (process.platform === 'linux') { |
| 33 | + userConfigPath = `${userHome}/.config/Soundnode`; |
40 | 34 | } |
41 | 35 |
|
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`; |
49 | 39 | } |
50 | 40 |
|
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() |
58 | 45 | } |
59 | 46 |
|
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 | + } |
68 | 76 |
|
69 | 77 | } |
70 | 78 |
|
71 | | -module.exports = Configuration; |
| 79 | +module.exports = configuration; |
0 commit comments