Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load preinstalled 3rd party widgets #1274

Closed
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
76 changes: 52 additions & 24 deletions nodes/config/ui_base.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,39 +88,67 @@ module.exports = function (RED) {
}

/**
* Load in third party widgets
*/
let packagePath, packageJson
if (RED.settings?.userDir) {
packagePath = path.join(RED.settings.userDir, 'package.json')
packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf8'))
} else {
node.log('Cannot import third party widgets. No access to Node-RED package.json')
}
* Load in third party widgets
*/

const load3rdPartyWidgetsFromModule = (packageName, modulePath) => {
const packagePath = path.join(modulePath, 'package.json');
console.log("CHECKING: " + packagePath + " for widgets");

const packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf8'));
if (packageJson?.['node-red-dashboard-2']) {
console.log("node-red-dashboard-2 found in " + packagePath);

Object
.entries(packageJson['node-red-dashboard-2'].widgets)
.forEach(([widgetName, widgetConfig]) => {
if(uiShared.contribs[widgetName]) {
console.log("widget " + widgetName + " already loaded");
return;
}

if (packageJson && packageJson.dependencies) {
Object.entries(packageJson.dependencies)?.filter(([packageName, _packageVersion]) => {
return packageName.includes('node-red-dashboard-2-')
}).map(([packageName, _packageVersion]) => {
const modulePath = path.join(RED.settings.userDir, 'node_modules', packageName)
const packagePath = path.join(modulePath, 'package.json')
// get third party package.json
const packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf8'))
if (packageJson?.['node-red-dashboard-2']) {
// loop over object of widgets
Object.entries(packageJson['node-red-dashboard-2'].widgets).forEach(([widgetName, widgetConfig]) => {
uiShared.contribs[widgetName] = {
package: packageName,
name: widgetName,
src: widgetConfig.output,
component: widgetConfig.component
}
};
console.log("loaded widget " + widgetName);
})
}
return packageJson
})
}
return packageJson;
}

const load3rdPartyWidgetsFromPackage = (basePath) => {
// step 1. get the contents of baseFolder/package.json.
let packagePath = path.join(basePath, 'package.json');
let packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf8'));

// step 2. filter out non 'node-red-dashboard-2-' dependencies
if (packageJson && packageJson.dependencies && Object.entries(packageJson.dependencies)) {

Object
.entries(packageJson.dependencies)
.filter(([packageName, packageReference]) => packageName.includes('node-red-dashboard-2-'))
.map(([packageName, packageReference]) => {
// step3. calculate module path based on the packageReference:
// if packageReference starts with "file:" modulePath is relative to basePath
// else modulepath is releative to 'node_modules' folder.
const modulePath = (packageReference.startsWith("file:"))
? path.join(basePath, packageReference.substring("file:".length))
: path.join(basePath, 'node_modules', packageName);

return load3rdPartyWidgetsFromModule(packageName, modulePath);
});
}
};

if(RED.settings?.userDir){ load3rdPartyWidgetsFromPackage(RED.settings.userDir); }
else{ node.log('Cannot import third party widgets from userDir package.json'); }

if(process.cwd()){ load3rdPartyWidgetsFromPackage(process.cwd()); }
else{ node.log('Cannot import third party widgets from root package.json'); }

/**
* Configure Web Server to handle UI traffic
*/
Expand Down