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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Thanks to: @dathbe.
- [clock] Add CSS to prevent line breaking of sunset/sunrise time display (#3816)
- [core] Enhance system information logging format and include additional env and RAM details (#3839, #3843)
- [refactor] Add new file `js/module_functions.js` to move code used in several modules to one place (#3837)
- [refactor] Use global.root_path where possible (#3883)
- [tests] refactor: simplify jest config file (#3844)
- [tests] refactor: extract constants for weather electron tests (#3845)
- [tests] refactor: add `setupDOMEnvironment` helper function to eliminate repetitive JSDOM setup code (#3860)
Expand Down
15 changes: 8 additions & 7 deletions js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,27 @@ const path = require("node:path");
const envsub = require("envsub");
const Log = require("logger");

// global absolute root path
global.root_path = path.resolve(`${__dirname}/../`);

const Server = require(`${__dirname}/server`);
const Utils = require(`${__dirname}/utils`);
const defaultModules = require(`${__dirname}/../modules/default/defaultmodules`);

const defaultModules = require(`${global.root_path}/modules/default/defaultmodules`);
// used to control fetch timeout for node_helpers
const { setGlobalDispatcher, Agent } = require("undici");
const { getEnvVarsAsObj } = require("#server_functions");
// common timeout value, provide environment override in case
const fetch_timeout = process.env.mmFetchTimeout !== undefined ? process.env.mmFetchTimeout : 30000;

// Get version number.
global.version = require(`${__dirname}/../package.json`).version;
global.version = require(`${global.root_path}/package.json`).version;
global.mmTestMode = process.env.mmTestMode === "true";
Log.log(`Starting MagicMirror: v${global.version}`);

// Log system information.
Utils.logSystemInformation(global.version);

// global absolute root path
global.root_path = path.resolve(`${__dirname}/../`);

if (process.env.MM_CONFIG_FILE) {
global.configuration_file = process.env.MM_CONFIG_FILE.replace(`${global.root_path}/`, "");
}
Expand Down Expand Up @@ -180,10 +181,10 @@ function App () {
const elements = module.split("/");
const moduleName = elements[elements.length - 1];
const env = getEnvVarsAsObj();
let moduleFolder = path.resolve(`${__dirname}/../${env.modulesDir}`, module);
let moduleFolder = path.resolve(`${global.root_path}/${env.modulesDir}`, module);

if (defaultModules.includes(moduleName)) {
const defaultModuleFolder = path.resolve(`${__dirname}/../modules/default/`, module);
const defaultModuleFolder = path.resolve(`${global.root_path}/modules/default/`, module);
if (process.env.JEST_WORKER_ID === undefined) {
moduleFolder = defaultModuleFolder;
} else {
Expand Down
7 changes: 3 additions & 4 deletions js/check_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ const Ajv = require("ajv");
const globals = require("globals");
const { Linter } = require("eslint");

const rootPath = path.resolve(`${__dirname}/../`);
const Log = require(`${rootPath}/js/logger.js`);
const Utils = require(`${rootPath}/js/utils.js`);
const Log = require(`${global.root_path}/js/logger.js`);
const Utils = require(`${global.root_path}/js/utils.js`);

const linter = new Linter({ configType: "flat" });
const ajv = new Ajv();
Expand All @@ -19,7 +18,7 @@ const ajv = new Ajv();
*/
function getConfigFile () {
// FIXME: This function should be in core. Do you want refactor me ;) ?, be good!
return path.resolve(process.env.MM_CONFIG_FILE || `${rootPath}/config/config.js`);
return path.resolve(process.env.MM_CONFIG_FILE || `${global.root_path}/config/config.js`);
}

/**
Expand Down
5 changes: 1 addition & 4 deletions js/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
const path = require("node:path");

const rootPath = path.resolve(`${__dirname}/../`);
const Log = require(`${rootPath}/js/logger.js`);
const Log = require(`${global.root_path}/js/logger.js`);
const os = require("node:os");
const fs = require("node:fs");
const si = require("systeminformation");
Expand Down
2 changes: 1 addition & 1 deletion modules/default/calendar/calendarfetcherutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
const moment = require("moment-timezone");

const Log = require("../../../js/logger");
const Log = require("logger");

const CalendarFetcherUtils = {

Expand Down
2 changes: 1 addition & 1 deletion modules/default/calendar/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
// Alias modules mentioned in package.js under _moduleAliases.
require("module-alias/register");
const Log = require("../../../js/logger");
const Log = require("logger");

const CalendarFetcher = require("./calendarfetcher");

Expand Down
6 changes: 2 additions & 4 deletions modules/default/updatenotification/git_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ const fs = require("node:fs");
const path = require("node:path");
const Log = require("logger");

const BASE_DIR = path.normalize(`${__dirname}/../../../`);

class GitHelper {
constructor () {
this.gitRepos = [];
Expand Down Expand Up @@ -35,10 +33,10 @@ class GitHelper {
}

async add (moduleName) {
let moduleFolder = BASE_DIR;
let moduleFolder = `${global.root_path}`;

if (moduleName !== "MagicMirror") {
moduleFolder = `${moduleFolder}modules/${moduleName}`;
moduleFolder = `${moduleFolder}/modules/${moduleName}`;
}

try {
Expand Down
5 changes: 3 additions & 2 deletions modules/default/updatenotification/node_helper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const fs = require("node:fs");
const path = require("node:path");
const NodeHelper = require("node_helper");
const defaultModules = require("../defaultmodules");

const defaultModules = require(`${global.root_path}/modules/default/defaultmodules`);
const GitHelper = require("./git_helper");
const UpdateHelper = require("./update_helper");

Expand All @@ -21,7 +22,7 @@ module.exports = NodeHelper.create({
return modules;
} else {
// get modules from modules-directory
const moduleDir = path.normalize(`${__dirname}/../../`);
const moduleDir = path.normalize(`${global.root_path}/modules`);
const getDirectories = (source) => {
return fs.readdirSync(source, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory() && dirent.name !== "default")
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/fonts_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const helpers = require("./helpers/global-setup");
describe("All font files from roboto.css should be downloadable", () => {
const fontFiles = [];
// Statements below filters out all 'url' lines in the CSS file
const fileContent = require("node:fs").readFileSync(`${__dirname}/../../css/roboto.css`, "utf8");
const fileContent = require("node:fs").readFileSync(`${global.root_path}/css/roboto.css`, "utf8");
const regex = /\burl\(['"]([^'"]+)['"]\)/g;
let match = regex.exec(fileContent);
while (match !== null) {
Expand Down
3 changes: 1 addition & 2 deletions tests/e2e/helpers/basic-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ app.use(basicAuth);

// Set available directories
const directories = ["/tests/configs", "/tests/mocks"];
const rootPath = path.resolve(`${__dirname}/../../../`);

for (let directory of directories) {
app.use(directory, express.static(path.resolve(rootPath + directory)));
app.use(directory, express.static(path.resolve(`${global.root_path}/${directory}`)));
}

let server;
Expand Down
10 changes: 7 additions & 3 deletions tests/e2e/helpers/global-setup.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const path = require("node:path");
const os = require("node:os");
const fs = require("node:fs");
const jsdom = require("jsdom");

const indexFile = `${__dirname}/../../../index.html`;
const cssFile = `${__dirname}/../../../css/custom.css`;
// global absolute root path
global.root_path = path.resolve(`${__dirname}/../../../`);

const indexFile = `${global.root_path}/index.html`;
const cssFile = `${global.root_path}/css/custom.css`;
const sampleCss = [
".region.row3 {",
" top: 0;",
Expand All @@ -29,7 +33,7 @@ exports.startApplication = async (configFilename, exec) => {
process.env.mmTestMode = "true";
process.setMaxListeners(0);
if (exec) exec;
global.app = require("../../../js/app");
global.app = require(`${global.root_path}/js/app`);

return global.app.start();
};
Expand Down
3 changes: 1 addition & 2 deletions tests/e2e/modules/newsfeed_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ describe("Newsfeed module", () => {

describe("Newsfeed module located in config directory", () => {
beforeAll(() => {
const baseDir = `${__dirname}/../../..`;
fs.cpSync(`${baseDir}/modules/default/newsfeed`, `${baseDir}/config/newsfeed`, { recursive: true });
fs.cpSync(`${global.root_path}/modules/default/newsfeed`, `${global.root_path}/config/newsfeed`, { recursive: true });
process.env.MM_MODULES_DIR = "config";
});

Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/serveronly_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe("App environment", () => {
let serverProcess;
beforeAll(async () => {
process.env.MM_CONFIG_FILE = "tests/configs/default.js";
serverProcess = await require("node:child_process").spawn("npm", ["run", "server"], { env: process.env, detached: true });
serverProcess = await require("node:child_process").spawn("node", ["--run", "server"], { env: process.env, detached: true });
// we have to wait until the server is started
await delay(2000);
});
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/vendor_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe("Vendors", () => {
});

describe("Get list vendors", () => {
const vendors = require(`${__dirname}/../../js/vendor.js`);
const vendors = require(`${global.root_path}/js/vendor.js`);

Object.keys(vendors).forEach((vendor) => {
it(`should return 200 HTTP code for vendor "${vendor}"`, async () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/global_vars/defaults_modules_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const path = require("node:path");
const root_path = path.join(__dirname, "../../..");

describe("Default modules set in modules/default/defaultmodules.js", () => {
const expectedDefaultModules = require("../../../modules/default/defaultmodules");
const expectedDefaultModules = require(`${root_path}/modules/default/defaultmodules`);

for (const defaultModule of expectedDefaultModules) {
it(`contains a folder for modules/default/${defaultModule}"`, () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/global_vars/root_path_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const fs = require("node:fs");
const path = require("node:path");

const root_path = path.join(__dirname, "../../..");
const version = require(`${__dirname}/../../../package.json`).version;
const version = require(`${root_path}/package.json`).version;

describe("'global.root_path' set in js/app.js", () => {
const expectedSubPaths = ["modules", "serveronly", "js", "js/app.js", "js/main.js", "js/electron.js", "config"];
Expand Down