mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Cache config.yaml reads
This commit is contained in:
12
src/util.js
12
src/util.js
@ -10,11 +10,20 @@ const { Readable } = require('stream');
|
||||
|
||||
const { PUBLIC_DIRECTORIES } = require('./constants');
|
||||
|
||||
/**
|
||||
* Parsed config object.
|
||||
*/
|
||||
let CACHED_CONFIG = null;
|
||||
|
||||
/**
|
||||
* Returns the config object from the config.yaml file.
|
||||
* @returns {object} Config object
|
||||
*/
|
||||
function getConfig() {
|
||||
if (CACHED_CONFIG) {
|
||||
return CACHED_CONFIG;
|
||||
}
|
||||
|
||||
if (!fs.existsSync('./config.yaml')) {
|
||||
console.error(color.red('No config file found. Please create a config.yaml file. The default config file can be found in the /default folder.'));
|
||||
console.error(color.red('The program will now exit.'));
|
||||
@ -23,6 +32,7 @@ function getConfig() {
|
||||
|
||||
try {
|
||||
const config = yaml.parse(fs.readFileSync(path.join(process.cwd(), './config.yaml'), 'utf8'));
|
||||
CACHED_CONFIG = config;
|
||||
return config;
|
||||
} catch (error) {
|
||||
console.warn('Failed to read config.yaml');
|
||||
@ -47,6 +57,8 @@ function getConfigValue(key, defaultValue = null) {
|
||||
* @param {any} value Value to set
|
||||
*/
|
||||
function setConfigValue(key, value) {
|
||||
// Reset cache so that the next getConfig call will read the updated config file
|
||||
CACHED_CONFIG = null;
|
||||
const config = getConfig();
|
||||
_.set(config, key, value);
|
||||
fs.writeFileSync('./config.yaml', yaml.stringify(config));
|
||||
|
Reference in New Issue
Block a user