Skip to content

Commit d8fe63b

Browse files
refactor: centralize config file path resolution
- Move getConfigFilePath() to server_functions.js - Use in app.js and watcher.js to avoid duplication
1 parent b79bfcc commit d8fe63b

File tree

3 files changed

+16
-20
lines changed

3 files changed

+16
-20
lines changed

js/app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const Utils = require(`${__dirname}/utils`);
1515
const defaultModules = require(`${global.root_path}/modules/default/defaultmodules`);
1616
// used to control fetch timeout for node_helpers
1717
const { setGlobalDispatcher, Agent } = require("undici");
18-
const { getEnvVarsAsObj } = require("#server_functions");
18+
const { getEnvVarsAsObj, getConfigFilePath } = require("#server_functions");
1919
// common timeout value, provide environment override in case
2020
const fetch_timeout = process.env.mmFetchTimeout !== undefined ? process.env.mmFetchTimeout : 30000;
2121

@@ -72,7 +72,7 @@ function App () {
7272

7373
// For this check proposed to TestSuite
7474
// https://forum.magicmirror.builders/topic/1456/test-suite-for-magicmirror/8
75-
const configFilename = path.resolve(global.configuration_file || `${global.root_path}/config/config.js`);
75+
const configFilename = getConfigFilePath();
7676
let templateFile = `${configFilename}.template`;
7777

7878
// check if templateFile exists

js/server_functions.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,12 @@ function getEnvVars (req, res) {
176176
res.send(obj);
177177
}
178178

179-
module.exports = { cors, getConfig, getHtml, getVersion, getStartup, getEnvVars, getEnvVarsAsObj, getUserAgent };
179+
/**
180+
* Get the config file path from environment or default location
181+
* @returns {string} The absolute config file path
182+
*/
183+
function getConfigFilePath () {
184+
return path.resolve(global.configuration_file || `${global.root_path}/config/config.js`);
185+
}
186+
187+
module.exports = { cors, getConfig, getHtml, getVersion, getStartup, getEnvVars, getEnvVarsAsObj, getUserAgent, getConfigFilePath };

serveronly/watcher.js

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
// Load lightweight internal alias resolver to enable require("logger")
2+
require("../js/alias-resolver");
3+
14
const { spawn } = require("child_process");
25
const fs = require("fs");
36
const path = require("path");
47
const net = require("net");
58
const http = require("http");
6-
const Log = require("../js/logger");
9+
const Log = require("logger");
10+
const { getConfigFilePath } = require("#server_functions");
711

812
const RESTART_DELAY_MS = 500;
913
const PORT_CHECK_MAX_ATTEMPTS = 20;
@@ -201,22 +205,6 @@ function watchFile (file) {
201205
}
202206
}
203207

204-
/**
205-
* Get the config file path from environment or default location
206-
* @returns {string} The config file path
207-
*/
208-
function getConfigFilePath () {
209-
if (process.env.MM_CONFIG_FILE) {
210-
return process.env.MM_CONFIG_FILE;
211-
}
212-
213-
if (global.configuration_file && global.root_path) {
214-
return path.resolve(global.root_path, global.configuration_file);
215-
}
216-
217-
return path.join(__dirname, "..", "config", "config.js");
218-
}
219-
220208
startServer();
221209

222210
// Watch the config file (might be in custom location)

0 commit comments

Comments
 (0)