|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const fs = require('fs-extra'); |
| 4 | +const userHome = require('user-home'); |
| 5 | +const mkdirp = require('mkdirp'); |
| 6 | + |
| 7 | +const configuration = { |
| 8 | + |
| 9 | + createUserConfig(userConfigPath) { |
| 10 | + mkdirp(userConfigPath, err => { |
| 11 | + if (err) { |
| 12 | + console.error(err); |
| 13 | + } |
| 14 | + }); |
| 15 | + }, |
| 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; |
| 24 | + |
| 25 | + /** Windows platform */ |
| 26 | + if (process.platform === 'win32') { |
| 27 | + userConfigPath = `${userHome}/.config/Soundnode`; |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | + /** Linux platforms - XDG Standard */ |
| 32 | + if (process.platform === 'linux') { |
| 33 | + userConfigPath = `${userHome}/.config/Soundnode`; |
| 34 | + } |
| 35 | + |
| 36 | + /** Mac os configuration location */ |
| 37 | + if (process.platform === 'darwin') { |
| 38 | + userConfigPath = `${userHome}/Library/Preferences/Soundnode`; |
| 39 | + } |
| 40 | + |
| 41 | + // create user config in path |
| 42 | + // if there is no userConfig path |
| 43 | + if (!fs.statSync(userConfigPath).isDirectory()) { |
| 44 | + this.createUserConfig() |
| 45 | + } |
| 46 | + |
| 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 | + } |
| 76 | + |
| 77 | +} |
| 78 | + |
| 79 | +module.exports = configuration; |
0 commit comments