Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions js/app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import { Agent, setGlobalDispatcher } from "undici";
import packageJson from "../package.json" with { type: "json" };
import Log from "./logger.js";
import Server from "./server.mjs";
import Utils from "./utils.js";
import { ConfigError, loadConfig, checkConfigFile, getModulePositions, moduleHasValidPosition } from "./utils.mjs";
import { getEnvVarsAsObj } from "#server_functions";

const { ConfigError } = Utils;
// global absolute root path
global.root_path = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");

Expand Down Expand Up @@ -232,12 +230,12 @@ class App {
global.configuration_file = process.env.MM_CONFIG_FILE.replace(`${global.root_path}/`, "");
}

const configObj = Utils.loadConfig();
const configObj = loadConfig();
global.config = configObj.fullConf;
// Keep a copy of the redacted config to later verify module secret permissions
global.configRedacted = configObj.redactedConf;
const config = global.config;
Utils.checkConfigFile(configObj);
checkConfigFile(configObj);

global.defaultModulesDir = config.defaultModulesDir;
const defaultModulesPath = path.resolve(`${global.root_path}/${global.defaultModulesDir}/defaultmodules`);
Expand All @@ -261,13 +259,13 @@ class App {
}

// get the used module positions
Utils.getModulePositions();
getModulePositions();

const modules = [];
for (const module of config.modules) {
if (module.disabled) continue;
if (module.module) {
if (Utils.moduleHasValidPosition(module.position) || typeof (module.position) === "undefined") {
if (moduleHasValidPosition(module.position) || typeof (module.position) === "undefined") {
// Only add this module to be loaded if it is not a duplicate (repeated instance of the same module)
if (!modules.includes(module.module)) {
modules.push(module.module);
Expand Down
6 changes: 3 additions & 3 deletions js/check_config.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import "./alias-resolver.js";
import Log from "./logger.js";
import Utils from "./utils.js";
import { checkConfigFile, ConfigError } from "./utils.mjs";

try {
Utils.checkConfigFile();
checkConfigFile();
} catch (error) {
if (error instanceof Utils.ConfigError) {
if (error instanceof ConfigError) {
Log.error(error.message);
} else {
const message = error?.message ?? error;
Expand Down
5 changes: 1 addition & 4 deletions js/defaults.js → js/defaults.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,4 @@ const defaults = {
]
};

/*************** DO NOT EDIT THE LINE BELOW ***************/
if (typeof module !== "undefined") {
module.exports = defaults;
}
export default defaults;
34 changes: 16 additions & 18 deletions js/utils.js → js/utils.mjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
const fs = require("node:fs");
const { loadEnvFile } = require("node:process");
import fs from "node:fs";
import { loadEnvFile } from "node:process";
import { styleText } from "node:util";
import Module from "node:module";
import globals from "globals";
import { Linter } from "eslint";
import Log from "./logger.js";
import defaults from "./defaults.mjs";
import { clock as deprecatedClock, configs as deprecatedConfigs } from "./deprecated.mjs";
import { getConfigFilePath } from "#server_functions";

const modulePositions = []; // will get list from index.html
const regionRegEx = /"region ([^"]*)/i;
const indexFileName = "index.html";
const discoveredPositionsJSFilename = "js/positions.js";

const { styleText } = require("node:util");
const Log = require("logger");
const globals = require("globals");
const { Linter } = require("eslint");
const { getConfigFilePath } = require("#server_functions");

const linter = new Linter({ configType: "flat" });

class ConfigError extends Error {
Expand All @@ -27,7 +28,7 @@ class ConfigError extends Error {
* @returns {object} The exported module value.
*/
const requireFromString = (src) => {
const m = new module.constructor();
const m = new Module();
m._compile(src, "");
return m.exports;
};
Expand Down Expand Up @@ -81,20 +82,17 @@ const getModulePositions = () => {
* @param {object} userConfig The user config
*/
const checkDeprecatedOptions = (userConfig) => {
const deprecated = require(`${global.root_path}/js/deprecated.mjs`);

// check for deprecated core options
const deprecatedOptions = deprecated.configs;
const deprecatedOptions = deprecatedConfigs;
const usedDeprecated = deprecatedOptions.filter((option) => userConfig.hasOwnProperty(option));
if (usedDeprecated.length > 0) {
Log.warn(`WARNING! Your config is using deprecated option(s): ${usedDeprecated.join(", ")}. Check README and Documentation for more up-to-date ways of getting the same functionality.`);
}

// check for deprecated module options
for (const element of userConfig.modules) {
if (deprecated[element.module] !== undefined && element.config !== undefined) {
const deprecatedModuleOptions = deprecated[element.module];
const usedDeprecatedModuleOptions = deprecatedModuleOptions.filter((option) => element.config.hasOwnProperty(option));
if (element.module === "clock" && element.config !== undefined) {
const usedDeprecatedModuleOptions = deprecatedClock.filter((option) => element.config.hasOwnProperty(option));
if (usedDeprecatedModuleOptions.length > 0) {
Log.warn(`WARNING! Your config for module ${element.module} is using deprecated option(s): ${usedDeprecatedModuleOptions.join(", ")}. Check README and Documentation for more up-to-date ways of getting the same functionality.`);
}
Expand All @@ -108,7 +106,7 @@ const checkDeprecatedOptions = (userConfig) => {
*/
const loadConfig = () => {
Log.log("Loading config ...");
const defaults = require("./defaults");

if (global.mmTestMode) {
// if we are running in test mode
defaults.address = "0.0.0.0";
Expand Down Expand Up @@ -301,4 +299,4 @@ const checkConfigFile = (configObject) => {
}
};

module.exports = { loadConfig, getModulePositions, moduleHasValidPosition, checkConfigFile, ConfigError };
export { loadConfig, getModulePositions, moduleHasValidPosition, checkConfigFile, ConfigError };
24 changes: 20 additions & 4 deletions tests/unit/classes/utils_spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const fs = require("node:fs");

const Log = require("../../../js/logger");
const { checkConfigFile, ConfigError } = require("../../../js/utils");
import fs from "node:fs";
import { checkConfigFile, ConfigError, loadConfig } from "../../../js/utils.mjs";
import Log from "../../../js/logger.js";

const createConfigObject = (modules, configContentFull = "module.exports = { modules: [] };") => ({
configFilename: "config.js",
Expand Down Expand Up @@ -55,6 +54,23 @@ describe("utils", () => {
expect(Log.error).not.toHaveBeenCalled();
});

it("keeps config __dirname relative to the process working directory", () => {
const previousConfigurationFile = global.configuration_file;
const configFilename = `${process.cwd()}/tests/configs/dirname-test.js`;
global.configuration_file = configFilename;
vi.spyOn(fs, "readFileSync").mockImplementationOnce(() => "module.exports = { modules: [], configDir: __dirname };");

try {
expect(loadConfig().fullConf.configDir).toBe(".");
} finally {
if (previousConfigurationFile === undefined) {
delete global.configuration_file;
} else {
global.configuration_file = previousConfigurationFile;
}
}
});

it("throws when modules is not an array", () => {
expectConfigErrorForModules("not-an-array");
expect(Log.error).not.toHaveBeenCalled();
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/functions/updatenotification_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const createGitHelper = async (fsStatSyncMockRef, loggerMockRef, execGitSpyRef)

vi.doMock("logger", () => loggerMockRef.current);

const defaults = await import("../../../js/defaults");
const defaultsModule = await import("../../../js/defaults.mjs");
const defaults = defaultsModule.default ?? defaultsModule;
const gitHelperModule = await import(`../../../${defaults.defaultModulesDir}/updatenotification/git_helper.js`);
const GitHelper = gitHelperModule.default || gitHelperModule;
const instance = new GitHelper();
Expand Down
9 changes: 4 additions & 5 deletions tests/unit/modules/default/compliments/compliments_spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import defaults from "../../../../../js/defaults.mjs";

describe("Compliments module", () => {
let complimentsModule;

beforeEach(() => {
beforeEach(async () => {
// Mock global dependencies
global.Module = {
register: vi.fn((name, moduleDefinition) => {
Expand All @@ -16,10 +18,7 @@ describe("Compliments module", () => {
global.Cron = vi.fn();

// Load the module
const defaults = require("../../../../../js/defaults");
require(`../../../../../${defaults.defaultModulesDir}/compliments/compliments`);

// Setup module instance
await import(`../../../../../${defaults.defaultModulesDir}/compliments/compliments.js`);
complimentsModule.config = { ...complimentsModule.defaults };
complimentsModule.name = "compliments";
complimentsModule.file = vi.fn((path) => `http://localhost:8080/${defaults.defaultModulesDir}/compliments/${path}`);
Expand Down