Support cjs files

This commit is contained in:
Nick O'Leary
2025-07-21 16:19:11 +01:00
parent 9b4acba82e
commit 1749ef7ac0
2 changed files with 19 additions and 9 deletions

View File

@@ -54,11 +54,12 @@ function loadModuleTypeFiles(module, type) {
for (let thingName in things) {
/* istanbul ignore else */
if (things.hasOwnProperty(thingName)) {
const thing = things[thingName]
if (module.name != "node-red" && first) {
// Check the module directory exists
first = false;
let moduleFn = module.path
const fn = things[thingName].file
const fn = thing.file
const parts = splitPath(fn)
const nmi = parts.indexOf('node_modules')
if(nmi > -1) {
@@ -78,9 +79,9 @@ function loadModuleTypeFiles(module, type) {
try {
let promise;
if (type === "nodes") {
promise = loadNodeConfig(things[thingName]);
promise = loadNodeConfig(thing);
} else if (type === "plugins") {
promise = loadPluginConfig(things[thingName]);
promise = loadPluginConfig(thing);
}
promises.push(
promise.then(
@@ -91,10 +92,14 @@ function loadModuleTypeFiles(module, type) {
return nodeSet;
}
})()
).catch(err => {console.log(err)})
).catch(err => {
// This shouldn't fail - but if it does, we don't want to crash the loader
// But we also need to log it to have some chance of figuring out what went wrong
console.log(thing.name, err)
})
);
} catch(err) {
console.log(err)
console.log(thing.name, err)
}
}
}
@@ -265,7 +270,7 @@ async function loadNodeConfig(fileInfo) {
module: module,
name: name,
file: file,
template: file.replace(/\.js$/,".html"),
template: file.replace(/\.c?js$/,".html"),
enabled: isEnabled,
loaded:false,
version: version,
@@ -324,6 +329,11 @@ async function loadPluginConfig(fileInfo) {
var htmlFile = file.replace(/\.[^.]+$/,".html");
if (fs.existsSync(jsFile)) {
plugin.file = jsFile;
} else {
jsFile = file.replace(/\.[^.]+$/,".cjs")
if (fs.existsSync(jsFile)) {
plugin.file = jsFile;
}
}
if (fs.existsSync(htmlFile)) {
plugin.template = htmlFile;

View File

@@ -72,11 +72,11 @@ function getLocalFile(file) {
return null;
}
try {
fs.statSync(file.replace(/\.js$/,".html"));
fs.statSync(file.replace(/\.c?js$/,".html"));
return {
file: file,
module: "node-red",
name: path.basename(file).replace(/^\d+-/,"").replace(/\.js$/,""),
name: path.basename(file).replace(/^\d+-/,"").replace(/\.c?js$/,""),
version: settings.version
};
} catch(err) {
@@ -114,7 +114,7 @@ function getLocalNodeFiles(dir, skipValidNodeRedModules) {
files.forEach(function(fn) {
var stats = fs.statSync(path.join(dir,fn));
if (stats.isFile()) {
if (/\.js$/.test(fn)) {
if (/\.c?js$/.test(fn)) {
var info = getLocalFile(path.join(dir,fn));
if (info) {
result.push(info);