-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathconfig.mjs
107 lines (85 loc) · 2.52 KB
/
config.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import fs from "fs";
import yaml from "js-yaml";
import path from "path";
import * as os from "os";
/**
* Manages the configuration of the CLI. Stored as config.yml
*/
export default class Config {
/**
* The singleton instance.
* @type {Config|null}
*/
static #instance = null;
/* -------------------------------------------- */
/**
* Get the singleton instance of the Config class
* @returns {Config}
*/
static get instance() {
if ( !this.#instance ) this.#instance = new Config();
return this.#instance;
}
/* -------------------------------------------- */
constructor() {
// Set the config file path to the appData directory
let basePath = os.homedir();
switch ( process.platform ) {
case "win32": basePath = process.env.APPDATA || path.join(basePath, "AppData", "Roaming"); break;
case "darwin": basePath = path.join(basePath, "Library", "Preferences"); break;
case "linux": basePath = process.env.XDG_DATA_HOME || path.join(basePath, ".local", "share"); break;
}
fs.mkdirSync(basePath, { recursive: true });
this.configPath = path.join(basePath, ".fvttrc.yml");
// Ensure the config file exists
if ( !fs.existsSync(this.configPath) ) fs.writeFileSync(this.configPath, yaml.dump({}));
this.#config = yaml.load(fs.readFileSync(this.configPath, "utf8"));
}
/* -------------------------------------------- */
/**
* The configuration data.
* @type {Record<string, any>}
*/
#config = {};
/* -------------------------------------------- */
/**
* The path to the configuration file.
* @type {string}
*/
configPath = "";
/* -------------------------------------------- */
/**
* Get the entire configuration object
* @returns {Record<string, any>}
*/
getAll() {
return this.#config;
}
/* -------------------------------------------- */
/**
* Get a specific configuration value
* @param {string} key The configuration key
* @returns {any}
*/
get(key) {
return this.#config[key];
}
/* -------------------------------------------- */
/**
* Set a specific configuration value
* @param {string} key The configuration key
* @param {any} value The configuration value
*/
set(key, value) {
this.#config[key] = value;
// Write to disk
this.#writeConfig();
}
/* -------------------------------------------- */
/**
* Write the configuration to disk
*/
#writeConfig() {
fs.writeFileSync(this.configPath, yaml.dump(this.#config));
}
}